Tom
Tom

Reputation: 7436

Android: SQLite and ListViews

Firstly, I have found many examples of how to grab data from a db and place it into a list, however this seems to be all for ListActivites.

My list is part of the UI and therefore I can't use a ListActivity because it does not consume the whole screen (or can I?).

This is the UI:

<SlidingDrawer android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
   android:id="@+id/Drawer"
  android:orientation="vertical"
  android:handle="@+id/handle" 
   android:content="@+id/content">

<ImageView
android:id="@id/handle"
android:src="@drawable/tray_handle_normal"
android:layout_height="wrap_content" android:layout_width="wrap_content"/>

<RelativeLayout 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:id="@id/content" android:background="@color/black">


<ListView 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:id="@+id/List">
</ListView>

</RelativeLayout>

So, from what ive read I need to grab the data from the db, then place it into some sort of array then use an array adapter to fill the list view. Is that correct? If so, is there some sample code because all I can find is code releated to ListActivites.

Upvotes: 0

Views: 2773

Answers (2)

Erich Douglass
Erich Douglass

Reputation: 52002

You can still use a ListActivity. The magic is to set the id of your ListView to "@android:id/list". However, ListActivity is just a convenience. You can get the same result by creating an adapter (look at ArrayAdapter or SimpleCursorAdapter) and connecting it to the ListView by hand.

Upvotes: 2

JRL
JRL

Reputation: 78033

I think you can use a SimpleCursorAdapter for this.

I think the Notepad tutorial uses it. If you need more complicated things, it's just a matter of you implementing your own view adapter. Here's sample code that shows you how to do it.

Upvotes: 1

Related Questions