gpt4 book ai didi

java - 在android中的自定义数组适配器中获取过滤器选项?

转载 作者:行者123 更新时间:2023-12-01 08:52:23 24 4
gpt4 key购买 nike

我有一个使用 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);

最佳答案

使用类过滤器 https://developer.android.com/reference/android/widget/Filter.html例如在您的适配器中实现 Filterable 接口(interface)

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();
}
}

希望对你有帮助

关于java - 在android中的自定义数组适配器中获取过滤器选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42305180/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com