gpt4 book ai didi

Android ListView 不通过 Filter 刷新

转载 作者:可可西里 更新时间:2023-11-01 19:08:00 28 4
gpt4 key购买 nike

在我的应用程序中,我有三个带有 ViewPager 的 fragment 。其中一个 fragment 我有简单的 Arraylist 作为电话联系人列表中的 ListView,我试图在输入 edittext 后对其进行过滤。但在 softkeyboard 可见之前不会刷新,我必须隐藏键盘才能通过过滤的字符串刷新 ListView 。

例如:

按“a”过滤 ListView :

adapter.getFilter().filter("a");

我的适配器:

public class AdapterContacts extends BaseAdapter implements Filterable {

private LayoutInflater inflater;
private Context context;
private List<ContactLists> categoryArrayList;
private final ArrayList<ContactLists> originalList = new ArrayList<ContactLists>();
private NameFilter filter;

public AdapterContacts(ArrayList<ContactLists> array) {
categoryArrayList = array;
}

public AdapterContacts(Context context, List<ContactLists> array) {
this.context = context;
inflater = LayoutInflater.from(this.context);
categoryArrayList = array;
originalList.addAll(array);
}

@Override
public int getCount() {
return categoryArrayList.size();
}

@Override
public ContactLists getItem(int position) {
return categoryArrayList.get(position);
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mViewHolder;

if (convertView == null) {
convertView = inflater.inflate(R.layout.layout_contacts_list_item, null);
mViewHolder = new ViewHolder(convertView);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (ViewHolder) convertView.getTag();
}

ContactLists item = getItem(position);
mViewHolder.fillItems(this, item, position);

return convertView;
}

private static class UI extends HelperUI {
public TextView tv_person_nickname_mobile_number;
public TextView btn_invite_message;
public ImageView img_contact_image;
public ImageView imgv_user_rank;
public TextView tv_contact_name;
public LinearLayout ll_root;

public UI(View view) {
parseUi(view);
}
}

private class ViewHolder {
private UI UI;

public ViewHolder(View view) {
UI = new UI(view);
}

public void fillItems(final AdapterContacts adapter, final ContactLists item, final int position) {
UI.tv_contact_name.setText(item.getContact_name());

if (item.getStatus() == 1) {
UI.btn_invite_message.setVisibility(View.GONE);
UI.imgv_user_rank.setVisibility(View.VISIBLE);

if (item.getRank() != null || !TextUtils.isEmpty(item.getRank())) {
//Picasso.with(G.context).load(item.getRank()).into(UI.imgv_user_rank);
}

UI.tv_person_nickname_mobile_number.setText(item.getNick_name());
//UI.ll_root.setBackgroundDrawable(G.context.getResources().getDrawable(R.drawable.selector_button_actions));
if (item.getContact_image() == null || TextUtils.isEmpty(item.getContact_image())) {
Bitmap bitmap = UC.getContactPhoto(item.getMobile_number(), G.context.getContentResolver());
if (bitmap != null) {
UI.img_contact_image.setImageBitmap(bitmap);
} else {
UI.img_contact_image.setImageDrawable(G.context.getResources().getDrawable(R.drawable.no_avatar));
}
} else {
// show user avatar from web
//Picasso.with(G.context).load(item.getContact_image()).into(UI.img_contact_image);
UI.img_contact_image.setImageBitmap(BitmapFactory.decodeFile(G.dir_image + "/" + item.getContact_image()));
}
} else {
// UI.ll_root.setBackgroundDrawable(G.context.getResources().getDrawable(R.drawable.selector_invite_actions));
UI.btn_invite_message.setVisibility(View.VISIBLE);
UI.imgv_user_rank.setVisibility(View.GONE);
UI.btn_invite_message.setText(UC.getString(R.string.invite_person));
UI.btn_invite_message.setBackgroundDrawable(G.context.getResources().getDrawable(R.drawable.shape_invite_button_default));
UI.tv_person_nickname_mobile_number.setText(item.getMobile_number());
Bitmap bitmap = UC.getContactPhoto(item.getMobile_number(), G.context.getContentResolver());
if (bitmap != null) {
UI.img_contact_image.setImageBitmap(bitmap);
} else {
UI.img_contact_image.setImageDrawable(G.context.getResources().getDrawable(R.drawable.no_avatar));
}
}
}
}

@Override
public Filter getFilter() {
if (filter == null) {
filter = new NameFilter();
}
return filter;
}

public class NameFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();

String searchText = constraint.toString().toLowerCase();
ArrayList<ContactLists> newList = filterListBasedOnSearchText(searchText);
results.values = newList;
results.count = newList.size();

return results;
}

