Android Filter data in listivew from ActionBar using SearchView

961 Views Asked by At

I'm trying to filter the data on the listview using the searchview widget but is not wroking, i can write any query and it does not filter anything.

This is the code i'm using:

public class ConectadosActivity extends ActionBarActivity implements
        OnQueryTextListener {

ListView listview;
    ListConectadosAdapter adapter;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lista_conectados);

        actionBar = getSupportActionBar();
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME
                | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        cd = new ConnectionDetector(getApplicationContext());
        session = new SessionManager(getApplicationContext());
        session.checkLogin();
        listview = (ListView) findViewById(R.id.listado_conectados);
        listview.setTextFilterEnabled(true);

        HashMap<String, String> usuario = session.getUserDetails();

        usuarioID = usuario.get(SessionManager.KEY_USER_ID);

        if (!cd.isConnectingToInternet()) {

            MostrarAlert(ConectadosActivity.this,
                    "No se ha podido cargar la lista de contactos",
                    "Reintentar", "Salir", true, 1);
        }
        new Conectados().execute();
    }

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_contactos, menu);
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        MenuItem searchItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) MenuItemCompat
                .getActionView(searchItem);

        searchView.setSearchableInfo(searchManager
                .getSearchableInfo(getComponentName()));
        //searchView.setSubmitButtonEnabled(true);
        searchView.setOnQueryTextListener(this);
        searchView.setQueryHint("Buscar nombre de contacto");

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        // this is your adapter that will be filtered
        if (TextUtils.isEmpty(newText)) {
            listview.clearTextFilter();
        } else {
            listview.setFilterText(newText.toString());
        }

        return true;
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        // TODO Auto-generated method stub
        return false;
    }

But as i said is not doing nothing.

Thanks in advance.

0

There are 0 best solutions below