So i am having this listview in my MainActivity, which is updated every time the main activity is started or resumed. The code for that is
@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);
}
Now for the listview in the MainActivity i have a setOnItemClickListener as shown below, which basically displays the data in another activity "ModifyPatient". Code for that is:
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);
}
});
Now in the ModifyPatient class.. i can do Upate/Delete operations on the database entry. Now the problem is, when i do a delete operation and return back to the MainActivity, and click on any item in the listview, i get an OutOfBounds exception for the cursor. The current code for delete button in the ModifyPatient class is as follows:
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();
}
});
This deletes the entry and returns to the mainactivity, but clicking on any of the items in the listview throws the error which i mentioned before. I have done a small workaround, where i have replaced the below line:
ModifyPatient.this.finish();
with
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
But the problem with this is, the original activity remains in the background and a new MainActivity is forked. So pressing the back button, goes back to the original activity with the stale listview.
If somebody can help me out, with how to update the cursor, because i could not implement the swapcursor in my case. I am not sure what i am doing wrong.
Thank you in advance.