Skip to content

Commit

Permalink
handle orientation changes correctly in SelectFromCursorDialogFragment
Browse files Browse the repository at this point in the history
  • Loading branch information
mtotschnig committed Jul 10, 2013
1 parent 5fdc114 commit 03695a4
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 10 deletions.
28 changes: 28 additions & 0 deletions res/layout/select_dialog_singlechoice.xml
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:textColor="?android:textColorPrimary"
android:gravity="center_vertical"
android:paddingLeft="12dip"
android:paddingRight="7dip"
android:checkMark="@android:drawable/btn_radio"
android:ellipsize="marquee"
/>
26 changes: 21 additions & 5 deletions src/org/totschnig/myexpenses/activity/MyExpenses.java
Expand Up @@ -126,6 +126,7 @@ private void setCurrentAccount(Account newAccount) {
//private Cursor mExpensesCursor;
private MyViewPagerAdapter myAdapter;
private ViewPager myPager;
private String fragmentCallbackTag = null;

/* (non-Javadoc)
* Called when the activity is first created.
Expand Down Expand Up @@ -609,12 +610,22 @@ public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
mAccountsCursor.moveToNext();
}
getSupportActionBar().setSelectedNavigationItem(currentPosition);
if ("SELECT_ACCOUNT".equals(fragmentCallbackTag)) {
((SelectFromCursorDialogFragment) getSupportFragmentManager().findFragmentByTag("SELECT_ACCOUNT"))
.setCursor(new AllButOneCursorWrapper(mAccountsCursor,currentPosition));
fragmentCallbackTag = null;
}
return;
}
//templates cursor that are loaded are not necessarily for the current account
if (id==currentPosition) {
mTemplatesCursor = cursor;
configButtons();
if ("SELECT_TEMPLATE".equals(fragmentCallbackTag)) {
((SelectFromCursorDialogFragment) getSupportFragmentManager().findFragmentByTag("SELECT_TEMPLATE"))
.setCursor(mTemplatesCursor);
fragmentCallbackTag = null;
}
}
}
@Override
Expand Down Expand Up @@ -670,15 +681,20 @@ public void onItemSelected(Bundle args) {
configButtons();
}
@Override
public Cursor getCursor(int cursorId) {
public Cursor getCursor(int cursorId,String fragmentCallbackTag) {
Cursor c = null;
switch(cursorId) {
case ACCOUNTS_CURSOR:
return mAccountsCursor;
c = mAccountsCursor;
break;
case ACCOUNTS_OTHER_CURSOR:
return new AllButOneCursorWrapper(mAccountsCursor,currentPosition);
c = mAccountsCursor == null ? null : new AllButOneCursorWrapper(mAccountsCursor,currentPosition);
break;
case TEMPLATES_CURSOR:
return mTemplatesCursor;
c = mTemplatesCursor;
}
return null;
if (c==null)
this.fragmentCallbackTag = fragmentCallbackTag;
return c;
}
}
@@ -1,6 +1,8 @@
package org.totschnig.myexpenses.dialog;


import org.totschnig.myexpenses.R;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
Expand All @@ -9,10 +11,13 @@
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.widget.SimpleCursorAdapter;
import android.widget.ListView;

public class SelectFromCursorDialogFragment extends DialogFragment implements OnClickListener {
SimpleCursorAdapter mAdapter;
public interface SelectFromCursorDialogListener {
Cursor getCursor(int cursorId);
Cursor getCursor(int cursorId, String tag);
void onItemSelected(Bundle args);
}
/**
Expand All @@ -30,11 +35,12 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
Context ctx = getActivity();
Bundle bundle = getArguments();
String column = bundle.getString("column");
Cursor c = ((SelectFromCursorDialogListener) ctx).getCursor(bundle.getInt("cursorId"),getTag());
mAdapter = new SimpleCursorAdapter(ctx, R.layout.select_dialog_singlechoice,
c, new String[]{column}, new int[]{android.R.id.text1},0);
return new AlertDialog.Builder(ctx)
.setTitle(bundle.getString("dialogTitle"))
.setSingleChoiceItems(
((SelectFromCursorDialogListener) ctx).getCursor(bundle.getInt("cursorId"))
, -1, column, this)
.setAdapter(mAdapter,this)
.create();
}
@Override
Expand All @@ -45,4 +51,7 @@ public void onClick(DialogInterface dialog, int which) {
activity.onItemSelected(args);
dismiss();
}
public void setCursor(Cursor c) {
mAdapter.swapCursor(c);
}
}
2 changes: 1 addition & 1 deletion src/org/totschnig/myexpenses/fragment/TransactionList.java
Expand Up @@ -111,7 +111,7 @@ public void onCreateContextMenu(ContextMenu menu, View v,
menu.add(0, R.id.CLONE_TRANSACTION_COMMAND, 0, R.string.menu_clone_transaction);
mTransactionsCursor.moveToPosition(info.position);
//move transaction is disabled for transfers,
if (((MyExpenses) getSherlockActivity()).getCursor(MyExpenses.ACCOUNTS_CURSOR).getCount() > 1 &&
if (((MyExpenses) getSherlockActivity()).getCursor(MyExpenses.ACCOUNTS_CURSOR,null).getCount() > 1 &&
DbUtils.getLongOrNull(mTransactionsCursor, KEY_TRANSFER_PEER) == null) {
menu.add(0,R.id.MOVE_TRANSACTION_COMMAND,0,R.string.menu_move_transaction);
}
Expand Down

0 comments on commit 03695a4

Please sign in to comment.