I’m trying to get some data that is stored in an SQLite database and display it in a custom list view.
File Settle.java:
public class Settle extends ListActivity {
SQLiteDatabase DB;
Cursor cur;
SimpleCursorAdapter mAdapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settle);
DB = openOrCreateDatabase("MoneyManager.db",MODE_PRIVATE,null);
cur = DB.rawQuery("SELECT * FROM Money",null);
String[] columns = new String[] {cur.getString(cur.getColumnIndex("name")),Double.toString(cur.getDouble(cur.getColumnIndex("amount")))};
int[] to = new int[] { R.id.settle_name,R.id.settle_amount};
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.settle_entry, cur, columns, to);
this.setListAdapter(mAdapter);
}
protected void onDestroy() {
super.onDestroy();
DB.close();
}
}
File Settle_entry.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/settle_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28dip" />
<TextView
android:id="@+id/settle_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28dip" />
</LinearLayout>
My database schema is
- Name – VARCHAR
- Amount – REAL
- Reason – VARCHAR
How do I go about fetching data from the SQLite database and displaying it in the Custom ListView?
Assuming that your database is already created and populated, your code should work just by changing the columns array:
Anyway, I suggest you to implement some mechanism (that is, an AsyncTask) for loading the cursor in the background.