2011-09-28 75 views
3

假设我有这样的listview内容。searchview如何在android中为listView工作?

------------------------------------------------------ 
TextView    TextView 
-------------------------------------------------- 
TextView    TextView 
---------------------------------------------------- 
TextView    TextView 
------------------------------------------------------- 
TextView    TextView 
---------------------------------------------------- 

所以有可能在android中使用searchview在listview的每个组件上搜索我的搜索内容。

例如,如果我输入searchview“bob”。那么它会检查鲍勃在所有4文本视图,并给你一个更新的结果....

回答

1

是的,这是可能的。

你需要做这样的事情:

public class SearchViewFilterMode extends Activity implements SearchView.OnQueryTextListener { 

    private static final String TAG = "SearchViewFilterMode"; 

    private SearchView mSearchView; 
    private ListView mListView; 
    private ArrayAdapter<String> mAdapter; 

    private final String[] mStrings = Cheeses.sCheeseStrings; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     getWindow().requestFeature(Window.FEATURE_ACTION_BAR); 

     setContentView(R.layout.searchview_filter); 

     mSearchView = (SearchView) findViewById(R.id.search_view); 
     mListView = (ListView) findViewById(R.id.list_view); 
     mListView.setAdapter(mAdapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, 
       mStrings)); 
     mListView.setTextFilterEnabled(true); 
     setupSearchView(); 
    } 

    @TargetApi(11) 
    private void setupSearchView() { 
     mSearchView.setIconifiedByDefault(false); 
     mSearchView.setOnQueryTextListener(this); 
     mSearchView.setSubmitButtonEnabled(false); 
     mSearchView.setQueryHint(getString(R.string.cheese_hunt_hint)); 
    } 

    public boolean onQueryTextChange(String newText) { 
     if (TextUtils.isEmpty(newText)) { 
      mListView.clearTextFilter(); 
     } else { 
      mListView.setFilterText(newText.toString()); 
     } 
     return true; 
    } 

    public boolean onQueryTextSubmit(String query) { 
     return false; 
    } 
} 
6

是的,这是可能的,并且它可以使用搜索查看得以实现。您的适配器应该实现可筛选,并且它的getFilter()方法将具有您的筛选条件。

我在ActionBar中使用SearchView来显示此功能。

我的活动 -

public class MyActivity extends SherlockListActivity implements OnQueryTextListener { 

    private MyMessage myMessage; // list item, which is a model class in my case 
    private ListView mListView; 
    private MessageAdapter mAdapter; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

     setContentView(R.layout.layout_containing_listView); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     //Create the search view 
     SearchView searchView = new SearchView(getSupportActionBar().getThemedContext()); 

     mListView = getListView(); 
     mListView.setTextFilterEnabled(true); 

     setupSearchView(searchView); 

     menu.add(0, 1, 1, null) 
      .setIcon(R.drawable.ic_search) 
      .setActionView(searchView) 
      .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW); 

     return super.onCreateOptionsMenu(menu); 
    } 

    private void setupSearchView(SearchView mSearchView) { 
     mSearchView.setIconifiedByDefault(false); 
     mSearchView.setSubmitButtonEnabled(false); 
     mSearchView.setOnQueryTextListener(this); 
     mSearchView.setQueryHint("Search Sender"); 
    } 

    @Override 
    public boolean onQueryTextChange(String newText) { 
     if (TextUtils.isEmpty(newText)) { 
      mListView.clearTextFilter(); 
     } else { 
      mListView.setFilterText(newText); 
     } 
     return true; 
    } 

    @Override 
    public boolean onQueryTextSubmit(String query) { 
     return false; 
    } 
} 

我的适配器 -

public class MessageAdapter extends BaseAdapter implements Filterable { 

    List<MyMessage> msgsList; 
    List<MyMessage> mOriginalValues; 
    List<String> mListItem; // any key of MyMessage Model Class 
    MyMessage message; 

    public MessageAdapter(List<MyMessage> msgList) { 
     super(); 
     this.msgsList = msgList; 
    } 

    …. 
    …. 
    … 

    @Override 
    public Filter getFilter() { 
     /** 
     * A filter object which will 
     * filter message key 
     * */ 
     Filter filter = new Filter() { 

      @SuppressWarnings("unchecked") 
      @Override 
      protected void publishResults(CharSequence constraint,FilterResults results) { 

       msgsList = (List<MyMessage>) results.values; // has the filtered values 
       notifyDataSetChanged(); // notifies the data with new filtered values. Only filtered values will be shown on the list 
      } 

      @Override 
      protected FilterResults performFiltering(CharSequence constraint) { 
       FilterResults results = new FilterResults();  // Holds the results of a filtering operation for publishing 

       List<MyMessage> FilteredArrList = new ArrayList<MyMessage>(); 

       if (mOriginalValues == null) { 
        mOriginalValues = new ArrayList<MyMessage>(msgsList); // saves the original data in mOriginalValues 
       } 

       if(mListItem == null){ 
        mListItem = new ArrayList<String>(); 
        for(MyMessage message : mOriginalValues){ 
         mListItem.add(message.getMessage()); 
        } 
       } 

       /** 
       * 
       * If constraint(CharSequence that is received) is null returns the mOriginalValues(Original) values 
       * else does the Filtering and returns FilteredArrList(Filtered) 
       * 
       **/ 

       if (constraint == null || constraint.length() == 0) { 

        /* CONTRACT FOR IMPLEMENTING FILTER : set the Original values to result which will be returned for publishing */ 
        results.count = mOriginalValues.size(); 
        results.values = mOriginalValues; 
       } else { 
        /* Do the filtering */ 
        constraint = constraint.toString().toLowerCase(); 

        for (int i = 0; i < mListItem.size(); i++) { 
         String data = mListItem.get(i); 
         if (data.toLowerCase().startsWith(constraint.toString())) { 
          FilteredArrList.add(mOriginalValues.get(i)); 
         } 
        }  

        // set the Filtered result to return 
        results.count = FilteredArrList.size(); 
        results.values = FilteredArrList; 
       } 
       return results; 
      } 
     }; 
     return filter; 
    } 
} 

为了方便起见,我的模型类MyMessage -

public class MyMessage implements Parcelable{ 

    private String messageKey = ""; 
    private String key = ""; 
    private String key2 = ""; 

    //getters and setters 
} 
+0

感谢的人是工作的伟大 –