Change background color when ListView has no items

0

Is there any way to customize a listView in addition to the textView default? The screen is obviously empty, but I would like to change the background color only when fetching it does not display the results.

 mEstadoVazioTextView = (TextView) findViewById(R.id.visualizacaoVazia);
        listview.setEmptyView(mEstadoVazioTextView);

 mEstadoVazioTextView.setText("Nenhum livro encontrado!\n\nFavor verificar ortografia ou" +
                    " informar algum dado sobre o livro novamente.");
    
asked by anonymous 17.05.2017 / 14:47

2 answers

1

Before making the list available to Adapter make sure it is empty and change the background of the ListView accordingly.

Anything like this:

if(lista.size() == 0){
    listview.setBackgroundColor(Color.RED)
else{
    listview.setBackgroundColor(Color.WHITE)
}

Edit after comment

  

I'm using Loader, the list loading is outside the onCreate, how do I reference the variable in that case?

Declare the variable, to save the ListView, as an attribute of the Activity.

private ListView listview;

In the onLoadFinished() method, check that the cursor has results and change the background of the ListView accordingly:

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {

    if(cursor.getCount() == 0){
        listview.setBackgroundColor(Color.RED)
    else{
        listview.setBackgroundColor(Color.WHITE)
    }

    adapter.changeCursor(cursor);
}
    
17.05.2017 / 15:22
1

Hello, look, I'm a newbie in development, but I hope I can help.

From what I understand you have a ListView that is being populated with a list of Books, what you want is if the search does not return the result that a message is displayed to the user.

I've done this in a few different ways.

1) Instead of putting the message in the ListView, you could pick up the list size and if this list has size 0 (no result listed) you can put this message in the Layout in the ListView).

int tamanho = Lista.size();
if(tamanho == 0){
    Texto.SetText("Livro não encontrado")
}

2) Now if you really want to customize the List, I advise you to use a RecyclerView because you will have more freedom in customization. Research on this will surely help.

3) You can still put a Toast informing the user that there is no search result.

I hope I have helped.

    
17.05.2017 / 15:13