我正在尝试获取一些存储在 SQLite 数据库中的数据并将其显示在自定义列表视图中。
文件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();
}
}
文件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>
我的数据库架构是
- 名称 - VARCHAR
- 金额 - 真实
- 原因 - VARCHAR
如何从 SQLite 数据库中获取数据并将其显示在 Custom 中ListView
?