Android ListView过滤器不起作用 [英] Android ListView Filter not working
问题描述
有人可以帮助我使用一个我一直困扰着它的自定义适配器来过滤列表..我一直关注logcat,它确实输出了过滤后的结果,我的问题是用过滤后的结果更新列表视图.
Can someone help me to filter the list using a custom adapter I have been stuck on this for sometime now.. I have followed logcat and It does output the filtered results my problem is updating the listview with the filtered results.
// list of data items
private static List<ListData> mDataList = Arrays.asList(
new ListData("Apple"),
new ListData("Banana"),
new ListData("Caramel"),
new ListData("Dog"),
new ListData("Cat"),
new ListData("Snake"),
new ListData("Lion"),
new ListData("Zebra"),
new ListData("Ant"),
new ListData("Bee"),
new ListData("Bird"),
new ListData("Leopard"),
new ListData("Flamingo"),
new ListData("Tusk"),
new ListData("Elephant"),
new ListData("Chicken")
);
SampleAdapter work = new SampleAdapter();
private Button btnoe;
private AutoCompleteTextView aoe;
private TextView txtHistory, ao;
private ListView lv;
private String[] places;
private String p = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
final EditText e = (EditText) findViewById(R.id.edt_search);
// init the list view and its adapter
final ListView lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(work);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
e.setText("" + mDataList.get(position).data);
}
});
//setFields();
e.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
work.getFilter().filter(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
private static class ListData {
private String data;
public ListData(String data) {
this.data = data;
}
}
private class SampleAdapter extends BaseAdapter implements Filterable {
private List<String> filtered = null;
private ItemFilter mFilter = new ItemFilter();
@Override
public int getCount() {
return mDataList.size();
}
@Override
public ListData getItem(int position) {
return mDataList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public Filter getFilter() {
return mFilter;
}
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
final List<ListData> list = mDataList;
int count = list.size();
final List<String> nlist = new ArrayList<>(count);
String FilterableString;
for (int i = 0; i < count; i++) {
FilterableString = list.get(i).data;
if (FilterableString.toLowerCase().contains(filterString)) {
mDataList.contains(FilterableString);
Log.d("mDataList", ""+mDataList.get(i).data);
nlist.add(FilterableString);
}
}
results.values = nlist;
results.count = nlist.size();
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filtered = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
int pos = 0;
String s = "";
if (convertView == null) {
convertView = View.inflate(ListActivity.this, R.layout.sample_list_item, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ListData item = getItem(position);
s = item.data.substring(0, 1);
holder.textView.setText(item.data);
for (pos = 0; pos > mDataList.size(); pos++) {
s = mDataList.get(pos).data.substring(0, 1);
}
convertView.findViewById(R.id.imageView);
return convertView;
}
}
private class ViewHolder {
private View view;
private ImageView imageView, checkIcon;
private TextView textView;
private ViewHolder(View view) {
this.view = view;
imageView = (ImageView) view.findViewById(R.id.imageView);
textView = (TextView) view.findViewById(R.id.textView);
checkIcon = (ImageView) view.findViewById(R.id.check_icon);
}
}
}
推荐答案
对象work
是您的ArrayAdapter,但不会将数据传递到SampleAdapter
上.示例代码(类似于我的代码)如下:
The object work
is your ArrayAdapter but it does not pass the data onto SampleAdapter
. Sample code (similar to mine) is like:
SampleAdapter work = null;
lvAdapter = new SampleAdapter(getActivity(), R.layout.list_item, mDataList);
lv.setAdapter(work);
基本上,您需要将数据从构造函数传递到Adapter子类上.另一个,SampleAdapter需要getView
方法的布局ID(XML).
Basically you need to pass the data onto Adapter subclass from the constructor. Another, SampleAdapter needs a layout ID (XML) for the getView
method.
玩得开心...
这篇关于Android ListView过滤器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!