Android使用arraylist显示来自数据库的信息

时间:2014-05-08 12:42:28

标签: java android arraylist

我无法弄清楚如何获得我设法在我设计的arraylist中添加并保存到我的数据库的信息。这个按钮用于获取记录(我已经设置了日志跟踪来检查这个)但是我无法将它们传输到列表视图....我对android很新...

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;
}

1 个答案:

答案 0 :(得分:0)

ArrayList fileList = new ArrayList();

光标游标= database.rawQuery(“SELECT * FROM”+你的表名+“WHERE”+你的条件+“\”“,null);

        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            <the type you want to get the result> offlineFile = cursorToOfflineFile(cursor);

            fileList.add(offlineFile);
            cursor.moveToNext();
        }

cursor.close();

return fileList;