vinaygaba / Ultimate-String-Array-List

🧵 A comprehensive list of string-arrays that you may need on a daily basis when developing an Android app
155 stars 73 forks source link

Optimization: List should contain ID, name, code and other details #4

Open PareshMayani opened 9 years ago

PareshMayani commented 9 years ago

This point is about to optimize the code you are using. Instead of referring to two or more different lists separately, there should be a single list contains all the items and each item should contain code, name, ID, language, etc. details (i.e. required details)

Yes the current solution which is like preparing an arraylist of string directly is best, but it's a good solution when you are getting values from different array lists, because by change if someone or you delete an item from a particular list then items count won't be matched and so loop break!

Current:

String[] us_state_codes = getResources().getStringArray(R.array.us_state_codes);
String[] us_state = getResources().getStringArray(R.array.us_states);
final Map<String, String> m = new HashMap<String, String>();

for(int i=0;i<us_state_codes.length();i++){
  m.put(us_state_codes[i],us_states[i]);
}

Optimized solution:

<resources>    
<string-array name="countries_array">        
  <item>
    <name>Bahrain</name>
    <code>12345</code>
  </item>
...  
</string-array>
</resources>

And solution should be like parsing XML and preparing list of objects. More details: http://stackoverflow.com/questions/9568700/android-resource-an-array-to-store-country-name-country-code

vinaygaba commented 9 years ago

@PareshMayani I quite like this idea. Honestly I didn't know this was possible to do. I will make these changes for sure. Might take some time but will get to it. Thanks for sharing!

PareshMayani commented 9 years ago

Keep the current lists as same, just add a single list with all the details belonging to the particular country!

AkshayChordiya commented 8 years ago

@vinaygaba @PareshMayani The problem to go with the above pattern is, it removes the simplicity of using string-array and switches to use of XML Parsers.