2017-04-04 49 views
-4

我實現列表視圖搜索,現在我想在列表視圖如何在動作條中添加搜索查看,並從列表視圖

這裏實現搜索功能是我的代碼

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

@Override 
public boolean onQueryTextChange(String newText) { 
    return true; 
} 

如何能夠做到在搜索列表-view.i我沒有得到如何實現其他功能也經過搜索和更新列表

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    // Inflate menu to add items to action bar if it is present. 
    inflater.inflate(R.menu.menu_product_detail, menu); 
    // Associate searchable configuration with the SearchView 
    SearchManager searchManager = 
      (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
    SearchView searchView = 
      (SearchView) menu.findItem(R.id.menu_search).getActionView(); 
    searchView.setSearchableInfo(
      searchManager.getSearchableInfo(getComponentName())); 
    searchView.getQuery(); 
    Log.v("Abhijit", "" + searchManager.QUERY.toString()); 
    searchView.setOnQueryTextListener(this); 
    return true; 
} 

如何能夠做到在列表view.i搜索我沒有得到如何實現其他功能先進而精湛也是經過搜索和更新列表

+0

http://www.androidhive.info/2012/09/android-adding-search-functionality這個鏈接可能對您有所幫助 –

+0

我是新來的機器人可以請您告訴我在我的代碼中的解決方案請 –

回答

0

使用此代碼:

<item 
    android:id="@+id/action_search" 
    android:icon="@android:drawable/ic_menu_search" 
    app:showAsAction="always" 
    app:actionViewClass="android.support.v7.widget.SearchView" 
    android:title="Search"/> 

活動文件:

public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater menuInflater = getMenuInflater(); 
    menuInflater.inflate(R.menu.dashboard, menu); 

    MenuItem searchItem = menu.findItem(R.id.action_search); 

    SearchManager searchManager = (SearchManager) MainActivity.this.getSystemService(Context.SEARCH_SERVICE); 

    SearchView searchView = null; 
    if (searchItem != null) { 
     searchView = (SearchView) searchItem.getActionView(); 
    } 
    if (searchView != null) { 
     searchView.setSearchableInfo(searchManager.getSearchableInfo(MainActivity.this.getComponentName())); 
    } 
     return super.onCreateOptionsMenu(menu); 
} 

清單文件:

<meta-data 
     android:name="android.app.default_searchable" 
     android:value="com.apkgetter.SearchResultsActivity" /> 

     <activity 
      android:name="com.apkgetter.SearchResultsActivity" 
      android:label="@string/app_name" 
      android:launchMode="singleTop" > 
      <intent-filter> 
       <action android:name="android.intent.action.SEARCH" /> 
      </intent-filter> 
      <intent-filter> 
       <action android:name="android.intent.action.VIEW" /> 
      </intent-filter> 

      <meta-data 
       android:name="android.app.searchable" 
       android:resource="@xml/searchable" /> 
     </activity> 

搜索XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<searchable xmlns:android="http://schemas.android.com/apk/res/android" 
    android:hint="@string/search_hint" 
    android:label="@string/app_name" /> 

實現可過濾的listview適配器根據搜索到的數據過濾列表。

+0

我想添加過濾器功能 –

0

這是您在適配器如何篩選:

public class FavouritesAdapter extends RecyclerView.Adapter<FavouritesAdapter.MyViewHolder> implements Filterable { 

    private ItemFilter mFilter = new ItemFilter(); 
    private ArrayList<ProductDetail> listItems, filterList; 
    private Context context; 

    public class MyViewHolder extends RecyclerView.ViewHolder { 

     public TextView textView; 

     public MyViewHolder(View view) { 
      super(view); 
      textView  = (TextView) view.findViewById(R.id.textView); 
     } 
    } 

    public FavouritesAdapter(Context context, List<ProductDetail> listItems) { 
     this.context = context; 
     this.listItems = listItems; 
     this.filterList = new ArrayList<ProductDetail>(); 
     // we copy the original list to the filter list and use it for setting row values 
     this.filterList.addAll(this.listItems); 
    } 

    @Override 
    public FavouritesAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_favourites, parent, false); 

     return new FavouritesAdapter.MyViewHolder(itemView); 
    } 

    @Override 
    public void onBindViewHolder(FavouritesAdapter.MyViewHolder holder, int position) { 
     holder.textView.setText(filterList.get(position)); 
    } 

    // Clean all elements of the recycler 
    public void clear() { 
     filterList.clear(); 
     notifyDataSetChanged(); 
    } 

    // Add a list of items 
    public void addAll(List<ProductDetail> list) { 
     filterList.addAll(list); 
     notifyDataSetChanged(); 
    } 

    @Override 
    public int getItemCount() { 
     return (null != filterList ? filterList.size() : 0); 
    } 

    @Override 
    public Filter getFilter() { 
     return mFilter; 
    } 

    private class ItemFilter extends Filter { 

     @Override 
     protected FilterResults performFiltering(CharSequence constraint) { 

      final Calendar calendar = Calendar.getInstance(); 

      String filterString = constraint.toString().toLowerCase(); 

      FilterResults results = new FilterResults(); 

      final List<ProductDetail> list = listItems; 

      int count = list.size(); 
      final ArrayList<ProductDetail> nlist = new ArrayList<ProductDetail>(count); 

      String filterableString ; 

      for (int i = 0; i < count; i++) { 
       filterableString = list.get(i).getYourObject; // get Your Object against whom you want to compare the list 
       if (filterableString.toLowerCase().contains(filterString)) { 
        nlist.add(listItems.get(i)); 
       } 
      } 

      results.values = nlist; 
      results.count = nlist.size(); 

      return results; 
     } 

     @SuppressWarnings("unchecked") 
     @Override 
     protected void publishResults(CharSequence constraint, Filter.FilterResults results) { 
      filterList = (ArrayList<ProductDetail>) results.values; 
      notifyDataSetChanged(); 
     } 

    } 

} 

在你的活動:

adapter.getFilter().filter(searchedEditText.getText().toString()); 
+0

我使用ArrayList

+0

String filterableString; (int i = 0; i

相關問題