所以我在我的MainActivity中有這個列表視圖,每次主要活動開始或恢復時都會更新。 該代碼是從意圖返回主活動後更新光標
@Override public void onResume() { super.onResume(); Cursor array_list_patients = mydb.getAllPatientsDetails(); PatientAdapter arrayAdapter = new n PatientAdapter(this,array_list_patients); obj = (ListView)findViewById(R.id.listView1); obj.setAdapter(arrayAdapter); }
現在在MainActivity列表視圖我有如下所示的setOnItemClickListener,其基本上顯示在另一活性「ModifyPatient」的數據。代碼是:
Cursor array_list_patients = mydb.getAllPatientsDetails();
final PatientAdapter arrayAdapter = new PatientAdapter(this,array_list_patients);
obj = (ListView)findViewById(R.id.listView1);
obj.setAdapter(arrayAdapter);
obj.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
Cursor cur = (Cursor) arrayAdapter.getItem(arg2);
cur.moveToPosition(arg2);
int id_To_Search = cur.getInt(cur.getColumnIndexOrThrow("id"));
Bundle dataBundle = new Bundle();
dataBundle.putInt("id", id_To_Search);
Intent intent = new Intent(getApplicationContext(),ModifyPatient.class);
intent.putExtras(dataBundle);
startActivity(intent);
}
});
現在在ModifyPatient類..我可以做Upate /刪除的數據庫條目操作。現在的問題是,當我做一個刪除操作,並返回到MainActivity,並點擊列表視圖中的任何項目,我得到一個OutOfBounds異常的遊標。在ModifyPatient類中刪除按鈕,當前的代碼如下:
deleteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(ModifyPatient.this);
builder.setMessage(R.string.deleteContact)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mydb.deletePatient(id_To_Update);
Toast.makeText(getApplicationContext(), "Deleted Successfully",
Toast.LENGTH_SHORT).show();
ModifyPatient.this.finish();
}
})
.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
AlertDialog d = builder.create();
d.setTitle("Are you sure");
d.show();
}
});
這將刪除條目,並返回到在MainActivity,但點擊任何在ListView的項目投我之前提到的錯誤。 我做了一個小的解決辦法,在那裏我已經取代了下面一行:
ModifyPatient.this.finish();
與
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
但隨着這就是問題所在,原來的活動仍然在後臺和新MainActivity是分叉。所以按下後退按鈕,返回到陳舊列表視圖的原始活動。
如果有人可以幫助我,如何更新遊標,因爲我不能在我的情況下實現swapcursor。我不知道我做錯了什麼。
預先感謝您。