Android ListView Фильтр не работает
Может ли кто-нибудь помочь мне отфильтровать список с помощью специального адаптера, на котором я застрял на какое-то время?
// 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);
}
}
}
1 ответ
Предмет work
ваш ArrayAdapter, но он не передает данные на SampleAdapter
, Пример кода (похож на мой) выглядит так:
SampleAdapter work = null;
lvAdapter = new SampleAdapter(getActivity(), R.layout.list_item, mDataList);
lv.setAdapter(work);
По сути, вам нужно передать данные на подкласс Adapter из конструктора. Во-вторых, SampleAdapter требуется идентификатор макета (XML) для getView
метод.
Повеселись...