在我的 Android 应用程序中,我有一个包含大量项目的多选项目对话框(类似于图像中显示的)。所以我计划添加一个搜索框,当用户输入时,项目会被相应地过滤。做这个的最好方式是什么?谢谢
问问题
1940 次
1 回答
0
我这样做了,对我来说很好。
private static final class ListItemWithIndex {
public final int index;
public final String value;
public ListItemWithIndex(final int index, final String value) {
super();
this.index = index;
this.value = value;
}
@Override
public String toString() {
return value;
}
}
public static void showMultiChoiceDialogWithSearchFilterUI(final Activity _activity, final Object[] _optionsList,
final int _titleResId, final OnClickListener _itemSelectionListener) {
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(_activity);
final List<ListItemWithIndex> allItems = new ArrayList<ListItemWithIndex>();
final List<ListItemWithIndex> filteredItems = new ArrayList<ListItemWithIndex>();
for (int i = 0; i < _optionsList.length; i++) {
final Object obj = _optionsList[i];
final ListItemWithIndex listItemWithIndex = new ListItemWithIndex(i, obj.toString());
allItems.add(listItemWithIndex);
filteredItems.add(listItemWithIndex);
}
dialogBuilder.setTitle(_titleResId);
final ArrayAdapter<ListItemWithIndex> objectsAdapter = new ArrayAdapter<ListItemWithIndex>(_activity,
android.R.layout.simple_list_item_1, filteredItems) {
@Override
public Filter getFilter() {
return new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(final CharSequence constraint, final FilterResults results) {
filteredItems.clear();
filteredItems.addAll((List<ListItemWithIndex>) results.values);
notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(final CharSequence constraint) {
final FilterResults results = new FilterResults();
final String filterString = constraint.toString();
final ArrayList<ListItemWithIndex> list = new ArrayList<ListItemWithIndex>();
for (final ListItemWithIndex obj : allItems) {
final String objStr = obj.toString();
if ("".equals(filterString)
|| objStr.toLowerCase(Locale.getDefault()).contains(
filterString.toLowerCase(Locale.getDefault()))) {
list.add(obj);
}
}
results.values = list;
results.count = list.size();
return results;
}
};
}
};
final EditText searchEditText = new EditText(_activity);
searchEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(final CharSequence arg0, final int arg1, final int arg2, final int arg3) {
}
@Override
public void beforeTextChanged(final CharSequence arg0, final int arg1, final int arg2, final int arg3) {
}
@Override
public void afterTextChanged(final Editable arg0) {
objectsAdapter.getFilter().filter(searchEditText.getText());
}
});
final ListView listView = new ListView(_activity);
listView.setAdapter(objectsAdapter);
final LinearLayout linearLayout = new LinearLayout(_activity);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.addView(searchEditText, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 0);
layoutParams.weight = 1;
linearLayout.addView(listView, layoutParams);
dialogBuilder.setView(linearLayout);
final AlertDialog dialog = dialogBuilder.create();
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(final AdapterView<?> parent, final View view, final int position, final long id) {
dialog.dismiss();
_itemSelectionListener.onClick(null, filteredItems.get(position).index);
}
});
dialog.show();
}
只需使用“showMultiChoiceDialogWithSearchFilterUI”功能。不过,您可能希望从 XML 扩充 edittext 和 listview,因为在此解决方案中,视图创建是硬编码的。
于 2014-04-13T19:41:57.347 回答