简体   繁体   English

如何从Android中的Firebase数据库中按参数获取数据?

[英]How to get data by parameter from Firebase database in Android?

I am making a app, and I have a Firebase real-time database with following JSON structure: 我正在制作一个应用,并且我有一个具有以下JSON结构的Firebase实时数据库:

{
  "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"
    }
  }
}

So I want to retrieve all data from Firebase database by status, that means I want to retrieve all data WHERE status = 1 . 因此,我想按状态从Firebase数据库检索所有数据,这意味着我想在all data WHERE status = 1检索all data WHERE status = 1

Here this is my Class file: 这是我的班级文件:

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

this is my current code for get all data 这是我当前用于获取所有数据的代码

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

One easy way is to get all elements and use only the ones you need 一种简单的方法是获取所有元素并仅使用您需要的元素

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

            }
        });

To solve this, you need to use a query. 要解决此问题,您需要使用查询。 So please use the following code: 因此,请使用以下代码:

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

The output in your logcat will be all the titles from LearnCode objects that have the property status set to 1 . logcat中的输出将是属性status设置为1 LearnCode对象的所有titles

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM