Reputation: 2669
I've got the basic premise of how to get data from a sqlite db, and i've got it to log an item returned to the logcat. However, I can't seem to work out the best way to output that data in to a listview.
At first I thought i'd put the data in to an array, and setup a listview using that array, however from looking around you can link the Cursor directly as a datasource to a listview but I can't quite get my head around it.
Here is my MainActivity (once i've worked it out a bit more, i'd put the sql in to it's own helper class, but for now it's all from the main activity)
My main activity is:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SQLiteDatabase db = openOrCreateDatabase("MyDB", MODE_PRIVATE, null);
Cursor c = db.rawQuery("SELECT * FROM MyTable", null);
c.moveToFirst();
Log.e("TomDebug", c.getString(c.getColumnIndex("FirstName")));
db.close();
}
}
My layout activiy_main.xml is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="fill" >
<ListView
android:id="@+id/derooms"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
My table has three columns, but for now i'd be happy with it just spewing out all the FirstNames in the database to a listview
Tom
Upvotes: 0
Views: 353
Reputation: 1248
It seems you haven't coded any line to your listview, your question should not be answered because of your weak effort in contribution. Anyway, here is the demo code (not exaclty do all your requirement) for the answer:
(1) Create list_view_item.xml
to show your information in the list view, for e.g: a to show out your data field.
(2) Create DataBoundAdapter
to bound to your result of the DB cursor:
public class DataBoundAdapter extends CursorAdapter
{
Context _context;
public DataBoundAdapter(Context context, Cursor c, boolean autoRequery) {
super(context, c, autoRequery);
_context = context;
}
@Override
public void bindView(View view, Context c, Cursor cur)
{
// TODO: handle data when binding to your list view
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent)
{
int item_view_id = R.layout.list_view_item;
//inflate item view to list view holder
LinearLayout holderView = new LinearLayout(_context);
String inflaterName = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater inflater = (LayoutInflater) _context.getSystemService(inflaterName);
inflater.inflate(item_view_id, holderView, true);
return holderView;
}
}
(3) In MainActivity.onCreate(..)
:
ListView myListView = (ListView)findViewById(R.id.derooms);
DataBoundAdapter dbAdapter = new DataBoundAdapter(this, your_db_cursor, true);
myListView.setAdapter(dbAdapter);
Upvotes: 1
Reputation: 39396
You need an CursorAdapter. see http://developer.android.com/reference/android/widget/CursorAdapter.html
you can use a SimpleCursorAdapter to begin. If you are targetting 11+, you'll want to take a look at http://developer.android.com/reference/android/content/CursorLoader.html
Upvotes: 0