0
我無法弄清楚如何獲取我設法添加並保存到數據庫中的信息。這個按鈕的作品來獲取記錄(我已經把日誌跟蹤來檢查這一點),但我不能將它們轉移到列表視圖....我相當新的Android ...Android使用ArrayList顯示數據庫中的信息
public class ActualSalesTracker extends Activity implements OnClickListener {
Button BtnSalesCal, BtnAddRecordDB, BtnViewRecordsDB;
EditText item_name, item_cost, item_price_value, item_postage, actual_pl;
SalesProfitLossCal actSalesCal = new SalesProfitLossCal();
Cursor c;
DbAdapter db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.actual_sales_tracker);
db = new DbAdapter(this);
Log.d("write database", "got here");
Button salesCalBtn = (Button) findViewById(R.id.BtnSalesCal);
// register the click event with the sales calculating profit/loss
// button
salesCalBtn.setOnClickListener(new OnClickListener() {
// activate the view record button
Button viewRecordsDB = (Button) findViewById(R.id.BtnViewRecordsDB);
// register the click event with the add record button
viewRecordsDB.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
db.open();
c = db.getAllRecords();
db.close();
Log.d("got records", "Got the records");
Toast.makeText(ActualSalesTracker.this, "Loading saved sales",
Toast.LENGTH_SHORT).show();
}
});
}
}
}
我得到的所有記錄從我的數據庫適配器
public Cursor getAllRecords() {
return db.query(DATABASE_TABLE, new String[] { KEY_ROWID,
KEY_ITEM_NAME, KEY_ITEM_COST, KEY_ITEM_PRICE_VALUE,
KEY_ITEM_POSTAGE, KEY_ACTUAL_PL }, null, null, null, null,
null, null);
}
/**
* method to retrieve a particular record
*/
public Cursor getRecord(long id) throws SQLException {
Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
KEY_ROWID, KEY_ITEM_NAME, KEY_ITEM_COST, KEY_ITEM_PRICE_VALUE,
KEY_ITEM_POSTAGE, KEY_ACTUAL_PL }, KEY_ROWID + "=" + id, null,
null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
張貼'db.getAllRecords()的代碼;'並請從問題中刪除無關的代碼張貼代碼僅夠否則最確定您的問題甚至不會讀的問題 – SMR
意志。做!謝謝你的反饋 - 我是新手,並感謝你看對我來說這個。 – user3337560
爲什麼在查詢數據庫時使用'ArrayList'?查詢返回一個'Cursor',它可以和'SimpleCursorAdapter'一起用來填充'ListView'。如果你搜索它們,那裏有很多例子。 – Squonk