Как привязать данные из join query в FirebaseListAdapter?

Я пытаюсь заполнить ListView с помощью FirebaseListAdapter.
Я добавил следующий прослушиватель для объединения данных:

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) {

        }
    });

Я вижу, что println показывает мне правильные значения.
Как привязать строки round_key и exercise_key к полям android.R. layout.two_line_list_item?

1 ответ

  1. создание пользовательского класса для адаптера :

    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;
        }
    }
    

    пользовательский класс элементов :

    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;
        }
    }
    

    О 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) {
    
            }
        });
    }