【问题标题】:how to get data on all child in firebase realtime database android studio如何在firebase实时数据库android studio中获取所有孩子的数据
【发布时间】:2018-04-17 02:36:21
【问题描述】:

这是我的火力基地结构

    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 的数据,但我需要子回复上的标志以供在我的片段中参考。请帮助获取代码。

【问题讨论】:

  • 需要更多关于您想要做什么的详细信息。 Supp.class 看起来如何?请检查你的语法...
  • 对不起我的英语不好(:
  • @Dumbo Supp.class for setter 和 getter

标签: java android firebase firebase-realtime-database


【解决方案1】:

试试这样的:

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

【讨论】:

  • 为什么要这样? SuppList 适配器 = new SuppList(MainActivity2.this, suppList); mListViewSupp.setAdapter(适配器);它可以是构造函数上的一个列表数据吗?
  • 我不明白你在问什么。使用适配器有什么用?
  • 我真的很抱歉我的英语不好。我将 Supp.class 用于 setter 和 getter,将 SuppList.class 用于 ArrayList 和 Constructor。我希望子 Firebase 和回复可以是为 SuppList 添加的一个列表。
  • 你的适配器是什么样子的?
【解决方案2】:

喜欢这个

公共类 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;

}

}

【讨论】:

  • @Dumbo 这样的
猜你喜欢
  • 2016-12-22
  • 1970-01-01
  • 2021-07-03
  • 1970-01-01
  • 1970-01-01
  • 2021-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多