|
49 | 49 | import com.actionbarsherlock.view.Menu; |
50 | 50 | import com.actionbarsherlock.view.MenuInflater; |
51 | 51 | import com.actionbarsherlock.view.MenuItem; |
| 52 | +import org.gnucash.android.util.QualifiedAccountNameCursorAdapter; |
52 | 53 |
|
53 | 54 | /** |
54 | 55 | * Fragment used for creating and editing accounts |
@@ -163,22 +164,23 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
163 | 164 | public void onActivityCreated(Bundle savedInstanceState) { |
164 | 165 | super.onActivityCreated(savedInstanceState); |
165 | 166 |
|
166 | | - ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>( |
| 167 | + ArrayAdapter<String> currencyArrayAdapter = new ArrayAdapter<String>( |
167 | 168 | getActivity(), |
168 | 169 | android.R.layout.simple_spinner_item, |
169 | 170 | getResources().getStringArray(R.array.currency_names)); |
170 | | - arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
171 | | - mCurrencySpinner.setAdapter(arrayAdapter); |
172 | | - |
173 | | - loadParentAccountList(); |
174 | | - loadAccountTypesList(); |
| 171 | + currencyArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
| 172 | + mCurrencySpinner.setAdapter(currencyArrayAdapter); |
175 | 173 |
|
176 | 174 | mSelectedAccountId = getArguments().getLong(TransactionsListFragment.SELECTED_ACCOUNT_ID); |
177 | 175 | if (mSelectedAccountId > 0) { |
178 | 176 | mAccount = mAccountsDbAdapter.getAccount(mSelectedAccountId); |
179 | 177 | getSherlockActivity().getSupportActionBar().setTitle(R.string.title_edit_account); |
180 | 178 | } |
181 | 179 |
|
| 180 | + //need to load the cursor adapters for the spinners before initializing the views |
| 181 | + loadParentAccountList(); |
| 182 | + loadAccountTypesList(); |
| 183 | + |
182 | 184 | if (mAccount != null){ |
183 | 185 | initializeViewsWithAccount(mAccount); |
184 | 186 | } else { |
@@ -268,26 +270,38 @@ public boolean onOptionsItemSelected(MenuItem item) { |
268 | 270 |
|
269 | 271 | return false; |
270 | 272 | } |
271 | | - |
| 273 | + |
| 274 | + /** |
| 275 | + * Loads the list of possible accounts which can be set as a parent account and initializes the spinner |
| 276 | + */ |
272 | 277 | private void loadParentAccountList(){ |
273 | | - String condition = DatabaseHelper.KEY_ROW_ID + "!=" + mSelectedAccountId; |
| 278 | + String condition = null; |
| 279 | + if (mAccount != null){ //if editing an account |
| 280 | + // limit cyclic account hierarchies. Still technically possible since we don't forbid descendant accounts |
| 281 | + condition = "(" + DatabaseHelper.KEY_PARENT_ACCOUNT_UID + " IS NULL " |
| 282 | + + " OR " + DatabaseHelper.KEY_PARENT_ACCOUNT_UID + " != '" + mAccount.getUID() + "')" |
| 283 | + + " AND " + DatabaseHelper.KEY_ROW_ID + "!=" + mSelectedAccountId; |
| 284 | + //TODO: Limit all descendants of the account to eliminate the possibility of cyclic hierarchy |
| 285 | + } |
| 286 | + |
274 | 287 | mCursor = mAccountsDbAdapter.fetchAccounts(condition); |
275 | | - if (mCursor.getCount() <= 0){ |
| 288 | + if (mCursor == null || mCursor.getCount() <= 0){ |
276 | 289 | final View view = getView(); |
277 | 290 | view.findViewById(R.id.layout_parent_account).setVisibility(View.GONE); |
278 | 291 | view.findViewById(R.id.label_parent_account).setVisibility(View.GONE); |
279 | 292 | } |
280 | 293 |
|
281 | | - String[] from = new String[] {DatabaseHelper.KEY_NAME}; |
282 | | - int[] to = new int[] {android.R.id.text1}; |
283 | | - mCursorAdapter = new SimpleCursorAdapter( |
| 294 | + mCursorAdapter = new QualifiedAccountNameCursorAdapter( |
284 | 295 | getActivity(), |
285 | 296 | android.R.layout.simple_spinner_item, |
286 | | - mCursor, from, to, 0); |
| 297 | + mCursor); |
287 | 298 | mCursorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
288 | 299 | mParentAccountSpinner.setAdapter(mCursorAdapter); |
289 | 300 | } |
290 | 301 |
|
| 302 | + /** |
| 303 | + * Loads the list of account types into the account type selector spinner |
| 304 | + */ |
291 | 305 | private void loadAccountTypesList(){ |
292 | 306 | String[] accountTypes = getResources().getStringArray(R.array.account_type_entry_values); |
293 | 307 | ArrayAdapter<String> accountTypesAdapter = new ArrayAdapter<String>( |
|
0 commit comments