Reputation: 81
suppose i have listview content like this.
------------------------------------------------------
TextView TextView
--------------------------------------------------
TextView TextView
----------------------------------------------------
TextView TextView
-------------------------------------------------------
TextView TextView
----------------------------------------------------
so is it possible to search my search content on each component of listview using searchview in android.
for example if i type in searchview "bob". then it will check bob in all 4 textview and give a updated result to you....
Upvotes: 3
Views: 5421
Reputation: 2758
Yes it's possible.
You need to do something like this:
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;
}
}
Upvotes: 1
Reputation: 5201
Yes, it's possible, and it can be achieved using SearchView. Your Adapter should Implement Filterable and it's getFilter() method will have your filtering criteria.
I am using SearchView in ActionBar for showing this functionality.
My Activity -
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;
}
}
My Adapter -
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;
}
}
For convenience, my model class MyMessage -
public class MyMessage implements Parcelable{
private String messageKey = "";
private String key = "";
private String key2 = "";
//getters and setters
}
Upvotes: 6