private ArrayList<ContactLists> filterListBasedOnSearchText(String constraint) {
ArrayList<ContactLists> newList = new ArrayList<ContactLists>();

int l = originalList.size();
for (int i = 0; i < l; i++) {
ContactLists nameList = originalList.get(i);

if (nameList.getContact_name().toString().contains(constraint)) {
newList.add(nameList);
}
}

return newList;
}

@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
categoryArrayList = (ArrayList<ContactLists>) results.values;
notifyDataSetChanged();
}
}
}

ActivityMain list 中的软键盘状态状态。此类具有包含三个 fragment 的 View 寻呼机:

    <activity android:name=".Activities.ActivityBootstrap" android:windowSoftInputMode="adjustPan"  android:screenOrientation="portrait"/>

在没有 Adapter 能力的情况下,在 fragment 中进行 Filter 的其他方法

edt_sample.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

@Override
public void afterTextChanged(Editable s) {
String text = edt_sample.getText().toString();
filter(text);
}
});

public void filter(String charText) {
drinks.clear();
if (charText.length() == 0) {
drinks.addAll(contact_list);
} else {
for (ContactLists wp : contact_list) {
if (wp.getContact_name().contains(charText)) {
drinks.add(wp);
}
}
}
contact_list.clear();
contact_list.addAll(drinks);
adapter.notifyDataSetChanged();
}

当我关闭或隐藏用 nw 项刷新的软键盘时,ListView 成功过滤。

最佳答案

正如我从您发布的代码中看到的那样,您没有使用适配器过滤器。我将在此处发布一个示例过滤器以及如何调用它(保留所有变量名称以使其更容易)。

适配器类中的“NameFilter”类:

public class NameFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();

String searchText = constraint.toString().toLowerCase();
ArrayList<ContactLists> newList = filterListBasedOnSearchText(searchText);
results.values = newList;
results.count = newList.size();

return results;
}

private ArrayList<ContactLists> filterListBasedOnSearchText(String constraint) {
ArrayList<ContactLists> newList = new ArrayList<ContactLists>();

int l = originalList.size();
for (int i = 0; i < l; i++) {
ContactLists nameList = originalList.get(i);

if (nameList.getContact_name().toString().contains(constraint)) {
newList.add(nameList);
}
}

return newList;
}

@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
categoryArrayList = (ArrayList<ContactLists>) results.values;
notifyDataSetChanged();
}
}

列表 fragment 中的“TextWatcher”接口(interface)方法实现:

public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

public void onTextChanged(CharSequence s, int start, int before, int count) {
}

public void afterTextChanged(Editable s) {
String text = searchView.getText().toString();
NameFilter itemFilter = (NameFilter) adapter.getFilter();
itemFilter.filter(text);
}

此外,一些观察结果:

  • 如果“ContactLists”是一个联系人,将其命名为“Contact”,以避免混淆
  • 我会使用“SearchView”,而不是“EditText”
  • 我不知道你是怎么得到联系人列表的,但是有一个关于它的指南here (如果你还没有看过的话)

关于Android ListView 不通过 Filter 刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31096450/

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