actionbarsherlock - Android: Enable suggestions programmatically on a SearchView -
hello i'm using example provided actionbarsherlock 4.3.1 demo. want set search view in order show suggestions when there 0 characters inserted , let suggestions disappear when user write something. how can this? thanks
package com.actionbarsherlock.sample.demos; import android.app.searchmanager; import android.content.context; import android.database.cursor; import android.database.matrixcursor; import android.os.bundle; import android.provider.basecolumns; import android.support.v4.widget.cursoradapter; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.textview; import android.widget.toast; import com.actionbarsherlock.app.sherlockactivity; import com.actionbarsherlock.view.menu; import com.actionbarsherlock.view.menuitem; import com.actionbarsherlock.widget.searchview; public class searchviews extends sherlockactivity implements searchview.onquerytextlistener, searchview.onsuggestionlistener { private static final string[] columns = { basecolumns._id, searchmanager.suggest_column_text_1, }; private suggestionsadapter msuggestionsadapter; @override public boolean oncreateoptionsmenu(menu menu) { //used put dark icons on light action bar boolean islight = samplelist.theme == r.style.theme_sherlock_light; //create search view searchview searchview = new searchview(getsupportactionbar().getthemedcontext()); searchview.setqueryhint("search countries…"); searchview.setonquerytextlistener(this); searchview.setonsuggestionlistener(this); if (msuggestionsadapter == null) { matrixcursor cursor = new matrixcursor(columns); cursor.addrow(new string[]{"1", "'murica"}); cursor.addrow(new string[]{"2", "canada"}); cursor.addrow(new string[]{"3", "denmark"}); msuggestionsadapter = new suggestionsadapter(getsupportactionbar().getthemedcontext(), cursor); } searchview.setsuggestionsadapter(msuggestionsadapter); menu.add("search") .seticon(islight ? r.drawable.ic_search_inverse : r.drawable.abs__ic_search) .setactionview(searchview) .setshowasaction(menuitem.show_as_action_if_room | menuitem.show_as_action_collapse_action_view); return true; } @override protected void oncreate(bundle savedinstancestate) { settheme(samplelist.theme); //used theme switching in samples super.oncreate(savedinstancestate); setcontentview(r.layout.text); ((textview)findviewbyid(r.id.text)).settext(r.string.search_views_content); } @override public boolean onquerytextsubmit(string query) { toast.maketext(this, "you searched for: " + query, toast.length_long).show(); return true; } @override public boolean onquerytextchange(string newtext) { return false; } @override public boolean onsuggestionselect(int position) { return false; } @override public boolean onsuggestionclick(int position) { cursor c = (cursor) msuggestionsadapter.getitem(position); string query = c.getstring(c.getcolumnindex(searchmanager.suggest_column_text_1)); toast.maketext(this, "suggestion clicked: " + query, toast.length_long).show(); return true; } private class suggestionsadapter extends cursoradapter { public suggestionsadapter(context context, cursor c) { super(context, c, 0); } @override public view newview(context context, cursor cursor, viewgroup parent) { layoutinflater inflater = layoutinflater.from(context); view v = inflater.inflate(android.r.layout.simple_list_item_1, parent, false); return v; } @override public void bindview(view view, context context, cursor cursor) { textview tv = (textview) view; final int textindex = cursor.getcolumnindex(searchmanager.suggest_column_text_1); tv.settext(cursor.getstring(textindex)); } } }
Comments
Post a Comment