フィルタリングにを使用しようとしていますが、フィルタに問題があります。フィルタリングされたものの代わりにArrayList
のすべてのアイテムを返します。以下は私のコードです:AutoCompleteTextViewテキストフィルタリング
Filter nameFilter = new Filter() {
@Override
public String convertResultToString(Object resultValue) {
String str = ((State)(resultValue)).getName();
Log.e("Conv"+TAG, str);
return str;
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
if(constraint != null) {
suggestions.clear();
for (State customer : itemsAll) {
Log.e("Ite "+TAG, "" + itemsAll.size());
Log.e("Cons " + TAG, constraint.toString());
Log.e("Sta" + TAG, customer.getName()); //This is always null
if(customer.getName().toLowerCase().contains(constraint.toString().toLowerCase())) {
suggestions.add(customer);
}
}
FilterResults filterResults = new FilterResults();
filterResults.values = suggestions;
filterResults.count = suggestions.size();
return filterResults;
} else {
return new FilterResults();
}
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
List<State> filteredList = (ArrayList<State>) results.values;
if(results != null && results.count > 0) {
clear();
Log.e("D "+TAG, ""+results.count);
for (State c : filteredList) {
Log.e("The "+TAG, c.getName() + " " + c.getId());
add(c);
notifyDataSetChanged();
}
} else {
Log.e(TAG, "Empty Filter");
notifyDataSetChanged();
}
}
};
そして私は周りのロギングを開始し、私はこの行Log.e("Cons " + TAG, constraint.toString());
と、この行Log.e("Ite "+TAG, "" + itemsAll.size());
は決してnullである一方、この行は常にLog.e("Sta" + TAG, customer.getName()); //This is always null
nullです気づきました。テキストが渡されない限り、Constraintをnullにすることはできません。しかし、itemsAll.size()
がnullでも空でもない場合、最初のオブジェクトはnullになります。私は混乱しています。以下、私のList
初期
private static final String TAG = "StateAdapter";
private ArrayList<State> items;
private ArrayList<State> itemsAll;
private ArrayList<State> suggestions;
private Context context;
private int viewResourceId;
public StateAutoCompleteAdapter(Context context, int resource, int textViewResourceId, ArrayList<State> objects) {
super(context, resource, textViewResourceId, objects);
this.context = context;
this.items = objects;
this.itemsAll = new ArrayList<State>(items);
this.suggestions = new ArrayList<State>();
this.viewResourceId = resource;
}
とgetView
機能について
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(viewResourceId, null);
}
State customer = items.get(position);
if (customer != null) {
TextView customerNameLabel = (TextView) v.findViewById(R.id.bakerName);
TextView bakerAddress = (TextView) v.findViewById(R.id.bakerAddress);
if (customerNameLabel != null) {
// Log.i(MY_DEBUG_TAG, "getView Customer Name:"+customer.getName());
customerNameLabel.setText(customer.getName());
bakerAddress.setVisibility(View.GONE);
}
}
return v;
}
を行きます。あなたはpublishResults()で何が得られるかは言及していませんでしたが、私はあなたが "空のフィルター"を取得していると思いますか?また、私はArrayAdapterを拡張しようとしています。カスタムアダプターは標準的なシナリオ用に設計されているため、カスタムフィルターを使用する予定はありません。 – Luksprog
あなたのデータはどこから取得しましたか? – pskink
@pskink私は 'Realmdb'からデータを取得しています。だから私はカウントを得ることができるのです –