这是我的CustomAdapter类,我有一个函数调用setFilter
package Adapter;
public class CustomAdapter extends
RecyclerView.Adapter<CustomAdapter.ViewHolder> {
private Context c;
private List list;
public CustomAdapter(Context context, List itemlist) {
this.c= context;
this.list=itemlist;
}
@NonNull
@Override
public CustomAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup
parent, int viewType) {
View view=LayoutInflater.from(parent.getContext()).
inflate(R.layout.custom_layout,parent,false
);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull CustomAdapter.ViewHolder holder, int
position) {
Contact_Object listItem = (Contact_Object) list.get(position);
holder.text1.setText(listItem.getName());
holder.text2.setText(listItem.getNumber());
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener{
TextView text1,text2;
ViewHolder(View view) {
super(view);
view.setOnClickListener(this);
text1=view.findViewById(R.id.text1);
text2=view.findViewById(R.id.text2);
}
public void setFilter(List<Contact_Object> listItem){
list=new ArrayList<>();
list.addAll(listItem);
notifyDataSetChanged();
}
@Override
public void onClick(View v) {
int position=getAdapterPosition();
Contact_Object item= (Contact_Object) list.get(position);
Intent intent=new Intent(c, ContactDetails.class);
intent.putExtra("name",item.getName());
intent.putExtra("number",item.getNumber());
c.startActivity(intent);
}
}
}
这是我要调用setFilter函数的MainActivity类我在用户搜索时使用searchView setOnQueryListener我想将找到的列表显示为下拉列表
this is how I have setup my showContact function and adapter object of
CustomAdapter class
void showContacts() {
// Check the SDK version and whether the permission is already granted
or not.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
checkSelfPermission(Manifest.permission.READ_CONTACTS) !=
PackageManager.PERMISSION_GRANTED
&& checkSelfPermission(Manifest.permission.WRITE_CONTACTS) !=
PackageManager.PERMISSION_GRANTED) {
for (String permission : PERMISSIONS) {
if (ActivityCompat.checkSelfPermission(this, permission) !=
PackageManager.PERMISSION_GRANTED) {
requestPermissions(PERMISSIONS, PERMISSION_ALL);
return;
}
}
}
else
{// Android version is lesser than 6.0 or the permission is already
granted.
ArrayList<Contact_Object> contacts=getContactNames();
adapter=new CustomAdapter(this,contacts);
recyclerView.setAdapter(adapter);
}
}
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
if (!searchView.isIconified()){
searchView.setIconified(true);
}
myMenuItem.collapseActionView();
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
final List<Contact_Object> filtermodelist=filter(list,newText);
//here i am calling like adapter.setFilter(filterlist);
return false;
}
});
但是adapter.setFilter()显示错误; 请任何人帮助我:(
答案 0 :(得分:0)
你的方法
public void setFilter(List<Contact_Object> listItem){
list=new ArrayList<>();
list.addAll(listItem);
notifyDataSetChanged();
}
似乎在ViewHolder类中无法从类的外部访问,在ViewHolder类之外移动此setFilter方法