Hi,
I am trying to create an ExpandableListView from Cursors. The underlying database/table contains Group data and Child data as multiple rows. I am running a distinct + group by query to get the Group data and a separate query for the related Child Data (using a whereClause).
The child view contains EditTexts and I have put FocusChangeListeners to update the database with a new value.
childHolder.mInstallment.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
View parent = (View) v.getParent();
ChildHolder holder = (ChildHolder) parent.getTag();
Cursor cursor = holder.mCursor;
String installment = ((EditText)v).getText().toString();
if (!cursor.isClosed()) {
int _id = cursor.getInt(cursor
.getColumnIndex(Collections._ID));
Uri uri = Uri.parse(Collections.TRANSACTIONS_URI + "/"
+ _id);
ContentValues values = new ContentValues();
values.put(
Collections.KEY_TRANSACTION_INSTALLMENTAMOUNT,
installment);
getContentResolver().update(uri, values, null, null);
mCursor.requery();
}
//return false;
}
});
I can see that the database values are changing immediately, however the Group View is not changing. And when I collapse the group view I am getting a StaleDataException. I have a feeling this is happening because the groupCursor is not getting updated.
Whats the best way to implement and store state changes of an expandablelistview from cursors?
--- Edit #1
Logcat errors:
08-20 15:18:31.230: ERROR/AndroidRuntime(6167): Uncaught handler: thread main exiting due to uncaught exception
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): android.database.StaleDataException: Access closed cursor
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:175)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:84)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at android.database.CursorWrapper.getInt(CursorWrapper.java:123)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at com.artoo.collections.CollectionsActivity$EAdapter$2.onFocusChange(CollectionsActivity.java:163)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at android.view.View.onFocusChanged(View.java:2602)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at android.widget.TextView.onFocusChanged(TextView.java:6423)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at android.view.View.unFocus(View.java:2530)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at android.view.ViewGroup.unFocus(ViewGroup.java:533)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at android.view.ViewGroup.handleFocusGainInternal(ViewGroup.java:399)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at android.view.View.requestFocus(View.java:3536)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at android.view.ViewGroup.requestFocus(ViewGroup.java:974)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at android.view.View.requestFocus(View.java:3487)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at android.view.View.requestFocus(View.java:3465)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at android.widget.ListView.layoutChildren(ListView.java:1478)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at android.widget.AbsListView.onLayout(AbsListView.java:1112)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at android.view.View.layout(View.java:6569)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at android.view.View.layout(View.java:6569)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at android.view.View.layout(View.java:6569)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at android.view.View.layout(View.java:6569)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at android.view.View.layout(View.java:6569)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at android.view.ViewRoot.performTraversals(ViewRoot.java:979)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at android.view.ViewRoot.handleMessage(ViewRoot.java:1613)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at android.os.Handler.dispatchMessage(Handler.java:99)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at android.os.Looper.loop(Looper.java:123)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at android.app.ActivityThread.main(ActivityThread.java:4203)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at java.lang.reflect.Method.invokeNative(Native Method)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at java.lang.reflect.Method.invoke(Method.java:521)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
08-20 15:18:31.468: ERROR/AndroidRuntime(6167): at dalvik.system.NativeStart.main(Native Method)
I think because of the multiple times the update query is running its closing the cursor and giving a SlateDataException
- Whats best listener (FocusChange/Touch) to pick up value changes in an EditText in an ExpanadableListView.
- Is this the best way of storing the state:
- getContentResolver().update(childUri,...) and then groupCursor.requery()
--- Edit #2
When I closely monitor the program in the debug mode: The control loops the onFocusChange twice (I am not able to focus the EditText in one go -- it requires multiple touches):
First time, everything goes smoothly.
Second time, it crashes while trying to get '_id' variable.
Any help will be most appreciated