1

我想从弹出窗口中添加任务名称,它会自动添加到列表中。那么,有没有办法自动更新数据库。这是我的方法

SampleDB = this.openOrCreateDatabase("SampleDB", SQLiteDatabase.CREATE_IF_NECESSARY, null);
                String query = "select *from TaskList";
                Cursor c = SampleDB.rawQuery(query, null);
                c.moveToFirst();
                do{
                    String Task = c.getString(c.getColumnIndex("task"));

                    /* Add current Entry to results. */ 
                 results.add(Item);
                }while(c.moveToNext());
                 this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));
4

1 回答 1

0

此方法可以将项目添加到您的数据库中:

public void addTaskToDb(SQLiteDatabase database, String task) {
    ContentValues values = new ContentValues();
    values.put("task", task);

    database.insert("TaskList", null, values);
}

然后像这样替换您的代码:

SampleDB = this.openOrCreateDatabase("SampleDB", SQLiteDatabase.CREATE_IF_NECESSARY, null);
ArrayAdapter<String> adapter = (ArrayAdapter<String>)this.getListAdapter();

this.addTaskToDb(SampleDB, Item);
adapter.add(Item);
于 2013-03-19T09:11:21.937 回答