フィルタ可能な(カスタム)リストビューを実装しようとすると問題が発生します。デバッグ時に、フィルタリングは実際には動作するようですが、結果は表示されません(listviewは何もフィルタリングしていないようです)。私が検索するすべてのキーワード:すべてのアイテムが表示され続けます。私の断片でFiltering ArrayAdapter Listviewがリフレッシュされない(Android)
コード
ListView yourListView;
List<PokemonListItem> pokemonList;
PokemonListAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_pokemon_list_overview,
container, false);
pDialog = new ProgressDialog(getContext());
pDialog.setMessage("Refreshing pokemon list...");
pDialog.setCancelable(false);
TAG = PokemonViewer.class.getSimpleName();
yourListView = (ListView) view.findViewById(R.id.urlListView);
refreshPokemonListItems();
EditText inputSearch = (EditText) view.findViewById(R.id.searchInput);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {}
@Override
public void afterTextChanged(Editable arg0) {}
});
adapter = new PokemonListAdapter(getContext(), R.id.searchInput, pokemonList);
yourListView.setAdapter(adapter);
return view;
}
マイDetailListAdapter
public class PokemonListAdapter extends ArrayAdapter<PokemonListItem> implements Filterable{
List<PokemonListItem> pokemonList;
public PokemonListAdapter(Context context, int resource, List<PokemonListItem> items) {
super(context, resource, items);
this.pokemonList = items;
}
Filter myFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
ArrayList<PokemonListItem> tempList=new ArrayList<>();
//constraint is the result from text you want to filter against.
//objects is your data set you will filter from
Log.v("Logger", "The constraint is now: " + constraint.toString());
if(constraint != null && pokemonList!=null) {
int length=pokemonList.size();
int i=0;
while(i<length){
PokemonListItem item=pokemonList.get(i);
//do whatever you wanna do here
//adding result set output array
boolean containsString = item.getName().toLowerCase().contains(constraint.toString().toLowerCase());
if(containsString){
tempList.add(item);
//Log.v("Logger", "Constraint: " + constraint.toString().toLowerCase() + " value: " + item.getName() + " bool: " + containsString);
}
i++;
}
//following two lines is very important
//as publish result can only take FilterResults objects
filterResults.values = tempList;
filterResults.count = tempList.size();
}
return filterResults;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence contraint, FilterResults results) {
pokemonList = (List<PokemonListItem>) results.values;
Log.v("Logger", "This is called");
if (results.count > 0) {
Log.v("Logger", "true is called");
Log.v("Logger", results.values.toString());
clear();
for (PokemonListItem item : pokemonList) {
add(item);
}
notifyDataSetChanged();
} else {
Log.v("Logger", "false is called");
notifyDataSetInvalidated();
}
}
};
@Override
public Filter getFilter() {
return myFilter;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.url_list_row, null);
}
PokemonListItem p = getItem(position);
if (p != null) {
TextView name = (TextView) v.findViewById(R.id.name);
if (name != null) {
name.setText(p.getName());
}
}
return v;
}
}
PokemonListItem
public class PokemonListItem {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
//This only works with pokemon names > 2 chars
if(name.length() > 1){
char first = Character.toUpperCase(name.charAt(0));
this.name = first + name.substring(1);
}else{
this.name = name;
}
}
@Override
public String toString(){
return id;
}