【问题标题】:How to get data by parameter from Firebase database in Android?如何通过参数从 Android 中的 Firebase 数据库中获取数据?
【发布时间】:2018-08-23 16:02:45
【问题描述】:

我正在制作一个应用程序,我有一个 Firebase 实时数据库,其结构如下:JSON

{
  "learnhtml": {
    "1534958785102": {
      "id": "1534958785102",
      "title": "What is html",
      "details": "What is html",
      "code": "What is html",
      "status":"1"
    },
    "1534959878751": {
      "id": "1534959878759",
      "title": "",
      "details": "What is html",
      "code": "What is html",
      "status":"1"
    } 
    "1534959878753": {
      "id": "1534959878759",
      "title": "",
      "details": "What is html",
      "code": "What is html",
      "status":"2"
    }
  }
}

所以我想按状态从 Firebase 数据库中检索所有数据,这意味着我想检索 all data WHERE status = 1

这是我的类文件:

public class LearnCode {
    String id;
    String title;
    String details;
    String code;
    String type;
    String status;

    public LearnCode() {}

    public LearnCode(String id, String title, String details, String code, String type, String status) {
        this.id = id;
        this.title = title;
        this.details = details;
        this.code = code;
        this.type = type;
        this.status = status;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDetails() {
        return details;
    }

    public void setDetails(String details) {
        details = details;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}

这是我目前获取所有数据的代码

myref = FirebaseDatabase.getInstance().getReference("learnhtml");  
final FirebaseRecyclerAdapter<LearnCode, BlogViewHolder> recyclerAdapter = new FirebaseRecyclerAdapter<LearnCode, BlogViewHolder>(
                    LearnCode.class,
                    R.layout.each_row,
                    BlogViewHolder.class,
                    myref
            ) {
                @Override
                protected void populateViewHolder(BlogViewHolder viewHolder, final LearnCode model, final int position) {
                    progressDialog.dismiss();
                    viewHolder.setTitle(model.getTitle());

                    viewHolder.itemId.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {

                            /*final LearnCode domain = model;

                            rowId = domain.getId();
                            Intent intent = new Intent(shofolotarGolpo.this, Details.class);
                            intent.putExtra("id", rowId);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);*/
                        }
                    });
                }
            };
            recyclerView.setAdapter(recyclerAdapter);
        }

【问题讨论】:

  • 您具体遇到了什么问题?
  • 这段代码没问题....这里没有问题....这段代码适用于所有数据......但我想通过字段获取数据,例如状态为1
  • 听起来您应该阅读有关进行查询的文档。 firebase.google.com/docs/database/android/…

标签: java android firebase firebase-realtime-database firebaseui


【解决方案1】:

一种简单的方法是获取所有元素并仅使用您需要的元素

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

                Iterable<DataSnapshot> mData = dataSnapshot.getChildren();

                for(DataSnapshot d : mData){

                    LearnCode learnCode = d.getValue(LearnCode.class);

                    if(learnCode.getStatus == '1'){
                     //do what u want
                       }

                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

【讨论】:

    【解决方案2】:

    要解决这个问题,您需要使用查询。所以请使用以下代码:

    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    Query query = rootRef.child("learnhtml").orderByChild("status").equalsTo("1");
    ValueEventListener valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                LearnCode learnCode = ds.getValue(LearnCode.class);
                Log.d(TAG, learnCode.getTitle());
            }
        }
    
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            Log.d(TAG, databaseError.getMessage());
        }
    };
    query.addListenerForSingleValueEvent(valueEventListener);
    

    logcat 中的输出将是 LearnCode 对象中的所有 titles 属性 status 设置为 1

    【讨论】:

    • ohhh thnx 但它只是使用:myref = FirebaseDatabase.getInstance().getReference("learnhtml").orderByChild("status").equalTo("1");
    • 在这种情况下,请考虑通过单击左侧投票箭头下方的复选标记来接受我的回答。应将颜色更改为绿色。我会很感激的。谢谢!
    猜你喜欢
    • 2020-12-22
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 2017-01-07
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    相关资源
    最近更新 更多