Thursday, 15 April 2010

android - How to implement search widget with a listview using SherlockActionbar? -


So, I'm trying to implement this code on my application, but no success yet.

This is my main activity. Java:

  import java.util.List; Android Import Search manager; Import android.content.Context; Importroid.os.Bundle; Import android.widget.ArrayAdapter; Import android.widget.SearchView; Import com.actionbarsherlock.app.ActionBar; Import com.actionbarsherlock.app.SherlockListActivity; Import com.actionbarsherlock.view.Menu; Import com.actionbarsherlock.view.MenuItem; Public Class Main Activity Expands Sherlock Holistic Activism Options / Public Sector Main Activity List Extension Action ActionBar.TabListener {Private List & lt; Scos & gt; Scoss; ScosDataSource Datasource; @ Override protected void saved create (bundled Instansstet) {Suprknkret (saved Instensstet); SetContentView (R.layout.activity_main); Datasource = New ScosDataSource (this); Datasource.open (); Scaus = datasource.findal (); If (scoss.size () == 0) {createData (); Scaus = datasource.findal (); } RefreshPrint (); } @ Override Public Boolean On Crate Option Menu (Menu Menu) {// Enhancing Menu; It adds the item to the Action Bar if it exists GetSupportMenuInflater (). Flood (R.menu.options, menu); SearchManager searchManager = (SearchManager) getSystemService (Event.SEARCH_SERVICE); SearchView searchView = (SearchView) menu.findItem (R.id.menu_search) .getActionView (); If (null! = SearchView) {searchView.setSearchableInfo (searchManager .getSearchableInfo (getComponentName ())); SearchView.setIconifiedByDefault (wrong); } SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener () {public boolean onQueryTextChange (string newText) {// This will be adapters filter that your adapter. (Filter) (newText); Back true; } Public Boolean onQueryTextSubmit (string query) {// This is your adapter that will be adapted by the adapter.fifter. Filter (query); Back true; }}; SearchView.setOnQueryTextListener (queryTextListener); Return Super. Secret Option Menu (menu); // return true; } @ Override Public Boolean On Option It's Select (Menu Item) {Switch (ITAT. ITMID)} {Case Android. R.DHOM: Return (True); } Return (Super choice option item selected (item)); } Public Zero Fresh Performance () {Array Adapter & lt; Scos & gt; Adapter = new scolist adapter (this, scoss); SetListAdapter (adapter); } @ Override Resume at zero () {super.onResume (); Datasource.open (); } @ Override Protected Zero at Pause () {Super. Datasource.close (); } Private Zero DataData () {}}   

This is my option. Xml:

  & lt; Menu xmlns: android = "http: // schemas Android.com/apk/res/android" & gt; & Lt; Item android: id = "@ + id / menu_search" Android: actionViewClass = "com.actionbarsherlock.widget.SearchView" Android: showAsAction = "ifRoom | collapseActionView" android: icon = "@ drawable / ic_action_search" android: title = "@ Search "& gt; & Lt; / Item & gt; & Lt; / Menu & gt;   

">

I hope that helps because I had only one problem. < p> the best way to use adapters that class is applied to the SearchView.OnQueryTextListener and then you do not have to make this internal class and you'll have the adapter.

in your code it will:

  public class Menaktiviti Sherloklist Aktiwwaiti Oplements extended takes SearchView.OnQueryTextListener   

And then in the class you have to define the methods the adapter adapter will be in your class array adapter adapter. But you should define it in the class as private

  public Boolean onQueryTextChange (string newText) {// This filter will be filtered (newText) to your adapter.getFilter (). Back to true;} Public boolean onQueryTextSubmit (string query) {// This is your adapter which is the adapter. Effil Filter will be filtered (query); return true;}   

where the lines are set:

  searchView.setOnQueryTextListener (queryTextListener);   

should be:

  searchView.setOnQueryTextListener (this);   

If you have any problem tell me, today I am with a similar problem and read the answers without your question.

No comments:

Post a Comment