android - 使 FirebaseRecyclerAdapter 在条件下获取数据而不是全部

标签 android firebase firebase-realtime-database

我需要以某种方式调整 FirebaseRecyclerAdapter 以便从我的数据库中获取一些数据而不是所有数据。有什么办法可以实现吗?我的最终目标是在屏幕上随机显示 10 个条目!这是我的尝试和结果:

FirebaseRecyclerAdapter<Houses, HousesViewHolder> mHousesRecyclerAdapter = new FirebaseRecyclerAdapter<Houses, HousesViewHolder>(
                Houses.class,
                R.layout.house_single,
                HousesViewHolder.class,
                mDatabaseHouses
        ) {
            @Override
            protected void populateViewHolder(HousesViewHolder viewHolder, Houses house, int position) {
                if (Integer.parseInt(house.getPrice()) > 200) {
                    viewHolder.setHouseName(house.getHouse_name());
                    viewHolder.setCity(house.getCity());
                    viewHolder.setImage(house.getMainFoto(), getApplicationContext());
                    viewHolder.setPrice(house.getPrice());
                }

            }
        };

        mRecyclerView.setAdapter(mHousesRecyclerAdapter);

enter image description here

带有 google Logo 的条目是价格 < 200 的房屋,并且是本示例中我根本不希望它们出现的房屋。

单屋 XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/single_house_image"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/common_google_signin_btn_icon_dark_normal" />

    <TextView
        android:id="@+id/single_house_name_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="24dp"
        android:text="House Name"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        app:layout_constraintLeft_toRightOf="@+id/single_house_image"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/single_house_location_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="8dp"
        android:text="House Location "
        android:textSize="14sp"
        app:layout_constraintLeft_toRightOf="@+id/single_house_image"
        app:layout_constraintTop_toBottomOf="@+id/single_house_name_tv" />

    <TextView
        android:id="@+id/single_house_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="6dp"
        android:text="Price"
        android:textSize="14sp"
        app:layout_constraintLeft_toRightOf="@+id/single_house_image"
        app:layout_constraintTop_toBottomOf="@+id/single_house_location_tv" />
</android.support.constraint.ConstraintLayout>

最佳答案

您可以使用 ListView 代替这个 firebaserecycler。检查一个例子:

List<YourObject> mylist = new ArrayList<YourObject>();

database.child("houses").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
             for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
                 //Filter your data
                 String name = postSnapshot.child("name").getValue().toString();
                 if(name.equals("test") {
                     YourObject myObject = new YourObject(name);
                     mylist.add(myObject);
                 }
             }

             updatelist();
        }

        @Override
        public void onCancelled(DatabaseError databaseError2) {

        }
 });

更新你的 ListView

 void updatelist() {
     ListView yourListView = (ListView) findViewById(R.id.myListView);

     // get data from the table by the HousesAdapter
     YourAdapter customAdapter = new YourAdapter(YourClass.this, R.layout.adapter_layout, mylist);

     yourListView.setAdapter(customAdapter);
 }

适配器示例:

public class YourAdapter extends ArrayAdapter<YourObject> {
    Context context;

    public YourAdapter(Context context, int resource, List<Houses> items) {
        super(context, resource, items);
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;

        YourObject yourObject = getItem(position);

        if (v == null) {
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.adapter_layout, null);
        }

        TextView mytextview = (TextView) v.findViewById(R.id.mytextview);
        mytextview.setText(yourObject.getName());

        return v;
    }
}

你的对象类

public class YourObject {
    private String name;

    public YourObject(String name) {
         this.name = name;
    }

    public String getName() {
        return this.name;
    }

 }

布局

 <ListView
        android:id="@+id/myListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
 </ListView>

关于android - 使 FirebaseRecyclerAdapter 在条件下获取数据而不是全部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46156902/

相关文章:

ios - 从 firebase 下载的图像无法正确缩放,并且直到在表格 View 单元格上滚动后才显示

javascript - Angular/Firestore - 'Where' 查询返回错误 'property does not exist on type AngularFirestoreCollection'

android - 如何使用 3G/UMTS 从 Android 向 PC 发送数据

java - 减少字符串中的字符数

java - Firebase SetValue() 和 SetValueAsync() 方法之间有什么区别以及何时使用它们?

javascript - DELETE 后 GET 时 Firebase 返回空引用

android - Logcat 显示随机/不相关的错误

android - CoordinatorLayout layout_anchor 不起作用

javascript - 如何在不再次调用 db.collection 的情况下获取文档并编辑数据?

ios - 在 firestore 中存储字典值时存在逻辑问题