使FirebaseRecyclerAdapter在条件下获取数据,而不是全部 [英] Make FirebaseRecyclerAdapter get data under conditions and not all of them

查看:164
本文介绍了使FirebaseRecyclerAdapter在条件下获取数据,而不是全部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要调整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);



具有google徽标的条目是价格< 200是这个例子中的房子,我根本不出现他们。



单身家庭XML:

 <?xml version = 1.0encoding =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/>


解决方案

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

  List< YourObject> mylist = new ArrayList< YourObject>(); (DataSnapshot dataSnapshot){
for(DataSnapshot postSnapshot){

database.child(houses)。addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange :dataSnapshot.getChildren()){
//过滤数据
String name = postSnapshot.child(name)。getValue()。toString(); $ b $ if(name.equals (test){
YourObject myObject = new YourObject(name);
mylist.add(myObject);
}
}

updatelist );
}

@Override
public void onCancelled(DatabaseError databaseError2){


});

更新您的列表视图

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

//从ta获取数据可以通过HousesAdapter
YourAdapter customAdapter = new YourAdapter(YourClass.this,R.layout.adapter_layout,mylist);

yourListView.setAdapter(customAdapter);




$ b $适配器例子:

public class YourAdapter extends ArrayAdapter {
Context context;

  public YourAdapter(Context context,int resource,List< ;房屋>物品){
super(上下文,资源,物品);
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;

code










$ b $ p


$ b

YourObject类

  public class YourObject {
private String name;
$ b $ public YourObject(String name){
this.name = name;
}

public String getName(){
return this.name;


$ b code $
$ b $ p $布局

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


I need to adjust somehow the FirebaseRecyclerAdapter in order to get some data from my database and not all of them. Is there any way to achieve that? My end goal is to show 10 random entries on the screen! Here is what I tried and the results:

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

The entries with the google logo are the houses that have price < 200 and are the houses in this example that I do not them to be appeared at all.

Single House 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>

解决方案

You can use a ListView instead of this firebaserecycler. Check an example:

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

        }
 });

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

An adapter example:

public class YourAdapter extends ArrayAdapter { 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;
}

}

YourObject class

public class YourObject {
    private String name;

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

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

 }

Layout

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

这篇关于使FirebaseRecyclerAdapter在条件下获取数据,而不是全部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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