1

Пытаюсь сделать поиск в ListView. Мой адаптер:

public class MyAdapter extends ArrayAdapter{

public static ArrayList<Item> data = new ArrayList<Item>();
Context context;
public MyAdapter(Context context, int textViewResourceId, ArrayList<Item> arr) {
super(context, textViewResourceId, arr);
if (arr != null) {
data = arr;
}
this.context = context;   
// TODO Auto-generated constructor stub
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int num) {
// TODO Auto-generated method stub
return data.get(num);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int i, View someView, ViewGroup arg2) {
LayoutInflater inflater = LayoutInflater.from(context);

if (someView == null) {
someView = inflater.inflate(R.layout.rect, arg2, false);
}

final TextView header1 = (TextView) someView.findViewById(R.id.Text);
header1.setText(data.get(i).text1);
return someView;
}
}

Активити:

pullToRefreshView = (ListView) findViewById(R.id.lvMain);
final MyAdapter lol = new MyAdapter(this,R.id.lvMain ,data);
pullToRefreshView.setAdapter(lol);

data.add(new Item("privet"));
data.add(new Item("kak dela"));
data.add(new Item("dela narm"));
data.add(new Item("ohuet"));
data.add(new Item("nastya"));
data.add(new Item("prove"));
EditText txt = (EditText)findViewById(R.id.OMG);
txt.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
lol.getFilter().filter(arg0);
lol.notifyDataSetChanged();
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});

Но при изменении EditText'a ничего не происходит. Что я делаю не так?

1 ответ 1

1

Надо писать свою реализацию фильтра или переопределить метод compare() в Item классе.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.