有人可以帮助我使用自定义适配器过滤列表我现在已经停留了一段时间了。我已经跟踪了logcat,它确实输出了过滤结果我的问题是使用过滤结果更新列表视图。
// 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);
}
}
}
答案 0 :(得分:0)
对象work
是您的ArrayAdapter,但它不会将数据传递到SampleAdapter
。示例代码(类似于我的)类似于:
SampleAdapter work = null;
lvAdapter = new SampleAdapter(getActivity(), R.layout.list_item, mDataList);
lv.setAdapter(work);
基本上,您需要将数据从构造函数传递到Adapter子类。另一个,SampleAdapter需要getView
方法的布局ID(XML)。
玩得开心......