Skip to content

Commit 03695a4

Browse files
committed
handle orientation changes correctly in SelectFromCursorDialogFragment
1 parent 5fdc114 commit 03695a4

4 files changed

Lines changed: 63 additions & 10 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- Copyright (C) 2008 The Android Open Source Project
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
17+
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
18+
android:id="@android:id/text1"
19+
android:layout_width="fill_parent"
20+
android:layout_height="wrap_content"
21+
android:minHeight="?android:attr/listPreferredItemHeight"
22+
android:textColor="?android:textColorPrimary"
23+
android:gravity="center_vertical"
24+
android:paddingLeft="12dip"
25+
android:paddingRight="7dip"
26+
android:checkMark="@android:drawable/btn_radio"
27+
android:ellipsize="marquee"
28+
/>

src/org/totschnig/myexpenses/activity/MyExpenses.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ private void setCurrentAccount(Account newAccount) {
126126
//private Cursor mExpensesCursor;
127127
private MyViewPagerAdapter myAdapter;
128128
private ViewPager myPager;
129+
private String fragmentCallbackTag = null;
129130

130131
/* (non-Javadoc)
131132
* Called when the activity is first created.
@@ -609,12 +610,22 @@ public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
609610
mAccountsCursor.moveToNext();
610611
}
611612
getSupportActionBar().setSelectedNavigationItem(currentPosition);
613+
if ("SELECT_ACCOUNT".equals(fragmentCallbackTag)) {
614+
((SelectFromCursorDialogFragment) getSupportFragmentManager().findFragmentByTag("SELECT_ACCOUNT"))
615+
.setCursor(new AllButOneCursorWrapper(mAccountsCursor,currentPosition));
616+
fragmentCallbackTag = null;
617+
}
612618
return;
613619
}
614620
//templates cursor that are loaded are not necessarily for the current account
615621
if (id==currentPosition) {
616622
mTemplatesCursor = cursor;
617623
configButtons();
624+
if ("SELECT_TEMPLATE".equals(fragmentCallbackTag)) {
625+
((SelectFromCursorDialogFragment) getSupportFragmentManager().findFragmentByTag("SELECT_TEMPLATE"))
626+
.setCursor(mTemplatesCursor);
627+
fragmentCallbackTag = null;
628+
}
618629
}
619630
}
620631
@Override
@@ -670,15 +681,20 @@ public void onItemSelected(Bundle args) {
670681
configButtons();
671682
}
672683
@Override
673-
public Cursor getCursor(int cursorId) {
684+
public Cursor getCursor(int cursorId,String fragmentCallbackTag) {
685+
Cursor c = null;
674686
switch(cursorId) {
675687
case ACCOUNTS_CURSOR:
676-
return mAccountsCursor;
688+
c = mAccountsCursor;
689+
break;
677690
case ACCOUNTS_OTHER_CURSOR:
678-
return new AllButOneCursorWrapper(mAccountsCursor,currentPosition);
691+
c = mAccountsCursor == null ? null : new AllButOneCursorWrapper(mAccountsCursor,currentPosition);
692+
break;
679693
case TEMPLATES_CURSOR:
680-
return mTemplatesCursor;
694+
c = mTemplatesCursor;
681695
}
682-
return null;
696+
if (c==null)
697+
this.fragmentCallbackTag = fragmentCallbackTag;
698+
return c;
683699
}
684700
}

src/org/totschnig/myexpenses/dialog/SelectFromCursorDialogFragment.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.totschnig.myexpenses.dialog;
22

33

4+
import org.totschnig.myexpenses.R;
5+
46
import android.app.AlertDialog;
57
import android.app.Dialog;
68
import android.content.Context;
@@ -9,10 +11,13 @@
911
import android.database.Cursor;
1012
import android.os.Bundle;
1113
import android.support.v4.app.DialogFragment;
14+
import android.support.v4.widget.SimpleCursorAdapter;
15+
import android.widget.ListView;
1216

1317
public class SelectFromCursorDialogFragment extends DialogFragment implements OnClickListener {
18+
SimpleCursorAdapter mAdapter;
1419
public interface SelectFromCursorDialogListener {
15-
Cursor getCursor(int cursorId);
20+
Cursor getCursor(int cursorId, String tag);
1621
void onItemSelected(Bundle args);
1722
}
1823
/**
@@ -30,11 +35,12 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
3035
Context ctx = getActivity();
3136
Bundle bundle = getArguments();
3237
String column = bundle.getString("column");
38+
Cursor c = ((SelectFromCursorDialogListener) ctx).getCursor(bundle.getInt("cursorId"),getTag());
39+
mAdapter = new SimpleCursorAdapter(ctx, R.layout.select_dialog_singlechoice,
40+
c, new String[]{column}, new int[]{android.R.id.text1},0);
3341
return new AlertDialog.Builder(ctx)
3442
.setTitle(bundle.getString("dialogTitle"))
35-
.setSingleChoiceItems(
36-
((SelectFromCursorDialogListener) ctx).getCursor(bundle.getInt("cursorId"))
37-
, -1, column, this)
43+
.setAdapter(mAdapter,this)
3844
.create();
3945
}
4046
@Override
@@ -45,4 +51,7 @@ public void onClick(DialogInterface dialog, int which) {
4551
activity.onItemSelected(args);
4652
dismiss();
4753
}
54+
public void setCursor(Cursor c) {
55+
mAdapter.swapCursor(c);
56+
}
4857
}

src/org/totschnig/myexpenses/fragment/TransactionList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public void onCreateContextMenu(ContextMenu menu, View v,
111111
menu.add(0, R.id.CLONE_TRANSACTION_COMMAND, 0, R.string.menu_clone_transaction);
112112
mTransactionsCursor.moveToPosition(info.position);
113113
//move transaction is disabled for transfers,
114-
if (((MyExpenses) getSherlockActivity()).getCursor(MyExpenses.ACCOUNTS_CURSOR).getCount() > 1 &&
114+
if (((MyExpenses) getSherlockActivity()).getCursor(MyExpenses.ACCOUNTS_CURSOR,null).getCount() > 1 &&
115115
DbUtils.getLongOrNull(mTransactionsCursor, KEY_TRANSFER_PEER) == null) {
116116
menu.add(0,R.id.MOVE_TRANSACTION_COMMAND,0,R.string.menu_move_transaction);
117117
}

0 commit comments

Comments
 (0)