Simone
Simone

Reputation: 311

Change listview layout if empty

I use an adapter to set the item layout and to populate a ListView from a JSON string. Sometimes this JSON is empty and there aren't any rows into the ListView and it's full blank.

In this case, I want to change the view and pass another layout showing "There aren't any rows here".

So this is my code:

MainActivity.java

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView listView = (ListView) findViewById(R.id.listView);
        List list = new LinkedList();

        [...fill list...]

        ListAdapter adapter = new ListAdapter(this, R.layout.list_row_layout, list, R.id.edCod, R.id.edDes);
        listView.setAdapter(adapter);
    }
}

ListAdapter.java

public class ListAdapter extends ArrayAdapter<Adapter> {
    private LayoutInflater layoutInflater;
    private int layout;
    private int textCod;
    private int textDes;

    public FruttaAdapter(Context context, int layout, List<Adapter> objects, int textCod, int textDes) {
        super(context, layout, objects);

        this.layout = layout;
        this.textCod = textCod;
        this.textDes = textDes;
        layoutInflater = LayoutInflater.from(context);
    }

Upvotes: 1

Views: 399

Answers (2)

Rafael T
Rafael T

Reputation: 15689

Actually, You could use a ListActivity or ListFragment and use its EmptyView (any View which has the id android.R.id.empty)

Upvotes: -1

Geralt_Encore
Geralt_Encore

Reputation: 3771

You can use listView.setEmptyView(yourEmptyView).

Upvotes: 3

Related Questions