gpt4 book ai didi

android - SearchView 过滤器与 RecyclerView 过滤结果错误

转载 作者:行者123 更新时间:2023-11-29 15:40:24 25 4
gpt4 key购买 nike

我有一个带有 RecyclerView 的 SearchView,用于搜索联系人列表。但是搜索结果结果似乎是错误的。正在进行一些过滤,但不是正确的过滤。例如,如果我使用字符“a”进行搜索,它将随机过滤掉一些内容,而不是以 a 开头的联系人。代码 fragment 如下。

Activity 类

    public class MembersActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {

@BindView(R.id.meetup_members_recycler_view)
RecyclerView mRecyclerView;

CustomContactAdapter contactAdapter;
List<Contact> contactList = new ArrayList();
private SearchView mSearchView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_meetup_members);
ButterKnife.bind(this);
mSearchView = (SearchView) findViewById(R.id.action_search);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

getAllContacts();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);

final MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(this);

return true;
}

private void getAllContacts() {

Contact contact;

ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {

int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
if (hasPhoneNumber > 0) {
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

contact = new Contact();
contact.setName(name);

Cursor phoneCursor = contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id},
null);
if (phoneCursor.moveToNext()) {
String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contact.setMobile(phoneNumber);
}

phoneCursor.close();
contactList.add(contact);
}
}

contactAdapter = new CustomContactAdapter(getApplicationContext(),contactList);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(contactAdapter);
}
}


@Override
public boolean onQueryTextSubmit(String query) {
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
contactAdapter.getFilter().filter(newText);
return true;
}
}

适配器类

   public class CustomContactAdapter extends RecyclerView.Adapter<CustomContactAdapter.ContactViewHolder>
implements CompoundButton.OnCheckedChangeListener,Filterable {

Context mContext;
LayoutInflater mInflater;
private List<Contact> contactList = new ArrayList<Contact>();
private List<Contact> filterContactList = new ArrayList<Contact>();
private ContactFilter contactFilter;

public CustomContactAdapter(Context context,List<Contact> contacts)
{
mInflater = LayoutInflater.from(context);
mContext = context;
this.contactList = contacts;
this.filterContactList = contacts;

}

public static class ContactViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
@BindView(R.id.member_name)
TextView mContactName;
@BindView(R.id.member_mobile)
TextView mPhoneNumber;
@BindView(R.id.member_image)
ImageView mImage;

public ContactViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
view.setOnClickListener(this);
}

@Override
public void onClick(View view){

}
}

@Override
public CustomContactAdapter.ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.member_list_row, parent, false);

return new CustomContactAdapter.ContactViewHolder(itemView);
}

@Override
public void onBindViewHolder(CustomContactAdapter.ContactViewHolder holder, int position) {

Contact contact = contactList.get(position);
holder.mContactName.setText(contact.getName());
holder.mPhoneNumber.setText(contact.getMobile());

}

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

@Override
public Filter getFilter() {
if(contactFilter == null)
contactFilter = new ContactFilter();
return contactFilter;
}


private class ContactFilter extends Filter {

@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint!=null && constraint.length()>0) {
ArrayList<Contact> tempList = new ArrayList<Contact>();

for (Contact contact : contactList) {
if (contact.getName().toLowerCase().contains(constraint.toString().toLowerCase())) {
tempList.add(contact);
}
}

filterResults.count = tempList.size();
filterResults.values = tempList;
System.out.println("performFiltering:SearchFoundCount "+ tempList.size());
System.out.println("performFiltering:ActualSize "+ contactList.size());

} else {
filterResults.count = contactList.size();
filterResults.values = contactList;
}

return filterResults;
}

/**
* Notify about filtered list to ui
* @param constraint text
* @param results filtered result
*/
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filterContactList = (ArrayList<Contact>) results.values;
notifyDataSetChanged();
}
}
}

无法做到这一点,可能是我遗漏了一些愚蠢的东西。感谢任何帮助。

最佳答案

我在我的应用程序中做了类似的事情。在 onBindViewHolder 中,您应该使用过滤列表而不是初始列表

@Override
public void onBindViewHolder(CustomContactAdapter.ContactViewHolder holder, int position) {

Contact contact = filterContactList.get(position);
...

希望这对你有用。 :)

关于android - SearchView 过滤器与 RecyclerView 过滤结果错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41379204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com