Stillie
Stillie

Reputation: 2676

how to find out if ArrayAdapter for a ListView is empty after being filtered

Is there a way to check if a filtered listview is empty or not?

I would like a TextView to say no results found if its empty?

I have a list of say 15 items that i want to filter based on a search, and lets say after the filter i am left with 5, i would like to return 5. If the results are empty then return 0. Everytime i call adapter.getCount() it always returns 15 even after its filtered

EDIT: This is the code i have tried just for testing but it always goes into the Else Statement.

Code Example:

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

    Intent intent = getIntent();
    String searchPhrase = intent.getExtras().getString("search");
    Log.i(TAG, "Search phrase is" + searchPhrase);
    mListView = (ListView) findViewById(R.id.searchResultsListView);

    String[] listOfText = {"Row 1", "Row 2", "Row 3", "Row 4", "Row 5", "Row 6", "Row 7", "Row 8", "Row 9", "Row 10", "Row 11", "Row 12", "Row 13", "Row 14", "Row 15"};

    ArrayList<String> rowList = new ArrayList<>();
    rowList.addAll(Arrays.asList(listOfText));

    mArrayAdapter = new ArrayAdapter<>(this, R.layout.search_results_row_list, rowList);

    String searchQuery = searchPhrase.trim();
    if (searchQuery.length() > 0) {
        mArrayAdapter.getFilter().filter(searchPhrase);
        mArrayAdapter.notifyDataSetChanged();

        if (mArrayAdapter.getCount() == 0){
            Toast.makeText(this, "Empty", Toast.LENGTH_SHORT).show();

        } else {
            Toast.makeText(this, "!Empty", Toast.LENGTH_SHORT).show();

        }
    }
    mListView.setAdapter(mArrayAdapter);


}

Thanks

Upvotes: 1

Views: 3191

Answers (5)

DmitryArc
DmitryArc

Reputation: 4837

Try to use FilterListener interface http://developer.android.com/reference/android/widget/Filter.FilterListener.html

    mArrayAdapter.getFilter().filter(searchPhrase, new Filter.FilterListener() {
    public void onFilterComplete(int count) {
        if (count == 0){
            Toast.makeText(this, "Empty", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "!Empty", Toast.LENGTH_SHORT).show();
        }
    }
});

Upvotes: 2

RyanB
RyanB

Reputation: 1297

Are you sure this line:

mArrayAdapter.notifyDataSetChanged();

was called on UI thread? If not, your adapter will have no idea about data changes.

UPDATE

I suggest using FilterListener, here is an example:

mArrayAdapter.getFilter().filter(searchQuery, new Filter.FilterListener() {
    @Override
    public void onFilterComplete(int count) {
        if (count == 0){
            Toast.makeText(MainActivity.this, "0 item", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(MainActivity.this, String.format("Items: %d", count), Toast.LENGTH_SHORT).show();
        }
    }
});

Upvotes: 5

cw fei
cw fei

Reputation: 1564

Detect empty adapter

Use getCount() to track the adapter, it'll return 0 if the adapter was empty.

For example,

if(adapter.getCount()==0)
    // result is empty


Display message

Use the method setEmptyView which sets the view to show if the adapter is empty.

For example,

ListView lv = (ListView)findViewById(android.R.id.list);
TextView emptyText = (TextView)findViewById(android.R.id.empty);
lv.setEmptyView(emptyText);

Upvotes: 1

AnswerDroid
AnswerDroid

Reputation: 1893

ArrayAdapter handles the data related tasks. Better you check the size of the list after filtering and then can make the text view message visible/ invisible accordingly

Upvotes: 1

Rajan Kali
Rajan Kali

Reputation: 12953

it is possible with your List Adapter

if(adapter.getCount()==0)
    textView.setText("No Results Found");
    else
    textView.setText("");

Upvotes: 3

Related Questions