我有一个使用ArrayAdapter
的自定义适配器,但我不知道如何为所有值设置过滤器。这是我的自定义适配器代码
public class ListAdapter extends ArrayAdapter<String> {
String[] simNo, mobileNo, data;
Activity activity;
public ListAdapter(Activity activity, Object object[]) {
super(activity, R.layout.search_data, (String[]) object[0]);
this.simNo = (String[]) object[0];
this.mobileNo = (String[]) object[1];
this.data = (String[]) object[2];
this.activity = activity;
}
@Override
public View getView(final int position, View view, ViewGroup parent) {
String DELIMITER = " : ";
LayoutInflater inflater = activity.getLayoutInflater();
view = inflater.inflate(R.layout.search_data, null, true);
((TextView) view.findViewById(R.id.sim_no_text)).setText("Sim Number"+DELIMITER+simNo[position]);
((TextView) view.findViewById(R.id.mob_no_text)).setText("Mobile Number"+DELIMITER+mobileNo[position]);
((TextView) view.findViewById(R.id.data)).setText(data[position]);
return view;
}
}
正在加载数据:
public Object[] formListViewDetails(JSONObject result) {
int noOfSim;
noOfSim = result.optInt("no_of_sims");
String[] data, simNo, mobileNo;
simNo = new String[noOfSim];
mobileNo = new String[noOfSim];
data = new String[noOfSim];
try {
JSONArray totalData = result.getJSONArray("sim_details");
for (int i = 0; i < noOfSim; i++) {
simNo[i] = totalData.optJSONObject(i).optString("sim_no");
mobileNo[i] = totalData.optJSONObject(i).optString("mobile_no");
data[i] = totalData.optJSONObject(i).toString();
}
return new Object[]{simNo, mobileNo, data};
} catch (Exception e) {
Log.d(TAG, "formListViewDetails: " + e.toString());
return null;
}
}
调用数组适配器:
listAdapter = new ListAdapter(this, formListViewDetails(result));
listView.setAdapter(listAdapter);
答案 0 :(得分:1)
使用类过滤器 https://developer.android.com/reference/android/widget/Filter.html 例如在你的适配器实现接口Filterable
public class ListAdapter extends ArrayAdapter<String> implements
Filterable {
.
.
.
@Override
public Filter getFilter() {
if (mFilter == null)
mFilter = new LocalFilter();
return mFilter;
}
private class LocalFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
// We implement here the filter logic
if (constraint == null || constraint.length() == 0) {
// No filter implemented we return all the list
results.values = arrayString;
results.count = arrayString.size();
} else {
// We perform filtering operation
List<Local> nArrayList = new ArrayList<Local>();
for (String item : mStrings) {
if (item.toUpperCase()
.contains(constraint.toString().toUpperCase()))
nArrayList.add(local);
}
results.values = nArrayList;
results.count = nArrayList.size();
}
return results;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
// Now we have to inform the adapter about the new list filtered
mStrings = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
}
我希望它有所帮助