【问题标题】:how to filter searchview in a recyclerview based on two different object arraylist如何根据两个不同的对象arraylist过滤recyclerview中的searchview
【发布时间】:2026-02-17 14:35:02
【问题描述】:

目前,我需要在 Android Studio 中进行搜索视图。我正在使用 recyclerview 来显示我的所有信息。我的搜索视图现在可以根据名称而不是状态进行过滤。

(我认为问题是循环中的 Arraylist 仅包含名称和链接信息,但不包含其他四个数据(第三个构造函数)。谁能告诉我如何过滤两种类型的信息(用户可以输入名称或状态在同一个空间中)在一个搜索视图中?非常感谢 :)。

这是我的对象类,它由 3 种类型的构造函数组成。

public A(){

    }

    public A(String name,String link){
        this.name = name;

        this.link = link;
    }

    public A(String cname,String ad,String ad2, String state){

        this.cname=cname;
        this.ad=ad;
        this.ad2=ad2;
        this.state=state;

    }

这是我的 Java 类

ArrayList<A> t;
ArrayList<A> c;
ArrayList<String> searchnamestate;

  databaseReference = FirebaseDatabase.getInstance().getReference();
        databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                for (DataSnapshot dataSnapshot1 : dataSnapshot.child("alluser").child("thera").getChildren()) {
                    A a= dataSnapshot1.getValue(A.class);
                    tkey.add(dataSnapshot1.getKey());
//add the name into an String arraylist named searchnamestate             searchnamestate.add(dataSnapshot1.getValue(A.class).getName().toLowerCase());
                    t.add(a);
                }
                for (DataSnapshot dataSnapshot1 : dataSnapshot.child("company").getChildren()) {
                    A comp = dataSnapshot1.getValue(A.class);
//add the state into an String arraylist named searchnamestate                    searchnamestate.add(dataSnapshot1.getValue(A.class).getState().toLowerCase());
                   c.add(comp);
                }

                adapter = new MyRecyclerViewAdapter(MainActivity.this, t,c, tkey);
                rv.setAdapter(adapter);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(getApplicationContext(), "Oh no!", Toast.LENGTH_SHORT).show();
            }
        });

//searchview filter the information display in recyclerview based on what type
//in the searchview
search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                //    adapter.getFilter().filter(newText);

                String newquery= newText.toLowerCase();
                for(int i=0; i<searchnamestate.size();i++) {
                    if (searchnamestate.get(i).contains(newquery)) {
                        adapter.filter(newquery);
                    }
                    else{
                      // Toast.makeText(MainActivity.this, "No Match found", Toast.LENGTH_LONG).show();
                    }
                }

                return false;
            }
        });


    }

这是我的适配器类

 ArrayList<Therapist> tlist;
    ArrayList<Therapist> newtlist;
    ArrayList<Therapist> complist;

//the t is using the second constructor while comp is the third constuctor in //Object class
 public MyRecyclerViewAdapter(Context c, ArrayList<A> t, ArrayList<A> comp, ArrayList<String> tk ){
        context=c;
        tlist=t;
        complist=comp;
//add all the element from tlist into newtlist
        this.newtlist = new ArrayList<A>();
        this.newtlist.addAll(t);
tkey=tk;

    }

 public void filter(String findtext) {
        //  findtext = findtext.toLowerCase(Locale.getDefault());
        tlist.clear();
        if (findtext.length() == 0) {
            tlist.addAll(newtlist);
        } else {
            for (A wp : newtlist) {
//if the name type in searchview match name that stored in newtlist, it can filter based in name
                if (wp.getName().toLowerCase(Locale.getDefault()).contains(findtext)) {
                    tlist.add(wp);
                }

            }
        }
        notifyDataSetChanged();
    }

 @Override
    public int getItemCount() {
        return tlist.size();
    }

【问题讨论】:

    标签: android firebase android-recyclerview searchview


    【解决方案1】:

    A 类中为state 创建一个getter 方法

    然后添加另一个条件wp.getState().toLowerCase().contains(findtext)

     public void filter(String findtext) {
            //  findtext = findtext.toLowerCase(Locale.getDefault());
            tlist.clear();
            if (findtext.length() == 0) {
                tlist.addAll(newtlist);
            } else {
                for (A wp : newtlist) {
    //if the name type in searchview match name that stored in newtlist, it can filter based in name
                    if (wp.getName().toLowerCase().contains(findtext) 
                        || wp.getState().toLowerCase().contains(findtext)) {
    
                        tlist.add(wp);
                    }
    
                }
            }
           notifyDataSetChanged();
        }
    

    【讨论】:

      【解决方案2】:

      我想你想要按州搜索列表。 只需在 if 语句中添加更多条件 if (wp.getName().toLowerCase(Locale.getDefault()).contains(findtext) || wp.getName().toLowerCase(Locale.getDefault()).contains(findtext)){ tlist.add(wp); }

      【讨论】:

        最近更新 更多