1

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);
    }
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Simone
  • 311
  • 4
  • 16
  • We need a mod to mark this is an exact duplicate – Ali Bdeir Sep 14 '16 at 07:19
  • Also to @Simone, there is no point to add "Android Studio:" to your questions or as a tag. One could have this same exact question without using Android Studio. The tag is specifically for questions *about* the IDE itself, not the processes of writing the code – OneCricketeer Sep 14 '16 at 07:25
  • http://stackoverflow.com/questions/3771568/showing-empty-view-when-listview-is-empty – kgandroid Sep 14 '16 at 08:01

2 Answers2

3

You can use listView.setEmptyView(yourEmptyView).

Geralt_Encore
  • 3,721
  • 2
  • 31
  • 46
-1

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

Rafael T
  • 15,401
  • 15
  • 83
  • 144