Andrew
Andrew

Reputation: 334

Android ListView and SimpleCursosrAdapter: nothing is showed

A read a lot of docs, but can't figure out what I am doing wrong. I'm just trying to load data with Cursor and show it in a ListView using SimpleCursorAdapter.Here is my code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.select_expense_category);
    DBHelper dbHelper = new DBHelper(this);
    SQLiteDatabase db = dbHelper.getReadableDatabase();
    String q = "SELECT rowid as _id, " + GLO.DBEntry_Categories.COLUMN_CATEGORY +
            " FROM " + GLO.DBEntry_Categories.TABLE_NAME;
    Cursor cursor = db.rawQuery(q, new String[]{});
    cursor.moveToFirst();
    Toast toast = Toast.makeText(this, "Read count: " + Integer.toString(cursor.getCount()) + " " + cursor.getString(0), Toast.LENGTH_LONG);
    toast.show();
    cursor.moveToFirst();
    //TODO: use Loader
    String[] fromCols = {GLO.DBEntry_Categories.COLUMN_CATEGORY};
    int[] toViews = {R.id.MyTextView1};
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor,
            fromCols, toViews, 0);

    ListView listView = (ListView) findViewById(R.id.SelectExpenseCategory_list);
    listView.setAdapter(adapter);
}

So toast message shows there are 3 items in cursor, and data is peresent (at least in first one, but I'm sure for others too). But nothing is showed in ListView.

Here is a part of my activity layout:

<ListView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/SelectExpenseCategory_search"
        android:layout_centerHorizontal="true"
        android:id="@+id/SelectExpenseCategory_list">
</ListView>

And here is complete file for item layout:

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/MyTextView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:padding="5dp"
    android:textColor="@color/cm_dark_green"
    android:textSize="24sp">
</TextView>

Any ideas what's wrong?..

Upvotes: 1

Views: 107

Answers (1)

Simas
Simas

Reputation: 44118

You're using the internal layout for the list items: android.R.layout.simple_list_item_1.

The TextView id there is called text1, so instead use this:

int[] toViews = {android.R.id.text1};

If you want to use your own custom layout then change the layout used by your adapter to e.g. R.layout.list_item_layout.

Upvotes: 1

Related Questions