Я создал один просмотр списка и массив теперь, когда я фильтрую arrayadpter, чтобы его фильтрация была идеальной. Но список не обновляется: (он показывает только предыдущие данные).
Мое кодирование выглядит следующим образом:
TextView, используемый внутри ListView: простое текстовое представление для отображения каждой строки списка
ListView: здесь есть текстовое поле для панели поиска и представление списка, в котором я использую выше textview.
onCreate method of Activity Здесь seachCountry - это текстовое поле выше xml для поиска (фильтрации) стран.
countryList = (ListView) findViewById(R.id.countryList);
searchCountry = (EditText) findViewById(R.id.search);
createProfile = (Button) findViewById(R.id.createProfile);
countries = getResources().getStringArray(R.array.countries_array);
//countryArray = new ArrayAdapter<String>(this,R.layout.country_list_row , countries);
countryArray = new CountryArrayAdapter<String>(this,R.layout.country_list_row , countries);
countryList.setAdapter(countryArray);
countryList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
countryFrom = ((TextView) view).getText().toString();
}
});
searchCountry.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
SelectCountryActivity.this.countryArray.getFilter().filter(s);
countryArray.notifyDataSetChanged();
}
});
createProfile.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(SelectCountryActivity.this,CreateProfileActivity.class);
i.putExtra("countryFrom", countryFrom);
startActivity(i);
finish();
}
});
Адаптер ArrayList
public class CountryArrayAdapter extends ArrayAdapter{ public String[] allItems; public String[] subItems; private CountryFilter filter;
public CountryArrayAdapter (контекстный контекст, int textViewResourceId, String [] countryList) { супер (контекст, textViewResourceId, countryList); //TODO Автоматически созданный контур конструктора this.subItems = countryList; this.allItems = this.subItems; } @Override public Фильтр getFilter() { if (filter == null) { filter = new CountryFilter(); } обратный фильтр; } @Override public int getCount() { return subItems.length; } @Override public String getItem (int position) { return subItems [позиция]; } частный класс CountryFilter extends Filter { @Override protected FilterResults выполнитьFiltering (ограничение CharSequence) { FilterResults results = new FilterResults(); ArrayList <String> я = новый ArrayList <String>(); String [] содержимое; if (constraint! = null && constraint.toString(). length()> 0) { for (int index = 0; index <allItems.length; index++) { Строка si = allItems [index]; если (si.startsWith(constraint.toString())) { i.add(Si); } } contents = new String [i.size()]; i.toArray (содержание); results.values = содержимое; results.count = contents.length; } еще { synchronized (allItems) { results.values = allItems; results.count = allItems.length; } } результаты возврата; } @Override protected void publishResults (ограничение CharSequence, FilterResults) { //TODO Автоматически сгенерированный метод заглушки subItems = (String []) results.values; notifyDataSetChanged(); } }
Я узнаю из некоторых ссылок, что надуватель будет решать мою проблему, но не в состоянии понять, как использовать надув.
Пожалуйста, помогите мне решить проблему обновления части обзора.
Заранее большое спасибо.
с наилучшими пожеланиями, Ишан
автоматически установите объект адаптера в списке после завершения фильтра.