无法解析方法 .getFilter()
Posted
技术标签:
【中文标题】无法解析方法 .getFilter()【英文标题】:Can't resolve method .getFilter() 【发布时间】:2020-07-09 22:50:49 【问题描述】:我似乎无法找到我的适配器的问题,即我无法在 OnQueryTextChange 上使用 .getFilter() 方法。我将我的适配器称为 mAdapter,但没有用于 .getFilter() 的方法。我相信我的适配器存在一些问题,但我花了很长时间才弄清楚。
这是我的适配器
package Adapters;
import android.com.example.weddingappfinale.MuaView;
import android.com.example.weddingappfinale.R;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class MuaAdapter extends RecyclerView.Adapter<MuaAdapter.MuaViewHolder> implements Filterable
private List<MuaView> mMuaView;
private List<MuaView> mMuaViewFull;
public static class MuaViewHolder extends RecyclerView.ViewHolder
public ImageButton mImageButton;
public TextView mTextView1;
public MuaViewHolder(@NonNull View itemView)
super(itemView);
mImageButton = itemView.findViewById(R.id.mua_imageButton);
mTextView1 = itemView.findViewById(R.id.mua_title);
public MuaAdapter(List<MuaView> muaView)
this.mMuaView = muaView;
mMuaViewFull = new ArrayList<>(muaView);
@NonNull
@Override
public MuaViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.mua_view, parent, false);
MuaViewHolder mvh = new MuaViewHolder(v);
return mvh;
@Override
public void onBindViewHolder(@NonNull MuaViewHolder holder, int position)
MuaView currentView = mMuaView.get(position);
holder.mImageButton.setImageResource(currentView.getImageResource());
holder.mTextView1.setText(currentView.getText1());
@Override
public int getItemCount()
return mMuaView.size();
@Override
public Filter getFilter()
return MuaFilter;
private Filter MuaFilter = new Filter()
@Override
protected FilterResults performFiltering(CharSequence constraint)
List<MuaView> filteredList = new ArrayList<>();
if (constraint == null || constraint.length() == 0)
filteredList.addAll(mMuaViewFull);
else
String filterPattern = constraint.toString().toLowerCase().trim();
for (MuaView item : mMuaViewFull)
if (item.getText1().toLowerCase().contains(filterPattern))
filteredList.add(item);
FilterResults results = new FilterResults();
results.values = filteredList;
return results;
@Override
protected void publishResults(CharSequence constraint, FilterResults results)
mMuaView.clear();
mMuaView.addAll((ArrayList) results.values);
notifyDataSetChanged();
;
这是我的活动
package android.com.example.weddingappfinale;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.inputmethod.EditorInfo;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import Adapters.MuaAdapter;
public class MuaActivity extends AppCompatActivity
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.mua_list);
getSupportActionBar().setTitle("Make Up Artists");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ArrayList<MuaView> muaView = new ArrayList<>();
muaView.add(new MuaView(R.drawable.mua_image, "Make Up Artist Pte Ltd"));
muaView.add(new MuaView(R.drawable.mua_image, "Aake Up Artist Pte Ltd"));
muaView.add(new MuaView(R.drawable.mua_image, "Lake Up Artist 3Pte Ltd"));
muaView.add(new MuaView(R.drawable.mua_image, "f Up Artist Pte Ltd"));
// ArrayList
mRecyclerView = findViewById(R.id.recycler_view_list);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new MuaAdapter(muaView);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
@Override
public boolean onCreateOptionsMenu(Menu menu)
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
@Override
public boolean onQueryTextSubmit(String query)
return false;
@Override
public boolean onQueryTextChange(String newText)
mAdapter.getFilter().filter(newText);
);
return true;
【问题讨论】:
【参考方案1】:@Override
注释告诉编译器您要覆盖 RecyclerView.Adapter
中的 getFilter
方法 - 但没有这样的方法。删除注释。
// @Override <-- REMOVE THIS
public Filter getFilter()
return MuaFilter;
您还需要更改活动中的成员变量声明。
private MuaAdapter mAdapter;
【讨论】:
【参考方案2】:private RecyclerView.Adapter mAdapter;
替换
private MuaAdapter mAdapter;
问题是getFilter方法只存在于MuaAdapter类中,而RecyclerView.Adapte对getFilter一无所知
【讨论】:
非常感谢先生。是的。这是正确的。欣赏它。以上是关于无法解析方法 .getFilter()的主要内容,如果未能解决你的问题,请参考以下文章
无法解析方法'setWebChromeClient(匿名android