我正在製作一個非常簡單的轉儲和顯示應用程序。我希望看到我的新輸入的數據顯示在第一個標籤上的列表中。但我不知道在哪裏放置requery();當我從另一個選項卡添加新任務時,我會在哪裏放置requery()以刷新列表?
這是第一個選項卡的代碼。 (顯示從數據庫中的數據。)
public class ListTask extends ListActivity{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.list_task);
SQLiteDatabase db = openOrCreateDatabase("db_alist", 0, null);
db.execSQL("CREATE TABLE IF NOT EXISTS tbl_alist (_id INTEGER PRIMARY KEY AUTOINCREMENT, task_name VARCHAR, task_date DATE, task_details VARCHAR);");
Cursor c = db.rawQuery("SELECT _id, task_name, task_date FROM tbl_alist", null);
while(c.moveToNext()){
c.getString(c.getColumnIndex("task_name"));
c.getString(c.getColumnIndex("task_date"));
}
db.close();
String[] displayfields = new String[] {"task_name", "task_date"};
int[] displayviews = new int[] {android.R.id.text1, android.R.id.text2};
SimpleCursorAdapter sca = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, c, displayfields, displayviews);
this.setListAdapter(sca);
}
@Override
public void onResume(){
super.onResume();
System.out.println("Resume?");
}
@Override
public void onPause(){
super.onPause();
System.out.println("Paused?");
}}
我試圖做的(僞代碼只)
public class{
Cursor c
public void onCreate(){
...
}
public void onResume(){
super.onResume();
c.requery();
}
}
和
public class{
SimpleCursorAdapter sca
public void onCreate(){
...
}
public void onResume(){
super.onResume();
sca.notifyDataSetChanged();
}
}
但都不起作用!有些東西還是錯的。
如果我把Cursor c;在onCreate()之前,並在onResume()中放入c.requery(),該選項卡不會加載任何東西! – ice3d 2010-08-11 14:37:51
如果你不想讓遊標成爲類字段而不是本地變量,那麼你的SimpleCursorAdapter應該是。在這種情況下,你將在你的SimpleCursorAdapter上調用'notifyDataSetChanged()'。它會處理令人耳目一新的。 – codinguser 2010-08-11 15:09:29
試圖將我的SimpleCursorAdapter作爲局部變量,並在onResume中放入sca.notifyDataSetChanged(),但仍然沒有任何反應。 – ice3d 2010-08-11 15:20:38