如何获取firebase实时数据库android studio中所有子项的数据

时间:2018-04-17 02:36:21

标签: java android firebase firebase-realtime-database

这是我的火力架结构

enter image description here

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

            suppList.clear();

            for (DataSnapshot suppSnapshot : dataSnapshot.child("Task").getChildren()) {

                //Log.d("fbBee", dataSnapshot.getRef().child("Task").child("9223450").child("Firebase").toString());

                Log.d("fbBee", suppSnapshot.getValue().toString());
                Log.d("fbGet",suppSnapshot.child("Firebase").getValue().toString());
                Supp supp = suppSnapshot.child("Firebase").getValue(Supp.class);

                suppList.add(supp);

            }

            SuppList adapter = new SuppList(MainActivity2.this, suppList);
            mListViewSupp.setAdapter(adapter);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

这是我的代码,但此代码只显示子Firebase。我想阅读和显示来自儿童Firebase的数据,但是我需要在儿童上的标志回复以供我的fragmens参考。请帮助代码。

2 个答案:

答案 0 :(得分:0)

尝试类似的东西:

final DatabaseReference database = FirebaseDatabase.getInstance().getReference().child("Task/");
database.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot taskNo : dataSnapshot.getChildren()) {
            // now you in (9223450)
            Object firebaseObj = taskNo.child("Firebase").getValue(FirebaseObj.class); //class with params set/get methods
            Object replayObj = taskNo.child("Replay").getValue(ReplayObj.class); //class with params set/get methods

            // ALTERNATIVE
            /*
            for (DataSnapshot child : taskNo.getChildren()) {
                if(child.getKey().equals("Firebase")) {
                    String address = child.child("Address").getValue(String.class);
                    String customer = child.child("Customer").getValue(String.class);
                    // ...
                } else if (child.getKey().equals("Replay")) {
                    // replay 
                    // ...
                }
            }
            */
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) { }
});

答案 1 :(得分:0)

喜欢这个

公共类SuppList扩展了ArrayAdapter {

private Activity context;
private List<Supp> suppList;

public SuppList (Activity context, List<Supp> suppList){

    super(context, R.layout.list_layout_new, suppList);
    this.context = context;
    this.suppList = suppList;

}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();

    View listViewItem = inflater.inflate(R.layout.list_layout_new, null, true);

    TextView textViewSo = (TextView) listViewItem.findViewById(R.id.textViewSo);
    TextView textViewName = (TextView) listViewItem.findViewById(R.id.textViewName);
    TextView textViewAddress = (TextView) listViewItem.findViewById(R.id.textViewAddress);

    Supp supp = suppList.get(position);
    textViewSo.setText(supp.getSuppId());
    //Log.e("SuppTest", supp.getSuppId());
    textViewName.setText(supp.getSuppName());
    textViewAddress.setText(supp.getSuppAddress());

    return listViewItem;

}

}