Mike
Mike

Reputation: 41

Firebase returns random values when retrieving object of data to a Listview

I'm trying to retrieve Data from Firebase into a Listview. Although it does retrieve the data correctly, it displays some random values after my project ID. My database looks like this: enter image description here I'm creating an object containing all values within each of those string under "Buyers" and then I want certain values shown in the Listview ("Navn" and "Telefonnr". But as shown, it gives me what appears to be a random generated string (Changes each time I activate the activity). My code looks like this (Buyers class/object shown below): Update!: Solved, updated code is posted to people who need it :)

            private ListView mUserList;

        private ArrayList<String> 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<String> arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mUsernames);

            mUserList.setAdapter(arrayAdapter);

            mDatabaseReference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                    for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {

                        Map<String, String> map = (Map<String, String>) childSnapshot.getValue();
                        Log.v("YourValue,","Map value is:" +map.toString());
                        Log.d("TAG", "onChildAdded:" + dataSnapshot.getKey());
                        Buyers buyerList = new Buyers(map);
                        mUsernames.add(buyerList.getData());
                    }
                    arrayAdapter.notifyDataSetChanged();
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }

This is the "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");
}
String getData()
{
    return (name + phone_Number);
}

Upvotes: 0

Views: 80

Answers (1)

rsd96
rsd96

Reputation: 1114

you are passing a list of objects of your class Buyers populated with data from database hence its printing the id of each of that class. Each of those objects has the data though...

solution:

  • change mUsernames to an ArrayList
  • create a getter inside BuyerList that returns the data you want to display eg : String getData() { return car_value + interest) };
  • then add that data to your list
    mUsernames.add(buyerList.getData);

Upvotes: 1

Related Questions