2013-04-07 54 views
2

在我的Android應用程序中,我有一個包含大量項目的多選項目對話框(類似於圖中所示)。所以我打算添加一個搜索框和用戶類型,這些項目都會相應地進行過濾。做這個的最好方式是什麼?由於帶搜索功能的Android Multichoice項目對話框

Image

回答

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,因爲在此解決方案中,視圖創建是硬編碼的。