我正在创建一个电话目录应用,我想在不同的活动中显示不同的类别数据。我想帮助人们轻松地以不同的类别查找他们的信息
它向我显示了数据库中的所有数据。但我不希望仅在此处显示“救护车联系人”类别数据。

package com.example.moulvibazarinfo.allCatagory;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.view.ActionMode;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.moulvibazarinfo.AllAdapters.DataSetFire;
import com.example.moulvibazarinfo.AllAdapters.DetailsViewActivity;
import com.example.moulvibazarinfo.AllAdapters.FirebaseViewHolder;
import com.example.moulvibazarinfo.AllAdapters.SubmitedContact;
import com.example.moulvibazarinfo.R;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;

public class AmbulanceActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private ArrayList<DataSetFire> arrayList;
    private FirebaseRecyclerOptions<DataSetFire> options;
    private FirebaseRecyclerAdapter<DataSetFire, FirebaseViewHolder> adapter;
    private DatabaseReference databaseReference;

    @Override
    protected void onStart() {
        super.onStart();
        adapter.startListening();
    }

    @Override
    protected void onStop() {
        super.onStop();
        adapter.stopListening();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ambulance);

        recyclerView = findViewById(R.id.ambulanceRecycleListViewID);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        arrayList = new ArrayList<DataSetFire>();
        databaseReference = FirebaseDatabase.getInstance().getReference().child("MiContact");
        databaseReference.keepSynced(true);




        options = new FirebaseRecyclerOptions.Builder<DataSetFire>().setQuery(databaseReference, DataSetFire.class).build();


        adapter = new FirebaseRecyclerAdapter<DataSetFire, FirebaseViewHolder>(options) {
            @Override
            protected void onBindViewHolder(@NonNull final FirebaseViewHolder holder, int position, @NonNull final DataSetFire model) {

            //filtering database




                holder.nameTextView.setText("name: "+model.getName());
                holder.institutionNameTextView.setText("institution Name: "+model.getInstitutionName());
                holder.emailTextView.setText("Email: "+model.getEmail());
                holder.numberTextView.setText("Number: "+model.getNumber());
                holder.categoryTextView.setText("category: "+model.getCategory());
                holder.addressTextView.setText("address: "+model.getAddress());


                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(AmbulanceActivity.this,DetailsViewActivity.class);
                        intent.putExtra("name",model.getName());
                        intent.putExtra("institutionName",model.getInstitutionName());
                        intent.putExtra("email",model.getEmail());
                        intent.putExtra("number",model.getNumber());
                        intent.putExtra("category",model.getCategory());
                        intent.putExtra("address",model.getCategory());
                        startActivity(intent);
                    }
                });

            }

            @NonNull
            @Override
            public FirebaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                return new FirebaseViewHolder(LayoutInflater.from(AmbulanceActivity.this).inflate(R.layout.row, parent, false));
            }
        };


        recyclerView.setAdapter(adapter);
    }

}



请有人帮帮我!我是Java新手。

最佳答案

要在Firebase实时数据库中订购数据,可以使用其orderByChild()方法:

databaseReference = FirebaseDatabase.getInstance().getReference().child("MiContact");
Query query = databaseReference.orderByChild("category");

options = new FirebaseRecyclerOptions.Builder<DataSetFire>().setQuery(query, DataSetFire.class).build();


假设该属性在我们的JSON中称为category。如果名称不同,请更新orderByChild(...)调用。

您的其余代码可以保持不变。通过此简单更改,数据库服务器,Firebase SDK和FirebaseUI适配器可以一起工作,以按请求的顺序显示联系人。



编辑:如果要过滤以仅显示category等于Ambulance的项目,则可以通过更改查询来做到这一点,如下所示:

Query query = databaseReference.orderByChild("category").equalTo("Ambulance");




我强烈建议您查阅ordering and filtering data上的Firebase文档,也许还请查阅codelab for Android developers

关于java - 如何在FirebaseRecyclerAdapter中设置orderbychild?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59349224/

10-09 16:04