Really confused regarding the use of a cursor.
I have one activity that works fine, the code is as follows:
public class AreaActivity extends ListActivity {
private TextView secondaryTitle;
private Button newArea;
private static final int ACTIVITY_CREATE=0;
private RMDbAdapter rmDbHelper;
private AlertDialog clickOptionsDialog;
private long inspectionId;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_area);
rmDbHelper = new RMDbAdapter(this);
rmDbHelper.open();
Intent i = getIntent();
inspectionId = i.getLongExtra("Intent_InspectionID", -1);
setUpViews();
setLongClick();
// Get a Cursor for the list items
Cursor listCursor = rmDbHelper.fetchAllAreasForInspection(inspectionId);
startManagingCursor(listCursor);
// set the custom list adapter
setListAdapter(new MyListAdapter(this, listCursor));
}
private void setUpViews() {
secondaryTitle = (TextView)findViewById(R.id.secondary_title);
final Cursor cursor = (Cursor) rmDbHelper.fetchInspection(inspectionId);
String inspectionRef = RMUtilities.notEmpty(cursor.getString(cursor.getColumnIndex(
RMDbAdapter.INSPECTION_REF)), "Reference unknown");
String companyName = RMUtilities.notEmpty(cursor.getString(cursor.getColumnIndex(
RMDbAdapter.INSPECTION_COMPANY)), "company unknown");
cursor.close();
final String secondaryTitleText = inspectionRef + ", " + companyName;
secondaryTitle.setText(secondaryTitleText);
newArea = (Button)findViewById(R.id.new_area);
newArea.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
createArea();
}
});
}
The cursor gets the information from the database ok then checks it is not empty (using a code in separate class as recommended to me on this site) and sets the text in the TextView.
However, when I use this exact code in the next activity to do the same thing, I get the error:
CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0..
The code is the same, so I guess it is an issue with duplicating a cursor or not using moveToFirst but nothing I do seems to solve the problem.
Found the issue thanks to njzk2 – stupid error when I passed the inspectionId to the Intent..