Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Hide ListView from SearchView

Hello, I need to do app that have SearhView with ListView, but before looking for something from the searchView the User can see all the ListView and I need to hide this. This is my code:

import android.app.Activity; import android.os.Bundle; import android.widget.ListView; import android.widget.SearchView;

import java.util.ArrayList;

import static com.app4.android.application4.R.id.listView;

public class MainActivity extends Activity implements SearchView.OnQueryTextListener {

private SearchView mSearchView;
private ListView mListView;
private ArrayList<Cities> mEmployeeCities;
private CitiesAdapter CitiesAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mSearchView = (SearchView) findViewById(R.id.searchView);
    mListView = (ListView) findViewById(listView);



    mEmployeeCities = new ArrayList<Cities>();
    mEmployeeCities.add(new Cities("London, England"));
    mEmployeeCities.add(new Cities("Paris, France"));
    mEmployeeCities.add(new Cities("Roma, Italy"));
    mEmployeeCities.add(new Cities("Barcelona, Spain"));
    mEmployeeCities.add(new Cities("Madrid, Spain"));

    CitiesAdapter = new CitiesAdapter(MainActivity.this, mEmployeeCities);
    mListView.setAdapter(CitiesAdapter);
    mListView.setTextFilterEnabled(true);
    setupSearchView();

}

private void setupSearchView() {
    mSearchView.setIconifiedByDefault(false);
    mSearchView.setOnQueryTextListener(this);
    mSearchView.setSubmitButtonEnabled(false);
    mSearchView.setQueryHint("Search Here");

}

@Override
public boolean onQueryTextChange(String text) {
  CitiesAdapter.getFilter().filter(text);
    return false;
}

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

}

Thank you for Help :)