在使用Firestore时如何为RecyclerView添加搜索过滤器? [英] How to add search filter for RecyclerView while working with Firestore?

查看:65
本文介绍了在使用Firestore时如何为RecyclerView添加搜索过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试创建一个显示作家列表的应用程序(图像,名称,生物),但问题是我在实现搜索功能时遇到了问题.我使用的RecyclerView的使用指南为

So, i'm trying to create app that shows list of writers(image,name,bio) and the thing is that I have problem implementing search functionality. The guide for working with RecyclerView I used is here.

代码:MainActivity

Code: MainActivity

public class MainActivity extends AppCompatActivity {

private FirebaseFirestore db;
private FirestoreRecyclerAdapter adapter;
private LinearLayoutManager linearLayoutManager;//nodrosina kada veida recycle attelos
private List<Klasiki> list;

@BindView(R.id.imageRecycleView)
RecyclerView klasikiList;  //tas pats kas find by ID

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

    ButterKnife.bind(this);
    linearLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);
    klasikiList.setLayoutManager(linearLayoutManager);
    db = FirebaseFirestore.getInstance();
    getList();


}

            //atgriez ierakstus no datubazes un nodrosina atteloshanu recycle view
private void getList(){
    Query query = db.collection("klasiki");
    FirestoreRecyclerOptions<Klasiki> response = new FirestoreRecyclerOptions.Builder<Klasiki>()
            .setQuery(query, Klasiki.class)
            .build();

    adapter = new FirestoreRecyclerAdapter<Klasiki, klasikiHolder>(response) {
        @Override
        protected void onBindViewHolder(final klasikiHolder holder, int position, final Klasiki model) {
            holder.name.setText(model.getName());
            holder.shortBio.setText(model.getShortBio());
            Glide.with(getApplicationContext()).load(model.getImage()).into(holder.image);


            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(MainActivity.this, BioActivity.class);

                            //Parnes mainigos uz bio activity
                    Bundle extras = new Bundle();
                    extras.putString("imageInfo", model.getImage());
                    extras.putString("nameInfo", model.getName());
                    extras.putString("bioInfo", model.getFullBio());
                    intent.putExtras(extras);
                    startActivity(intent);

                }
            });

        }

        @Override
        public klasikiHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.klasiki_layout, parent, false);
            return new klasikiHolder(view);
        }                       //nodrosina jauna ieraksta returnu

        @Override
        public void onError(FirebaseFirestoreException e) {
            Log.e("error", e.getMessage());
        }

    };

    adapter.notifyDataSetChanged();
    klasikiList.setAdapter(adapter);

}

        //izsauc mainigos
public class klasikiHolder extends RecyclerView.ViewHolder{
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.klasikiImage)
    ImageView image;
    @BindView(R.id.shortBio)
    TextView shortBio;


    public klasikiHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);

    }
}

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

}

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

构造函数

public class Klasiki {
private String Name;
private String Image;
private String shortBio;
private String fullBio;

public Klasiki(){}

public Klasiki(String name, String image, String shortBio, String fullBio) {
    Name = name;
    Image = image;
    this.shortBio = shortBio;
    this.fullBio = fullBio;
}

public String getName() {
    return Name;
}

public void setName(String name) {
    Name = name;
}

public String getImage() {
    return Image;
}

public void setImage(String image) {
    Image = image;
}

public String getShortBio() {
    return shortBio;
}

public void setShortBio(String shortBio) {
    this.shortBio = shortBio;
}

public String getFullBio() {
    return fullBio;
}

public void setFullBio(String fullBio) {
    this.fullBio = fullBio;
}

}

我想我必须使用除FirestoreRecyclerOptions以外的其他RecyclerView,但是我不确定,这对android编程来说真的很新. :)

I think I have to use other RecyclerViewthan FirestoreRecyclerOptions but i'm not sure, really new to android programming. :)

更新: 所以我做了这样的事情:

UPDATE: So I made something like this:

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.search_menu, menu);
    // Retrieve the SearchView and plug it into SearchManager
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
    SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String text) {
            querySearch = db.collection("klasiki").whereEqualTo("name", text);
            if (!text.trim().isEmpty()){
                getList(querySearch);
                adapter.startListening();
            }
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            if (newText.trim().isEmpty()){
                getList(queryFinal);
                adapter.startListening();
            }
            return false;
        }
    });

    return true;
}

可能不是最好的解决方案,但是现在我遇到了另一个问题.是否有一种方法可以对Firestore进行子字符串搜索?像db.collection("klasiki").whereEqualTo("name", Ja);一样,它显示以Ja开头的名称.

Probably not the best solution, but now I have different problem.. Is there a way to make sub string searches for Firestore? Like db.collection("klasiki").whereEqualTo("name", Ja); And it shows names where it starts with Ja.

推荐答案

要搜索编写者,您需要创建Query.

In order to search for a writter you need to create a Query.

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
Query nameOfTheWritterQuery = rootRef.collection("writters")
    .whereEqualTo("writterName", "Eidemanis Roberts");

看到您的Klasiki类,我需要告诉您,您不尊重 Java命名约定适用于您所有领域的规则,反序列化时可能会遇到问题.您的模型类应如下所示:

Seeing your Klasiki class, I need to tell you that you do not respect Java Naming Conventions rules for all your fields and you can have problems while deserializing. Your model class should look like this:

public class Klasiki {
    private String name, image, shortBio, fullBio;

    public Klasiki( ){}

    public Klasiki(String name, String image, String shortBio, String fullBio) {
        this.name = name;
        this.image = image;
        this.shortBio = shortBio;
        this.fullBio = fullBio;
    }

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

    public String getImage() {return image;}
    public void setImage(String image) {this.image = image;}

    public String getShortBio() {return shortBio;}
    public void setShortBio(String shortBio) {this.shortBio = shortBio;}

    public String getFullBio() {return fullBio;}
    public void setFullBio(String fullBio) {this.fullBio = fullBio;}
}

请立即查看这些字段.所有开始都将是小写字母.

Please see the fields now. All start will small letter.

这篇关于在使用Firestore时如何为RecyclerView添加搜索过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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