47 import android.widget.LinearLayout; |
48 import android.widget.LinearLayout; |
48 import android.widget.RelativeLayout; |
49 import android.widget.RelativeLayout; |
49 import android.widget.ScrollView; |
50 import android.widget.ScrollView; |
50 import android.widget.SimpleAdapter; |
51 import android.widget.SimpleAdapter; |
51 import android.widget.Spinner; |
52 import android.widget.Spinner; |
|
53 import android.widget.SpinnerAdapter; |
52 import android.widget.TextView; |
54 import android.widget.TextView; |
53 import android.widget.Toast; |
55 import android.widget.Toast; |
54 |
56 |
55 public class TeamCreatorActivity extends Activity implements Runnable{ |
57 /** |
56 |
58 * Edit or create a team. If a team should be edited, it is supplied in the extras |
|
59 * as parameter oldTeamName. |
|
60 */ |
|
61 public class TeamCreatorActivity extends Activity implements Runnable { |
|
62 public static final String PARAMETER_EXISTING_TEAMNAME = "existingTeamName"; |
|
63 |
57 private TextView name; |
64 private TextView name; |
58 private Spinner difficulty, grave, flag, voice, fort; |
65 private Spinner difficulty, grave, flag, voice, fort; |
59 private ImageView imgFort; |
66 private ImageView imgFort; |
60 private ArrayList<ImageButton> hogDice = new ArrayList<ImageButton>(); |
67 private ArrayList<ImageButton> hogDice = new ArrayList<ImageButton>(); |
61 private ArrayList<Spinner> hogHat = new ArrayList<Spinner>(); |
68 private ArrayList<Spinner> hogHat = new ArrayList<Spinner>(); |
62 private ArrayList<EditText> hogName = new ArrayList<EditText>(); |
69 private ArrayList<EditText> hogName = new ArrayList<EditText>(); |
63 private ImageButton back, save, voiceButton; |
70 private ImageButton voiceButton; |
64 private ScrollView scroller; |
71 private ScrollView scroller; |
65 private MediaPlayer mp = null; |
72 private MediaPlayer mp = null; |
66 private boolean settingsChanged = false; |
73 private boolean initComplete = false; |
67 private boolean saved = false; |
74 |
68 private String fileName = null; |
75 private String existingTeamName = null; |
69 |
76 |
70 private final List<HashMap<String, ?>> flagsData = new ArrayList<HashMap<String, ?>>(); |
77 private final List<HashMap<String, ?>> flagsData = new ArrayList<HashMap<String, ?>>(); |
71 private final List<HashMap<String, ?>> typesData = new ArrayList<HashMap<String, ?>>(); |
78 private final List<HashMap<String, ?>> typesData = new ArrayList<HashMap<String, ?>>(); |
72 private final List<HashMap<String, ?>> gravesData = new ArrayList<HashMap<String, ?>>(); |
79 private final List<HashMap<String, ?>> gravesData = new ArrayList<HashMap<String, ?>>(); |
73 private final List<HashMap<String, ?>> hatsData = new ArrayList<HashMap<String, ?>>(); |
80 private final List<HashMap<String, ?>> hatsData = new ArrayList<HashMap<String, ?>>(); |
74 private final List<String> voicesData = new ArrayList<String>(); |
81 private final List<String> voicesData = new ArrayList<String>(); |
75 private final List<String> fortsData = new ArrayList<String>(); |
82 private final List<String> fortsData = new ArrayList<String>(); |
76 |
83 |
77 public void onCreate(Bundle savedInstanceState) { |
84 public void onCreate(Bundle savedInstanceState) { |
78 super.onCreate(savedInstanceState); |
85 super.onCreate(savedInstanceState); |
|
86 initComplete = false; |
|
87 |
|
88 // Restore state and read parameters |
|
89 if(savedInstanceState != null) { |
|
90 existingTeamName = savedInstanceState.getString(PARAMETER_EXISTING_TEAMNAME); |
|
91 } else { |
|
92 existingTeamName = getIntent().getStringExtra(PARAMETER_EXISTING_TEAMNAME); |
|
93 } |
|
94 |
|
95 // Set up view |
79 setContentView(R.layout.team_creation); |
96 setContentView(R.layout.team_creation); |
80 |
97 |
81 name = (TextView) findViewById(R.id.txtName); |
98 name = (TextView) findViewById(R.id.txtName); |
82 difficulty = (Spinner) findViewById(R.id.spinType); |
99 difficulty = (Spinner) findViewById(R.id.spinType); |
83 grave = (Spinner) findViewById(R.id.spinGrave); |
100 grave = (Spinner) findViewById(R.id.spinGrave); |
106 .findViewById(R.id.btnTeam1)); |
119 .findViewById(R.id.btnTeam1)); |
107 hogName.add((EditText) team_creation_entry |
120 hogName.add((EditText) team_creation_entry |
108 .findViewById(R.id.txtTeam1)); |
121 .findViewById(R.id.txtTeam1)); |
109 } |
122 } |
110 |
123 |
111 SimpleAdapter sa = new SimpleAdapter(this, gravesData, |
124 grave.setAdapter(createMapSpinnerAdapter(gravesData)); |
|
125 flag.setAdapter(createMapSpinnerAdapter(flagsData)); |
|
126 difficulty.setAdapter(createMapSpinnerAdapter(typesData)); |
|
127 SpinnerAdapter hatAdapter = createMapSpinnerAdapter(hatsData); |
|
128 for (Spinner spin : hogHat) { |
|
129 spin.setAdapter(hatAdapter); |
|
130 } |
|
131 |
|
132 |
|
133 voice.setAdapter(createListSpinnerAdapter(voicesData)); |
|
134 voiceButton.setOnClickListener(voiceClicker); |
|
135 |
|
136 fort.setAdapter(createListSpinnerAdapter(fortsData)); |
|
137 fort.setOnItemSelectedListener(fortSelector); |
|
138 |
|
139 new Thread(this).start(); |
|
140 } |
|
141 |
|
142 private SpinnerAdapter createMapSpinnerAdapter(List<? extends Map<String, ?>> data) { |
|
143 SimpleAdapter sa = new SimpleAdapter(this, data, |
112 R.layout.spinner_textimg_entry, new String[] { "txt", "img" }, |
144 R.layout.spinner_textimg_entry, new String[] { "txt", "img" }, |
113 new int[] { R.id.spinner_txt, R.id.spinner_img }); |
145 new int[] { R.id.spinner_txt, R.id.spinner_img }); |
114 sa.setDropDownViewResource(R.layout.spinner_textimg_dropdown_entry); |
146 sa.setDropDownViewResource(R.layout.spinner_textimg_dropdown_entry); |
115 sa.setViewBinder(viewBinder); |
147 sa.setViewBinder(viewBinder); |
116 grave.setAdapter(sa); |
148 return sa; |
117 grave.setOnFocusChangeListener(focusser); |
149 } |
118 |
150 |
119 sa = new SimpleAdapter(this, flagsData, R.layout.spinner_textimg_entry, |
151 private SpinnerAdapter createListSpinnerAdapter(List<String> data) { |
120 new String[] { "txt", "img" }, new int[] { R.id.spinner_txt, |
152 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listview_item, data); |
121 R.id.spinner_img }); |
|
122 sa.setDropDownViewResource(R.layout.spinner_textimg_dropdown_entry); |
|
123 sa.setViewBinder(viewBinder); |
|
124 flag.setAdapter(sa); |
|
125 flag.setOnFocusChangeListener(focusser); |
|
126 |
|
127 sa = new SimpleAdapter(this, typesData, R.layout.spinner_textimg_entry, |
|
128 new String[] { "txt", "img" }, new int[] { R.id.spinner_txt, |
|
129 R.id.spinner_img }); |
|
130 sa.setDropDownViewResource(R.layout.spinner_textimg_dropdown_entry); |
|
131 difficulty.setAdapter(sa); |
|
132 difficulty.setOnFocusChangeListener(focusser); |
|
133 |
|
134 sa = new SimpleAdapter(this, hatsData, R.layout.spinner_textimg_entry, |
|
135 new String[] { "txt", "img" }, new int[] { R.id.spinner_txt, |
|
136 R.id.spinner_img }); |
|
137 sa.setDropDownViewResource(R.layout.spinner_textimg_dropdown_entry); |
|
138 sa.setViewBinder(viewBinder); |
|
139 for (Spinner spin : hogHat) { |
|
140 spin.setAdapter(sa); |
|
141 } |
|
142 |
|
143 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listview_item, voicesData); |
|
144 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
153 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
145 voice.setAdapter(adapter); |
154 return adapter; |
146 voice.setOnFocusChangeListener(focusser); |
155 } |
147 voiceButton.setOnClickListener(voiceClicker); |
156 |
148 |
|
149 adapter = new ArrayAdapter<String>(this, R.layout.listview_item, fortsData); |
|
150 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
|
151 fort.setAdapter(adapter); |
|
152 fort.setOnItemSelectedListener(fortSelector); |
|
153 fort.setOnFocusChangeListener(focusser); |
|
154 |
|
155 new Thread(this).start(); |
|
156 } |
|
157 |
|
158 public void run(){ |
157 public void run(){ |
159 final ArrayList<HashMap<String, ?>> gravesDataNew = FrontendDataUtils.getGraves(this); |
158 try { |
160 this.runOnUiThread(new Runnable(){ |
159 final ArrayList<HashMap<String, ?>> gravesDataNew = FrontendDataUtils.getGraves(this); |
161 public void run() { |
160 runOnUiThread(new Runnable(){ |
162 copy(gravesData, gravesDataNew); |
161 public void run() { |
163 ((SimpleAdapter)grave.getAdapter()).notifyDataSetChanged(); |
162 gravesData.addAll(gravesDataNew); |
164 } |
163 ((SimpleAdapter)grave.getAdapter()).notifyDataSetChanged(); |
165 }); |
164 } |
166 |
165 }); |
167 final ArrayList<HashMap<String, ?>> flagsDataNew = FrontendDataUtils.getFlags(this); |
166 |
168 this.runOnUiThread(new Runnable(){ |
167 final ArrayList<HashMap<String, ?>> flagsDataNew = FrontendDataUtils.getFlags(this); |
169 public void run() { |
168 runOnUiThread(new Runnable(){ |
170 copy(flagsData, flagsDataNew); |
169 public void run() { |
171 ((SimpleAdapter)flag.getAdapter()).notifyDataSetChanged(); |
170 flagsData.addAll(flagsDataNew); |
172 } |
171 ((SimpleAdapter)flag.getAdapter()).notifyDataSetChanged(); |
173 }); |
172 } |
174 |
173 }); |
175 final ArrayList<HashMap<String, ?>> typesDataNew = FrontendDataUtils.getTypes(this); |
174 |
176 this.runOnUiThread(new Runnable(){ |
175 final ArrayList<HashMap<String, ?>> typesDataNew = FrontendDataUtils.getTypes(this); |
177 public void run() { |
176 runOnUiThread(new Runnable(){ |
178 copy(typesData, typesDataNew); |
177 public void run() { |
179 ((SimpleAdapter)difficulty.getAdapter()).notifyDataSetChanged(); |
178 typesData.addAll(typesDataNew); |
180 } |
179 ((SimpleAdapter)difficulty.getAdapter()).notifyDataSetChanged(); |
181 }); |
180 } |
182 |
181 }); |
183 final ArrayList<HashMap<String, ?>> hatsDataNew = FrontendDataUtils.getHats(this); |
182 |
184 this.runOnUiThread(new Runnable(){ |
183 final ArrayList<HashMap<String, ?>> hatsDataNew = FrontendDataUtils.getHats(this); |
185 public void run() { |
184 runOnUiThread(new Runnable(){ |
186 copy(hatsData, hatsDataNew); |
185 public void run() { |
187 ((SimpleAdapter)hogHat.get(0).getAdapter()).notifyDataSetChanged(); |
186 hatsData.addAll(hatsDataNew); |
188 } |
187 ((SimpleAdapter)hogHat.get(0).getAdapter()).notifyDataSetChanged(); |
189 }); |
188 } |
190 |
189 }); |
191 final ArrayList<String> voicesDataNew = FrontendDataUtils.getVoices(this); |
190 |
192 this.runOnUiThread(new Runnable(){ |
191 final ArrayList<String> voicesDataNew = FrontendDataUtils.getVoices(this); |
193 public void run() { |
192 runOnUiThread(new Runnable(){ |
194 copy(voicesData, voicesDataNew); |
193 public void run() { |
195 ((ArrayAdapter<String>)voice.getAdapter()).notifyDataSetChanged(); |
194 voicesData.addAll(voicesDataNew); |
196 } |
195 ((ArrayAdapter<?>)voice.getAdapter()).notifyDataSetChanged(); |
197 }); |
196 } |
198 |
197 }); |
199 final ArrayList<String> fortsDataNew = FrontendDataUtils.getForts(this); |
198 |
200 this.runOnUiThread(new Runnable(){ |
199 final ArrayList<String> fortsDataNew = FrontendDataUtils.getForts(this); |
201 public void run() { |
200 runOnUiThread(new Runnable(){ |
202 copy(fortsData, fortsDataNew); |
201 public void run() { |
203 ((ArrayAdapter<String>)fort.getAdapter()).notifyDataSetChanged(); |
202 fortsData.addAll(fortsDataNew); |
204 } |
203 ((ArrayAdapter<?>)fort.getAdapter()).notifyDataSetChanged(); |
205 }); |
204 } |
206 } |
205 }); |
207 |
206 |
208 private static <T> void copy(List<T> dest, List<T> src){ |
207 if(existingTeamName!=null) { |
209 for(T t: src) dest.add(t); |
208 final Team loadedTeam = Team.load(Team.getTeamfileByName(getApplicationContext(), existingTeamName)); |
210 } |
209 if(loadedTeam==null) { |
211 |
210 existingTeamName = null; |
|
211 } else { |
|
212 runOnUiThread(new Runnable(){ |
|
213 public void run() { |
|
214 setTeamValues(loadedTeam); |
|
215 } |
|
216 }); |
|
217 } |
|
218 } |
|
219 runOnUiThread(new Runnable(){ |
|
220 public void run() { |
|
221 initComplete = true; |
|
222 } |
|
223 }); |
|
224 } catch(FileNotFoundException e) { |
|
225 this.runOnUiThread(new Runnable(){ |
|
226 public void run() { |
|
227 Toast.makeText(getApplicationContext(), R.string.error_missing_sdcard_or_files, Toast.LENGTH_LONG).show(); |
|
228 finish(); |
|
229 } |
|
230 }); |
|
231 } |
|
232 } |
|
233 |
212 public void onDestroy() { |
234 public void onDestroy() { |
213 super.onDestroy(); |
235 super.onDestroy(); |
214 if (mp != null) { |
236 if (mp != null) { |
215 mp.release(); |
237 mp.release(); |
216 mp = null; |
238 mp = null; |
217 } |
239 } |
218 } |
240 } |
219 |
241 |
220 private OnFocusChangeListener focusser = new OnFocusChangeListener() { |
242 @Override |
221 public void onFocusChange(View v, boolean hasFocus) { |
243 protected void onSaveInstanceState(Bundle outState) { |
222 settingsChanged = true; |
244 super.onSaveInstanceState(outState); |
223 } |
245 outState.putString(PARAMETER_EXISTING_TEAMNAME, existingTeamName); |
224 |
246 } |
|
247 |
|
248 public void onBackPressed() { |
|
249 if(initComplete) { |
|
250 saveTeam(); |
|
251 } |
|
252 setResult(RESULT_OK); |
|
253 super.onBackPressed(); |
|
254 } |
|
255 |
|
256 private int getDifficultyLevelFromText(String text) { |
|
257 if (text.equals(getString(R.string.human))) { |
|
258 return 0; |
|
259 } else if (text.equals(getString(R.string.bot5))) { |
|
260 return 1; |
|
261 } else if (text.equals(getString(R.string.bot4))) { |
|
262 return 2; |
|
263 } else if (text.equals(getString(R.string.bot3))) { |
|
264 return 3; |
|
265 } else if (text.equals(getString(R.string.bot2))) { |
|
266 return 4; |
|
267 } else { |
|
268 return 5; |
|
269 } |
|
270 } |
|
271 |
|
272 private String getTxtFromDifficulty(int level) { |
|
273 switch(level) { |
|
274 case 0: return getString(R.string.human); |
|
275 case 1: return getString(R.string.bot5); |
|
276 case 2: return getString(R.string.bot4); |
|
277 case 3: return getString(R.string.bot3); |
|
278 case 4: return getString(R.string.bot2); |
|
279 default: return getString(R.string.bot1); |
|
280 } |
|
281 } |
|
282 |
|
283 private void saveTeam() { |
|
284 String teamName = name.getText().toString(); |
|
285 String teamFlag = (String)((Map<String, Object>) flag.getSelectedItem()).get("txt"); |
|
286 String teamFort = fort.getSelectedItem().toString(); |
|
287 String teamGrave = (String)((Map<String, Object>) grave.getSelectedItem()).get("txt"); |
|
288 String teamVoice = voice.getSelectedItem().toString(); |
|
289 String levelString = (String)((Map<String, Object>) difficulty.getSelectedItem()).get("txt"); |
|
290 int levelInt = getDifficultyLevelFromText(levelString); |
|
291 |
|
292 List<Hog> hogs = new ArrayList<Hog>(); |
|
293 for (int i = 0; i < hogName.size(); i++) { |
|
294 String name = hogName.get(i).getText().toString(); |
|
295 String hat = ((Map<String, Object>) hogHat.get(i).getSelectedItem()).get("txt").toString(); |
|
296 hogs.add(new Hog(name, hat, levelInt)); |
|
297 } |
|
298 |
|
299 Team team = new Team(teamName, teamGrave, teamFlag, teamVoice, teamFort, hogs); |
|
300 File teamsDir = new File(getFilesDir(), Team.DIRECTORY_TEAMS); |
|
301 if (!teamsDir.exists()) teamsDir.mkdir(); |
|
302 |
|
303 File newFile = Team.getTeamfileByName(this, teamName); |
|
304 File oldFile = null; |
|
305 if(existingTeamName != null) { |
|
306 oldFile = Team.getTeamfileByName(this, existingTeamName); |
|
307 } |
|
308 try { |
|
309 team.save(newFile); |
|
310 Toast.makeText(TeamCreatorActivity.this, R.string.saved, Toast.LENGTH_SHORT).show(); |
|
311 // If the team was renamed, delete the old file. |
|
312 if(oldFile != null && oldFile.isFile() && !oldFile.equals(newFile)) { |
|
313 oldFile.delete(); |
|
314 } |
|
315 existingTeamName = teamName; |
|
316 } catch(IOException e) { |
|
317 Toast.makeText(getApplicationContext(), R.string.error_save_failed, Toast.LENGTH_SHORT).show(); |
|
318 } |
225 }; |
319 }; |
226 |
320 |
227 public void onBackPressed() { |
|
228 onFinishing(); |
|
229 super.onBackPressed(); |
|
230 |
|
231 } |
|
232 |
|
233 private OnClickListener backClicker = new OnClickListener() { |
|
234 public void onClick(View v) { |
|
235 onFinishing(); |
|
236 finish(); |
|
237 } |
|
238 }; |
|
239 |
|
240 private void onFinishing() { |
|
241 if (settingsChanged) { |
|
242 setResult(RESULT_OK); |
|
243 } else { |
|
244 setResult(RESULT_CANCELED); |
|
245 } |
|
246 } |
|
247 |
|
248 private OnClickListener saveClicker = new OnClickListener() { |
|
249 public void onClick(View v) { |
|
250 String teamName = name.getText().toString(); |
|
251 String teamFlag = (String)((Map<String, Object>) flag.getSelectedItem()).get("txt"); |
|
252 String teamFort = fort.getSelectedItem().toString(); |
|
253 String teamGrave = (String)((Map<String, Object>) grave.getSelectedItem()).get("txt"); |
|
254 String teamVoice = voice.getSelectedItem().toString(); |
|
255 String levelString = (String)((Map<String, Object>) difficulty.getSelectedItem()).get("txt"); |
|
256 int levelInt; |
|
257 if (levelString.equals(getString(R.string.human))) { |
|
258 levelInt = 0; |
|
259 } else if (levelString.equals(getString(R.string.bot5))) { |
|
260 levelInt = 1; |
|
261 } else if (levelString.equals(getString(R.string.bot4))) { |
|
262 levelInt = 2; |
|
263 } else if (levelString.equals(getString(R.string.bot3))) { |
|
264 levelInt = 3; |
|
265 } else if (levelString.equals(getString(R.string.bot2))) { |
|
266 levelInt = 4; |
|
267 } else { |
|
268 levelInt = 5; |
|
269 } |
|
270 |
|
271 List<Hog> hogs = new ArrayList<Hog>(); |
|
272 for (int i = 0; i < hogName.size(); i++) { |
|
273 String name = hogName.get(i).getText().toString(); |
|
274 String hat = ((Map<String, Object>) hogHat.get(i).getSelectedItem()).get("txt").toString(); |
|
275 hogs.add(new Hog(name, hat, levelInt)); |
|
276 } |
|
277 |
|
278 Team team = new Team(teamName, teamGrave, teamFlag, teamVoice, teamFort, hogs); |
|
279 File teamsDir = new File(getFilesDir(), Team.DIRECTORY_TEAMS); |
|
280 if (!teamsDir.exists()) teamsDir.mkdir(); |
|
281 if(fileName == null){ |
|
282 fileName = createNewFilename(Utils.replaceBadChars(team.name)); |
|
283 } |
|
284 try { |
|
285 team.save(new File(teamsDir, fileName)); |
|
286 Toast.makeText(TeamCreatorActivity.this, R.string.saved, Toast.LENGTH_SHORT).show(); |
|
287 saved = true; |
|
288 } catch(IOException e) { |
|
289 Toast.makeText(getApplicationContext(), R.string.error_save_failed, Toast.LENGTH_SHORT).show(); |
|
290 } |
|
291 } |
|
292 }; |
|
293 |
|
294 private String createNewFilename(String suggestedName){ |
|
295 File f = new File(getFilesDir(), suggestedName); |
|
296 if(f.exists()){ |
|
297 return createNewFilename(suggestedName + (int)(Math.random()*10)); |
|
298 } else { |
|
299 return suggestedName + (int)(Math.random()*10); |
|
300 } |
|
301 } |
|
302 |
|
303 private OnItemSelectedListener fortSelector = new OnItemSelectedListener() { |
321 private OnItemSelectedListener fortSelector = new OnItemSelectedListener() { |
304 public void onItemSelected(AdapterView<?> arg0, View arg1, |
322 public void onItemSelected(AdapterView<?> arg0, View arg1, |
305 int position, long arg3) { |
323 int position, long arg3) { |
306 settingsChanged = true; |
|
307 String fortName = (String) arg0.getAdapter().getItem(position); |
324 String fortName = (String) arg0.getAdapter().getItem(position); |
308 Drawable fortIconDrawable = Drawable.createFromPath(Utils |
325 Drawable fortIconDrawable = Drawable.createFromPath(Utils |
309 .getDataPath(TeamCreatorActivity.this) |
326 .getDataPath(TeamCreatorActivity.this) |
310 + "Forts/" |
327 + "Forts/" |
311 + fortName + "L.png"); |
328 + fortName + "L.png"); |
349 e.printStackTrace(); |
366 e.printStackTrace(); |
350 } |
367 } |
351 } |
368 } |
352 }; |
369 }; |
353 |
370 |
|
371 @SuppressWarnings("unchecked") |
354 private void setTeamValues(Team t){ |
372 private void setTeamValues(Team t){ |
355 |
373 if (t == null) { |
356 if (t != null) { |
374 return; |
|
375 } |
|
376 |
|
377 try { |
357 name.setText(t.name); |
378 name.setText(t.name); |
358 int position = ((ArrayAdapter<String>) voice.getAdapter()).getPosition(t.voice); |
379 voice.setSelection(findPosition((ArrayAdapter<String>) voice.getAdapter(), t.voice)); |
359 voice.setSelection(position); |
380 fort.setSelection(findPosition((ArrayAdapter<String>) fort.getAdapter(), t.fort)); |
360 |
381 difficulty.setSelection(findPosition(typesData, getTxtFromDifficulty(t.hogs.get(0).level))); // TODO store actual difficulty int in typesData |
361 position = ((ArrayAdapter<String>) fort.getAdapter()).getPosition(t.fort); |
382 grave.setSelection(findPosition(gravesData, t.grave)); |
362 fort.setSelection(position); |
383 flag.setSelection(findPosition(flagsData, t.flag)); |
363 |
384 |
364 position = 0; |
385 for (int i = 0; i < Team.HEDGEHOGS_PER_TEAM; i++) { |
365 for (HashMap<String, ?> hashmap : typesData) { |
386 hogHat.get(i).setSelection(findPosition(hatsData, t.hogs.get(i).hat)); |
366 if (t.hogs.get(0) != null && hashmap.get("txt").equals(t.hogs.get(0).level)) { |
|
367 difficulty.setSelection(position); |
|
368 break; |
|
369 } |
|
370 } |
|
371 |
|
372 position = 0; |
|
373 for (HashMap<String, ?> hashmap : gravesData) { |
|
374 if (hashmap.get("txt").equals(t.grave)) { |
|
375 grave.setSelection(position); |
|
376 break; |
|
377 } |
|
378 } |
|
379 |
|
380 position = 0; |
|
381 for (HashMap<String, ?> hashmap : typesData) { |
|
382 if (hashmap.get("txt").equals(t.flag)) { |
|
383 flag.setSelection(position); |
|
384 break; |
|
385 } |
|
386 } |
|
387 |
|
388 for (int i = 0; i < Team.maxNumberOfHogs; i++) { |
|
389 position = 0; |
|
390 for (HashMap<String, ?> hashmap : hatsData) { |
|
391 if (hashmap.get("txt").equals(t.hogs.get(i).hat)) { |
|
392 hogHat.get(i).setSelection(position); |
|
393 } |
|
394 } |
|
395 |
|
396 hogName.get(i).setText(t.hogs.get(i).name); |
387 hogName.get(i).setText(t.hogs.get(i).name); |
397 } |
388 } |
398 //this.fileName = t.file; |
389 } catch(NoSuchElementException e) { |
399 } |
390 Toast.makeText(getApplicationContext(), R.string.error_team_attribute_not_found, Toast.LENGTH_LONG).show(); |
400 } |
391 finish(); |
401 |
392 } |
|
393 } |
|
394 |
|
395 int findPosition(ArrayAdapter<String> adapter, String key) throws NoSuchElementException { |
|
396 int position = adapter.getPosition(key); |
|
397 if(position<0) { |
|
398 throw new NoSuchElementException(); |
|
399 } |
|
400 return position; |
|
401 } |
|
402 |
|
403 int findPosition(List<? extends Map<String, ?>> data, String txtValue) throws NoSuchElementException { |
|
404 int position = 0; |
|
405 for (Map<String, ?> map : data) { |
|
406 if (map.get("txt").equals(txtValue)) { |
|
407 return position; |
|
408 } |
|
409 position++; |
|
410 } |
|
411 throw new NoSuchElementException(); |
|
412 } |
402 |
413 |
403 private SimpleAdapter.ViewBinder viewBinder = new SimpleAdapter.ViewBinder() { |
414 private SimpleAdapter.ViewBinder viewBinder = new SimpleAdapter.ViewBinder() { |
404 |
415 |
405 public boolean setViewValue(View view, Object data, |
416 public boolean setViewValue(View view, Object data, |
406 String textRepresentation) { |
417 String textRepresentation) { |