2

I am very new to Android development so please excuse any naivety.

I have created a list view on my activity:

<ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/textView"
        android:layout_toEndOf="@+id/textView" />

I have extracted all the date from my sqllite table:

public Cursor getAll()
    {
        SQLiteDatabase database = getReadableDatabase();
        Cursor cursor = database.rawQuery("select * from todos",null);
        return cursor;
    }

(At least I think I have, Im not sure how to test if there is data in there, if the database exists, if the table exists etc as I don't know how to view the database).

How do I populate the list view with the rows from my table?

RSM
  • 14,540
  • 34
  • 97
  • 144
  • Sorry, but this question is too broad for SO... There is a plenty examples over the internet(including original d.android.com site) – Selvin Feb 23 '15 at 22:44
  • I have looked before I asked. If you know any good ones please send me that way. – RSM Feb 23 '15 at 22:45
  • NotePad sample app from Android SDK - of course you have to change it to use Loaders API and Fragments (LoaderCursorSupport.java from support v4 ) – Selvin Feb 23 '15 at 22:47

1 Answers1

4

The short answer is that you can use a Cursor Adapter.

See this post: Cursor adapter and sqlite example

This might also be helpful: https://github.com/codepath/android_guides/wiki/Populating-a-ListView-with-a-CursorAdapter

Note that in addition to your xml layout file that has the ListView, you should have a xml layout file that defines each row in the ListView, which is what you will feed to your CursorAdapter.

Community
  • 1
  • 1
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137