Search code examples
androidfirebase-realtime-databasefirebaseui

How to bind data from join query in FirebaseListAdapter?


I'm trying to populate ListView using FirebaseListAdapter. I've added the following listener to join data:

mWODsReference.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            final String wod_key = dataSnapshot.getKey();
            System.out.println("The wod key is " + wod_key);
            mDatabase.child("wod_rounds/" + wod_key).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                        modelWODRound post = postSnapshot.getValue(modelWODRound.class);
                        String round_key = postSnapshot.getKey();
                        System.out.println("Round key and time cap are " + round_key + ": " + post.getRoundlimit().toString());
                        DatabaseReference refRE = mDatabase.child("round_exercises").child(round_key);
                        refRE.addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                                    String exercise_key = postSnapshot.getKey();
                                    System.out.println("Round exercises are " + exercise_key);
                                }
                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {

                            }
                        });

                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

I can see that println is showing me right values. How can I bind strings round_key and exercise_key to fields of android.R.layout.two_line_list_item?


Solution

  • create custom class for the adapter :

    public class ListAdapter extends ArrayAdapter<CustomItem> {
    
    public ListAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }
    
    public ListAdapter(Context context, int resource, List<Item> items) {
        super(context, resource, items);
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    
        View v = convertView;
    
        if (v == null) {
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.two_line_list_item, null);
        }
    
        CustomItem p = getItem(position);
        if (p != null) {
                TextView tt2 = (TextView) v.findViewById(R.id.round_key);
                TextView tt3 = (TextView) v.findViewById(R.id.exercise_key);
    
    
                if (tt2 != null) {
                    tt2.setText(p.getRoundKey());
                }
    
                if (tt3 != null) {
                    tt3.setText(p.getExerciseKey());
                }
            }
    
            return v;
        }
    }
    

    custom Item class :

    public class CustomItem {
    
        public String round_key;
        public String exercuse_key;
    
        public CustomItem() {
    
        }
    
        public CustomItem(String r_key, String e_key)
        {
            this.round_key=r_key;
            this.exercise_key=e_key;
        }
    
        public String getRoundKey(){
            return this.round_key;
        }
    
        public String getExerciseKey(){
            return this.exercise_key;
        }
    }
    

    On MainActivity :

    private List<CustomItem> customItem;
    private ListAdapter adapter;
    private CustomItem mItem;
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.activity_main);
    
        listView = (ListView)findViewById(R.id.listview1);
    
        customItem = new ArrayList<CustomItem>();
    
        getFirebaseData();
    
        listView.setAdapter(adapter);
    }
    
    private void getFirebaseData(){
            mWODsReference.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                final String wod_key = dataSnapshot.getKey();
                System.out.println("The wod key is " + wod_key);
                mDatabase.child("wod_rounds/" + wod_key).addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                            modelWODRound post = postSnapshot.getValue(modelWODRound.class);
                            String round_key = postSnapshot.getKey();
                            System.out.println("Round key and time cap are " + round_key + ": " + post.getRoundlimit().toString());
                            DatabaseReference refRE = mDatabase.child("round_exercises").child(round_key);
                            refRE.addValueEventListener(new ValueEventListener() {
                                @Override
                                public void onDataChange(DataSnapshot dataSnapshot) {
                                    for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                                        String exercise_key = postSnapshot.getKey();
                                        System.out.println("Round exercises are " + exercise_key);
                                        mItem = new CustomItem(round_key,exercise_key);
                                        customItem.add(mItem);
                                        adapter.notifyDataSetChanged();
                                    }
                                }
    
                                @Override
                                public void onCancelled(DatabaseError databaseError) {
    
                                }
                            });
    
                        }
                    }
    
                    @Override
                    public void onCancelled(DatabaseError databaseError) {
    
                    }
                });
            }
    
            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {
    
            }
    
            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {
    
            }
    
            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {
    
            }
    
            @Override
            public void onCancelled(DatabaseError databaseError) {
    
            }
        });
    }