SimpleCursorAdapter
可以被用來獲得從所述ListView
和database
顯示的數據。
下面是實現您的要求的示例代碼:
int[] names = new int[] {R.id.Full_Name,R.id.name};
private static final String fields[] = {"DatabaseColumn_Name1", "DatabaseColumn_Name2"};
ListView = (ListView)findViewById(R.id.list1);
DataBaseHelper myDbHelper = new DataBaseHelper(null);
myDbHelper = new DataBaseHelper(this);
String sql ="SELECT STATEMENT";
Cursor cdata = myDbHelper.getView(sql);
if (cdata != null)
{
cdata.moveToFirst();
while (cdata.isAfterLast() == false) {
String tx = (cdata.getString(2));
cdata.moveToNext();
}
startManagingCursor(cdata);
CursorAdapter adaptr = new MyCursorAdapter(
getApplicationContext(), R.layout.listview1, cdata, fields, names);
試試這個 > ['http://stackoverflow.com/questions/9785563/how-to-write-reusable-code-for-database-in-android/9785657#9785657'][1] [1]:http://stackoverflow.com/questions/9785563/how-to-write-reusable-code-for-database-in-android/9785657#9785657 – Hasmukh