适用于Android Studio的Firebase-如何遍历数据库中的每个项目? [英] Firebase For Android Studio - How do you loop through each item in a database?

查看:238
本文介绍了适用于Android Studio的Firebase-如何遍历数据库中的每个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于以下内容的firebase实时数据库:

I have a firebase real-time database that looks like this:

我要创建以下格式的按钮:

I want to create a button in this format:


我该怎么做?


How do I do this?

每个按钮也应该这样做:

Each Button should also do this:

Intent intent = new Intent(getBaseContext(), FishInfo.class);
            intent.putExtra("typeName", "Barbs");
            intent.putExtra("typeInfo", "Sample Description...");
            startActivity(intent);

但是使用正确的typeName和typeInfo

But use the correct typeName and typeInfo

提前谢谢

推荐答案

要在上述数据库中进行迭代,请尝试以下操作:-

To iterate in the above database, try this:-

    DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Categories");
ref.addValueEventListener(new ValueEventListener(){
 @Override
 public void onDataChange(DataSnapshot dataSnapshot) {
  for(DataSnasphot datas: dataSnasphot.getChildren()){
  String num=datas.child("1").getValue().toString();
  String twos=datas.child("2").getValue().toString();
  String threes=datas.child("3").getValue().toString();
  String four=datas.child("4").getValue().toString();
  //so on
 }

 }

 @Override
public void onCancelled(FirebaseError firebaseError) {


 }
});

使用for循环,您将能够在BarbsTetras内部进行迭代,并获取它们内部的属性值(值1,2,3,4,5,6 ..).

Using the for loop, then you will be able to iterate inside Barbs and Tetras, and get the values of the attributes insides them(values of 1,2,3,4,5,6..)

按钮:

     btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {

  Intent intent = new Intent(getBaseContext(), FishInfo.class);
        intent.putExtra("typeName", num);
        intent.putExtra("typeInfo", twos);
        startActivity(intent);
  finish();
   }
  });

这篇关于适用于Android Studio的Firebase-如何遍历数据库中的每个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