gpt4 book ai didi

android - 如何在android中的自定义 ListView 中实现搜索?

转载 作者:行者123 更新时间:2023-11-29 14:18:09 26 4
gpt4 key购买 nike

我的应用程序中有一个编辑文本和一个 ListView ,我的 ListView 显示联系人列表。我想要带有编辑文本的 ListView 过滤器。我在谷歌上搜索了很多,发现了一些考试,但没有一个对我有用这是我的代码
我的自定义适配器

public class ContactListAdapter extends ArrayAdapter {

private final Activity activity;
private final List<ContactStock> stocks;
private ArrayList<ContactStock> arraylist;

public ContactListAdapter(Activity activity, List<ContactStock> objects) {
super(activity, R.layout.listview_detail, objects);
this.activity = activity;
this.stocks = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View rowView = convertView;
ContactStockView sv = null;
if (rowView == null) {
// Get a new instance of the row layout view
LayoutInflater inflater = activity.getLayoutInflater();
rowView = inflater.inflate(
R.layout.listview_detail, null);

// Hold the view objects in an object,
// so they don't need to be re-fetched
sv = new ContactStockView();
sv.name = (TextView) rowView.findViewById(R.id.textView1);
sv.number = (TextView) rowView.findViewById(R.id.textView2);

// Cache the view objects in the tag,
// so they can be re-accessed later
rowView.setTag(sv);
} else {
sv = (ContactStockView) rowView.getTag();
}
// Transfer the stock data from the data object
// to the view objects
ContactStock currentStock = (ContactStock) stocks.get(position);
sv.name.setText(currentStock.getName());
sv.number.setText(currentStock.getNumber());

// TODO Auto-generated method stub
return rowView;
}

protected static class ContactStockView {
protected TextView name;
protected TextView number;
}

public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
stocks.clear();
if (charText.length() == 0) {
stocks.addAll(arraylist);
} else {
for (ContactStock cs : arraylist) {
if (cs.getName().contains(charText)) {
stocks.add(cs);
}
}
}
notifyDataSetChanged();
}
}

主类edittext代码为

edittext = (EditText)findViewById(R.id.editText1);
edittext.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
//MainActivity.this.adapt.getFilter().filter(s);
String searchString=edittext.getText().toString();
adapt.filter(searchString);
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub

}

@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub

}
});

没有自定义适配器,它与 getfilter() 一起工作。但我不知道如何使用自定义适配器进行过滤。任何帮助都会得到帮助。提前致谢

最佳答案

这个对我有用:

    @Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
int textlength = cs.length();
ArrayList<ContactStock> tempArrayList = new ArrayList<ContactStock>();
for(ContactStock c: arraylist){
if (textlength <= c.getName().length()) {
if (c.getName().toLowerCase().contains(cs.toString().toLowerCase())) {
tempArrayList.add(c);
}
}
}
mAdapter = new ContactListAdapter(activity, tempArrayList);
lv.setAdapter(mAdapter);
}

关于android - 如何在android中的自定义 ListView 中实现搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21827646/

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