![](/img/trans.png)
[英]AutoCompleteTextview with Recyclerview with custom filtering
[英]AutoCompleteTextView Text Filtering
我正在嘗試使用AutoCompleteTextView
進行過濾,但過濾器出現問題。 它返回ArrayList
中的所有項目,而不是過濾后的項目。 下面是我的代碼:
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("Sta" + TAG, customer.getName()); //This is always null
Log.e("Sta" + TAG, customer.getName()); //This is always null
行Log.e("Cons " + TAG, constraint.toString());
Log.e("Sta" + TAG, customer.getName()); //This is always null
這行Log.e("Ite "+TAG, "" + itemsAll.size());
永遠不會為空。 除非沒有傳遞任何文本,否則約束不能肯定為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;
}
因此,我自己解決了此問題。 所以,我用一個正常的BaseAdapter
和延長Filterable
因此,對於那些可能需要如何使用RealmDB
與AutoCompleteTextView
你去那里
public class LgasAutoCompleteAdapter extends BaseAdapter implements Filterable {
private static final String TAG = "LgasAutoComp";
private Context mContext;
private List<Lga> mResult = new ArrayList<>();
private LayoutInflater inflater;
public LgasAutoCompleteAdapter(Context mContext) {
this.mContext = mContext;
}
@Override
public int getCount() {
return mResult.size();
}
@Override
public Object getItem(int position) {
return mResult.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (view == null)
view = inflater.inflate(R.layout.item_baker_autocomplete, parent, false);
Lga lga = mResult.get(position);
TextView customerNameLabel = (TextView) view.findViewById(R.id.bakerName);
TextView bakerAddress = (TextView) view.findViewById(R.id.bakerAddress);
if (lga != null) {
if (customerNameLabel != null) {
// Log.i(MY_DEBUG_TAG, "getView Customer Name:"+customer.getName());
customerNameLabel.setText(lga.getName());
bakerAddress.setVisibility(View.GONE);
}
}
return view;
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
return null;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults filterResults) {
if (constraint != null) {
//String query = constraint.toString().toLowerCase();
mResult = filterStates(constraint.toString());
Log.e(TAG, ""+mResult.size());
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
}
@NonNull
private List<Lga> filterStates(String query) {
Realm mRealm = RealmUtils.getRealmInstance(mContext);
return mRealm.where(Lga.class)
.contains("name", query)
.or()
.beginsWith("name", query)
.findAll();
}
}
clear()方法可能會引起問題。 因此,請檢查您要清洗的陣列列表的代碼。
另一個問題是因為您使用的是State customer = items.get(position);
在getView()
並使用getView()
suggestions.add(customer);
在performFiltering(CharSequence constraint)
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.