如何在FirebaseListAdapter中绑定来自连接查询的数据?

时间:2017-01-17 06:12:11

标签: android firebase-realtime-database firebaseui

我正在尝试使用FirebaseListAdapter填充ListView。 我添加了以下侦听器来连接数据:

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 个答案:

答案 0 :(得分:0)

为适配器创建自定义类:

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

自定义Item类:

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

        }
    });
}