Note that there are some explanatory texts on larger screens.

plurals
  1. POSimpleCursorAdapter to populate ListView
    primarykey
    data
    text
    <p>I'm working with a database and I'm trying to populate a ListView with each row of my table. I've successfully created and queried data, but I can't get anything to appear in the ListView using a SimpleCursorAdapter.</p> <pre><code>public class History extends ListActivity { SQLiteDatabase db; DbHelper DbHelper; ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.history); DbHelper = new DbHelper(this); db = DbHelper.getWritableDatabase(); lv = getListView(); final String[] from = { DbHelper.TYPE, DbHelper.TITLE, DbHelper.CONTENT }; final String[] column = { DbHelper.C_ID, DbHelper.TYPE, DbHelper.TITLE, DbHelper.CONTENT }; final int[] to = { R.id.list_row_title, R.id.list_row_content }; Cursor cursor = db.query(DbHelper.TABLE_NAME, column, null, null, null, null, null); SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.history_list_row, cursor, from, to); lv.setAdapter(adapter); /* * Display as Toast for development purposes */ // move to first row cursor.moveToFirst(); // move to the next item each time while (cursor.moveToNext()) { // get string and column index from cursor String name = cursor .getString(cursor.getColumnIndex(DbHelper.TYPE)); String title = cursor.getString(cursor .getColumnIndex(DbHelper.TITLE)); String content = cursor.getString(cursor .getColumnIndex(DbHelper.CONTENT)); Toast.makeText(this, "Title: " + title + "\n" + "Content: " + content, Toast.LENGTH_SHORT).show(); } cursor.close(); } </code></pre> <p>}</p> <p>This is my custom ListView row to display 3 Strings from the database</p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" &gt; &lt;TextView android:id="@+id/list_row_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Title" android:textAppearance="?android:attr/textAppearanceLarge" /&gt; &lt;TextView android:id="@+id/list_row_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Content" android:textAppearance="?android:attr/textAppearanceMedium" /&gt; &lt;TextView android:id="@+id/list_row_type" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Type" android:textAppearance="?android:attr/textAppearanceSmall" /&gt; </code></pre> <p></p> <p>The Toast messages appear perfectly, but nothing appears in the ListView. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload