Stelios
Stelios

Reputation: 271

ListView with CursorAdapter does not display my values

I want to display some of the contents of my database to the user with a scroll ability.

So I have extended the ListActivity and I used the SimpleCursorAdapter. The problem is that i can not see the values displaced.

private void fillData() {
 // Get all of the rows from the database and create the item list
ListData = data.findAllDbValues();
startManagingCursor(ListData);

// Create an array to specify the fields we want to display in the list
String[] from = new String[]{Data.ID, Data.DESC};

// and an array of the fields we want to bind those fields to 
int[] to = new int[]{R.id.text1, R.id.text2};

// Now create a simple cursor adapter and set it to display 
ListAdapter la = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, ListData, from, to);//R.layout.showdata
setListAdapter(la);

}

If I use a custom layout(R.layout.showdata),where it has only two TextViews, I can see the values but I have to touch the text which is displayed to do my job instead of just touching anywhere of the selection.

So I want to use a simple_list_item_2 so I can have this ability. Any ideas why I do not see the values or how can I make my custom layout to produce the same abilities with the simple_list?

Upvotes: 1

Views: 799

Answers (2)

Navarro
Navarro

Reputation: 61

I know this question is old but I arrived here through Google and I think I have the answer since I had similar problem.

You are referencing custom TextView in your int[] to array, not the Android ones. You must use instead:

// and an array of the fields we want to bind those fields to 
int[] to = new int[]{android.R.id.text1, android.R.id.text2};

Just check out that the ids in the XML and the ids in your class are the same. When you use custom views the reference in your class is R.id.text1 and in your layout android:id="@+id:text1" but when you use Android resources the reference in your class is android.R.id.text1 and in your layout android:id="@android:id/text1".

You can use any of them but it must be consistent.

I was using android:id="@+id:text1" in my XML (creating a new TextView with id "text1") but using android.R.id.text1 in my Java Class so the CursorAdaptor was setting the information in a non-existent View and I was getting exactly that result: a listview with all the rows without text.

Upvotes: 0

Haphazard
Haphazard

Reputation: 10948

This worked for me:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android_orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:paddingLeft="4dip"
android:paddingRight="5dp">
<TextView android:id="@+id/productRow"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>

The trick is to wrap the TextView row in a LinearLayout. This lets me click anywhere on the list element to select it.

Upvotes: 1

Related Questions