Search code examples
androidlistviewfirebasefirebase-realtime-databasefirebaseui

How to create a listview of key and value from Firebase?


I am new to Firebase and from watching some tutorials and reading the docs, I made the app to display the values of a node in a listview. But what I want to do is to display both the key and value in the listview. How can I do that?

What I did:

public class MainActivity extends AppCompatActivity {

private ListView mListView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mListView = (ListView) findViewById(R.id.listView);
    final String med = "Meth";

    final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl("https://fir-test-d6253.firebaseio.com/"+med);

    final FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(
            this,
            String.class,
            android.R.layout.simple_list_item_1,
            databaseReference
    ) {
        @Override
        protected void populateView(View v, String model, int position) {


            TextView textView = (TextView) v.findViewById(android.R.id.text1);
            textView.setText(model);

        }
    };

    mListView.setAdapter(firebaseListAdapter);

}

}`

Database:

 - fir-test-d6253addclose
    -  Meth
        - Store1: "1"
        - Store2: "10"
    - Morphine
        - Store1: "5"
        - Store2: "10"
        - Store3: "10"

The output now is:

 - 1
 - 10

What I want:

 - Store1 1
 - Store2 10

or is there any other way instead of using firebaseui?


Solution

  • kindly post the Adapter code let me help. Else use this link to retrieve the data and display in listview. DataSnapshot parameters int the functions will have the key and its value.