Why is my ListView not showing anything?

A few things I can think of.

First, Your RelativeLayout has no relative positioning information. I would assume you meant to put this in a LinearLayout with orientation set to vertical from what you describe. My guess is that the list is not actually being drawn since it isn't even anchored to anything in the current RelativeLayout. If you stick with the RelativeLayout, make sure to put an id on the app_name TextView and position the ListView under it via layout_below.

LinearLayout Solution

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  <TextView android:text="@string/app_name"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  ></TextView>
  <ListView android:id="@+id/selectView"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content">
    </ListView>
</LinearLayout>

RelativeLayout Solution:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  <TextView
  android:id="@+id/app_name_text" 
  android:text="@string/app_name"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentTop="true"
  ></TextView>
  <ListView android:id="@+id/selectView"
    android:layout_below="@id/app_name_text"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content">
    </ListView>
</RelativeLayout>

Next, your getView() returns the same textView for all 3 indexes. It's not a problem to display the same view over multiple indexes however with a list size of three, I am betting that the screen can display all three at the same time. And since a View can't be in more than one position at a time, I actually would expect this to fail so I doubt it is even getting to this code yet. Try creating a new TextView for each getView(). Also your MockTextView doesn't have layout params of it's own. So laying it out within a listView cell might not be happening either. So you can give it params of type AbsListView.LayoutParams(WRAP_CONTENT, WRAP_CONTENT). Again though I would expect this to error if it got to the original code.

getView() tidy up:

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       if(convertView == null) {
          TextView textView = new TextView(parent.getContext());
          textView.setText("Position is:"+position);
          textView.setBackgroundColor(Color.CYAN);
          textView.setLayoutParams(new AbsListView.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
          convertView = textView;
        }
        return mMockTextView;
    }

And lastly the wrap_content height of your list can sometimes be problematic. I am not aware of all the scenarios. If you end up changing to a LinearLayout try setting your layout_height of the list view to 0 and then set the layout_weight=1. This forces the linear layout to inflate it into more space.

LinearLayout Weight Solution:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  <TextView android:text="@string/app_name"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  ></TextView>
  <ListView android:id="@+id/selectView"
    android:layout_weight="1"
    android:layout_height="0dp"
    android:layout_width="wrap_content">
    </ListView>
</LinearLayout>

I was using the Data Binding example from the book Hello Android by Ed Burnette (great book).

I changed the item layout from A RelativeLayout to a LinearLayout; however, I did not add an orientation when I made the change.

Once I added android:orientation="vertical" everything worked fine.

Two hours of my life on this one.


While it does not answer this question, a typical issue is to inflate as adapter resource a layout based on a parent LinearLayout with height=wrap_content and each LinearLayout element with height=fill_parent.

Like in

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parentLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<TextView
    android:id="@+id/someText"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"/>

While it is displayed nicely in Eclipse, each row ends with height=0. This can be fixed by setting height=wrap_content for one of the child element

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parentLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<TextView
    android:id="@+id/someText"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>