Reputation: 41
I have a database on Firebase and are trying to get android to print it into a ListView. I can get the ListView to print something, but it seems like it's some ID's of the values, rather than the actual values. The point is to be able to make a search bar, which can find any data on the database. I have some commented code down in the addValueEventListener. This code should work, but when I use it, my app crashes without an error when I go to the "ExistingCustomerActivity" activity. My database structure and the output from the app, with the current code. Be aware the ListView currently prints the values just under "Buyers". Is there anyone who can help with this issue? Thanks in advance. Update: I can see the data are loaded correctly in the log, but it still displays some kind of ID I assume.
public class ExistingCustomerActivity extends AppCompatActivity {
private DatabaseReference mDatabaseReference;
private ListView mUserList;
private ArrayList<Buyers> mUsernames = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_existing_customer);
mDatabaseReference = FirebaseDatabase.getInstance().getReference("Buyers");
mUserList = (ListView) findViewById(R.id.UserListView);
final ArrayAdapter<Buyers> arrayAdapter = new ArrayAdapter<Buyers>(this, android.R.layout.simple_list_item_1, mUsernames);
mUserList.setAdapter(arrayAdapter);
mDatabaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Buyers buyerList = new Buyers(childSnapshot);
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
Map<String, String> map = (Map<String, String>) childSnapshot.getValue();
Log.v("YourValue,","Map value is:" +map.toString());
Buyers buyerList = new Buyers(map);
mUsernames.add(buyerList);
}
arrayAdapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
Buyers class:
public class Buyers {
private String car_Value;
private String interest;
private String driverLicenseNr;
private String name;
private String personNr;
private String phone_Number;
public Buyers(Map<String,String> map){
car_Value = map.get("Bil");
interest = map.get("Interresse");
driverLicenseNr = map.get("Kørekortnr");
name = map.get("Navn");
personNr = map.get("Personnr");
phone_Number = map.get("Telefonnr");
}
Upvotes: 3
Views: 273
Reputation: 6899
You need to change your Buyers
class variable according to firebase DB structure or use Map to get the values
Try below code to get values using Map from Buyers
mDatabaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Buyers buyerList = new Buyers(childSnapshot);
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
Map<String,String> map=(Map<String, String>) childSnapshot.getValue();
Buyers buyerList = new Buyers(map);
mUsernames.add(buyerList);
}
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Change your Constructor from DataSnapshot
to Map
and get the values and set into your local Buyers
variable.
public class Buyers {
private String car_Value;
private String interest;
private String driverLicenseNr;
private String name;
private String personNr;
private String phone_Number;
public Buyers(Map<String,String map){
car_Value = map.get("Bil");
interest = map.get("Interresse")
driverLicenseNr = map.get("Korekortnr")
name = map.get("Navn")
personNr = map.get("Personnr")
phone_Number =map.get("Telefonnr")
}
}
Upvotes: 2
Reputation: 6622
you have to do like below.
mDatabaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Buyers buyerList = new Buyers(childSnapshot);
for (DataSnapshot childSnapshot: dataSnapshot.getChildren())
{
Buyers buyerList = childSnapshot.getValue(Buyers.class);
//buyerList = childSnapshot.child("PersonNumberTextField").getValue(Buyers.class);
//Buyers buyerList = childSnapshot.child("PersonNumberTextField").getValue(Buyers.class);
//Buyers buyerList = childSnapshot.getValue(Buyers.class);
mUsernames.add(buyerList);
}
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Upvotes: 0