33import android .annotation .SuppressLint ;
44import android .graphics .drawable .ColorDrawable ;
55import android .os .Build ;
6+ import android .support .annotation .Nullable ;
67import android .view .Menu ;
78import android .view .MenuItem ;
89import android .view .Window ;
@@ -91,30 +92,15 @@ record = getIntent().getParcelableExtra(KEY_RECORD);
9192 protected void initViews () {
9293 super .initViews ();
9394
94- List <String > accounts = new ArrayList <>();
95- for (Account account : accountList ) {
96- accounts .add (account .getTitle ());
97- }
98-
99- spinnerAccount .setAdapter (new ArrayAdapter <>(AddRecordActivity .this ,
100- android .R .layout .simple_list_item_1 , accounts ));
101-
10295 //Add texts to dialog if it's edit dialog
10396 if (mode == Mode .MODE_EDIT ) {
10497 etTitle .setText (record .getTitle ());
10598 if (record .getCategory () != null ) etCategory .setText (record .getCategory ().getName ());
10699 etPrice .setText (Integer .toString (record .getPrice ()));
107-
108- if (record .getAccount () != null ) {
109- for (int i = 0 ; i < accountList .size (); i ++) {
110- Account account = accountList .get (i );
111- if (account .getId () == record .getAccount ().getId ()) {
112- spinnerAccount .setSelection (i );
113- }
114- }
115- }
116100 }
117101
102+ presentSpinnerAccount ();
103+
118104 if (getSupportActionBar () != null ) {
119105 switch (type ) {
120106 case Record .TYPE_EXPENSE :
@@ -170,6 +156,38 @@ public boolean onOptionsItemSelected(MenuItem item) {
170156 }
171157 }
172158
159+ private void presentSpinnerAccount () {
160+ List <String > accounts = new ArrayList <>();
161+ for (Account account : accountList ) {
162+ accounts .add (account .getTitle ());
163+ }
164+
165+ int selectedAccountIndex = 0 ;
166+
167+ if (mode == Mode .MODE_EDIT ) {
168+ selectedAccountIndex = -1 ;
169+
170+ if (record .getAccount () != null ) {
171+ for (int i = 0 ; i < accountList .size (); i ++) {
172+ Account account = accountList .get (i );
173+ if (account .getId () == record .getAccount ().getId ()) selectedAccountIndex = i ;
174+ }
175+ }
176+
177+ if (selectedAccountIndex == -1 ) {
178+ selectedAccountIndex = 0 ;
179+ spinnerAccount .setEnabled (false );
180+
181+ accounts = new ArrayList <>();
182+ accounts .add (getString (R .string .account_was_removed ));
183+ }
184+ }
185+
186+ spinnerAccount .setAdapter (new ArrayAdapter <>(AddRecordActivity .this ,
187+ android .R .layout .simple_list_item_1 , accounts ));
188+ spinnerAccount .setSelection (selectedAccountIndex );
189+ }
190+
173191 private boolean prepareRecord () {
174192 String title = etTitle .getText ().toString ().trim ();
175193 String category = etCategory .getText ().toString ().trim ();
@@ -184,12 +202,17 @@ private boolean prepareRecord() {
184202 }
185203
186204 if (price >= 0 && price <= 1000000000 && spinnerAccount .getSelectedItemPosition () >= 0 ) {
187- Account account = accountList .get (spinnerAccount .getSelectedItemPosition ());
205+ Account account = null ;
206+ if (spinnerAccount .isEnabled ())
207+ account = accountList .get (spinnerAccount .getSelectedItemPosition ());
208+
188209 return doRecord (title , category , price , account );
189210 } else return false ;
190211 }
191212
192- protected boolean doRecord (String title , String category , int price , Account account ) {
213+ private boolean doRecord (String title , String category , int price , @ Nullable Account account ) {
214+ if (account == null ) return false ;
215+
193216 if (mode == Mode .MODE_ADD ) {
194217 switch (type ) {
195218 case Record .TYPE_EXPENSE :
0 commit comments