I’m facing a frustrating issue , I have an Activity with 3 Tabs and 3 ListViews , I’m facing an issue with the OnItemClick event. when i click on any entry it opens another activity and showing the desired results properly but when i get back again to the listview and try to open any entry i got the Index out of bound exception Index -1 requested. What makes no sense is that , there is a listview from the 3 can works properly and the other 2 is not. Hereunder my code :
This OnResume () :
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(final Editable s) {
mAdapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
String value = "%" + constraint + "%";
String Type2 = "%" + jype + "%";
curs = mDb.query(TABLE_NAME, columns, COL_SanTitle
+ " LIKE ? And " + COL_SanCat + " LIKE ?",
new String[] { value, Type2 }, null, null,
COL_SanTitle + " ASC");
return curs;
}
});
fAdapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
String value = "%" + constraint + "%";
String Type2 = "%YES%";
cursF = mDb.query(TABLE_NAME, columns, COL_SandFavor
+ " LIKE ? And " + COL_SanTitle + " LIKE ?",
new String[] { Type2, value }, null, null,
COL_SanTitle + " ASC");
return cursF;
}
});
dAdapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
String value = "%" + constraint + "%";
String Type2 = "%" + jype + "%";
cursD = mDb.query(TABLE_NAME, columns, COL_SanTitle
+ " LIKE ? And " + COL_SanCat + " LIKE ?",
new String[] { value, Type2 }, null, null,
COL_SanTitle + " ASC");
cursD.moveToFirst();
return cursD;
}
}
);
fAdapter.getFilter().filter(s.toString());
mAdapter.getFilter().filter(s.toString());
dAdapter.getFilter().filter(s.toString());
mAdapter.notifyDataSetChanged();
fAdapter.notifyDataSetChanged();
dAdapter.notifyDataSetChanged();
mList.setAdapter(mAdapter);
fList.setAdapter(fAdapter);
dList.setAdapter(dAdapter);
cursD.moveToFirst();
cursS.moveToFirst();
curs.moveToFirst();
}
});
Here is the Logcat error :
04-26 18:56:50.310: E/AndroidRuntime(878): FATAL EXCEPTION: main
04-26 18:56:50.310: E/AndroidRuntime(878): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
04-26 18:56:50.310: E/AndroidRuntime(878): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
04-26 18:56:50.310: E/AndroidRuntime(878): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
04-26 18:56:50.310: E/AndroidRuntime(878): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
04-26 18:56:50.310: E/AndroidRuntime(878): at master.chef.mediamaster.Interface.onItemClick(Interface.java:781)
04-26 18:56:50.310: E/AndroidRuntime(878): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
04-26 18:56:50.310: E/AndroidRuntime(878): at android.widget.ListView.performItemClick(ListView.java:3513)
04-26 18:56:50.310: E/AndroidRuntime(878): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
04-26 18:56:50.310: E/AndroidRuntime(878): at android.os.Handler.handleCallback(Handler.java:587)
04-26 18:56:50.310: E/AndroidRuntime(878): at android.os.Handler.dispatchMessage(Handler.java:92)
04-26 18:56:50.310: E/AndroidRuntime(878): at android.os.Looper.loop(Looper.java:123)
04-26 18:56:50.310: E/AndroidRuntime(878): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-26 18:56:50.310: E/AndroidRuntime(878): at java.lang.reflect.Method.invokeNative(Native Method)
04-26 18:56:50.310: E/AndroidRuntime(878): at java.lang.reflect.Method.invoke(Method.java:507)
04-26 18:56:50.310: E/AndroidRuntime(878): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-26 18:56:50.310: E/AndroidRuntime(878): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-26 18:56:50.310: E/AndroidRuntime(878): at dalvik.system.NativeStart.main(Native Method)
You start another activity, so that opens up the previous one to be removed, and it sounds like it is. To overcome this, move your list generation code to onResume, then it will run when you go to another activity and come back, as well as when you initially start the activity.