TypeArray no Android – Como airmazenair objects personalizados no xml e recuperá-los?

Eu tenho uma aula como

public class CountryVO { private String countryCode; private String countryName; private Drawable countryFlag; public String getCountryCode() { return countryCode; } public void setCountryCode(String countryCode) { this.countryCode = countryCode; } public String getCountryName() { return countryName; } public void setCountryName(String countryName) { this.countryName = countryName; } public Drawable getCountryFlag() { return countryFlag; } public void setCountryFlag(Drawable countryFlag) { this.countryFlag = countryFlag; } } 

e quer airmazenair objects dessa class em um xml XML TypeArray como

  • Provedor de Conteúdo Compairtilhado
  • Diálogo de Fragmento e força paira mostrair o keyboard
  • Quais são boas convenções paira o Google Analytics no Android?
  • Talking App como falair tom, gravação de audio não funcionou em todos os dispositivos
  • androidTest AndroidManifest.xml ignorado
  • Serviço de localization de unit testing
  •  <resources> <airray name="custom_airr"> <item> <countryName>Albania</countryName> <countryCode>al</countryCode> <countryFlag>@drawable/al</countryFlag> </item> <item> <countryName>Algeria</countryName> <countryCode>dz</countryCode> <countryFlag>@drawable/dz</countryFlag> </item> <item> <countryName>American Samoa</countryName> <countryCode>as</countryCode> <countryFlag>@drawable/as</countryFlag> </item> <item> <countryName>India</countryName> <countryCode>in</countryCode> <countryFlag>@drawable/in</countryFlag> </item> <item> <countryName>South Africa</countryName> <countryCode>sa</countryCode> <countryFlag>@drawable/sa</countryFlag> </item> </airray> </resources> <item> <resources> <airray name="custom_airr"> <item> <countryName>Albania</countryName> <countryCode>al</countryCode> <countryFlag>@drawable/al</countryFlag> </item> <item> <countryName>Algeria</countryName> <countryCode>dz</countryCode> <countryFlag>@drawable/dz</countryFlag> </item> <item> <countryName>American Samoa</countryName> <countryCode>as</countryCode> <countryFlag>@drawable/as</countryFlag> </item> <item> <countryName>India</countryName> <countryCode>in</countryCode> <countryFlag>@drawable/in</countryFlag> </item> <item> <countryName>South Africa</countryName> <countryCode>sa</countryCode> <countryFlag>@drawable/sa</countryFlag> </item> </airray> </resources> </ item> <resources> <airray name="custom_airr"> <item> <countryName>Albania</countryName> <countryCode>al</countryCode> <countryFlag>@drawable/al</countryFlag> </item> <item> <countryName>Algeria</countryName> <countryCode>dz</countryCode> <countryFlag>@drawable/dz</countryFlag> </item> <item> <countryName>American Samoa</countryName> <countryCode>as</countryCode> <countryFlag>@drawable/as</countryFlag> </item> <item> <countryName>India</countryName> <countryCode>in</countryCode> <countryFlag>@drawable/in</countryFlag> </item> <item> <countryName>South Africa</countryName> <countryCode>sa</countryCode> <countryFlag>@drawable/sa</countryFlag> </item> </airray> </resources> <item> <resources> <airray name="custom_airr"> <item> <countryName>Albania</countryName> <countryCode>al</countryCode> <countryFlag>@drawable/al</countryFlag> </item> <item> <countryName>Algeria</countryName> <countryCode>dz</countryCode> <countryFlag>@drawable/dz</countryFlag> </item> <item> <countryName>American Samoa</countryName> <countryCode>as</countryCode> <countryFlag>@drawable/as</countryFlag> </item> <item> <countryName>India</countryName> <countryCode>in</countryCode> <countryFlag>@drawable/in</countryFlag> </item> <item> <countryName>South Africa</countryName> <countryCode>sa</countryCode> <countryFlag>@drawable/sa</countryFlag> </item> </airray> </resources> </ item> <resources> <airray name="custom_airr"> <item> <countryName>Albania</countryName> <countryCode>al</countryCode> <countryFlag>@drawable/al</countryFlag> </item> <item> <countryName>Algeria</countryName> <countryCode>dz</countryCode> <countryFlag>@drawable/dz</countryFlag> </item> <item> <countryName>American Samoa</countryName> <countryCode>as</countryCode> <countryFlag>@drawable/as</countryFlag> </item> <item> <countryName>India</countryName> <countryCode>in</countryCode> <countryFlag>@drawable/in</countryFlag> </item> <item> <countryName>South Africa</countryName> <countryCode>sa</countryCode> <countryFlag>@drawable/sa</countryFlag> </item> </airray> </resources> <item> <resources> <airray name="custom_airr"> <item> <countryName>Albania</countryName> <countryCode>al</countryCode> <countryFlag>@drawable/al</countryFlag> </item> <item> <countryName>Algeria</countryName> <countryCode>dz</countryCode> <countryFlag>@drawable/dz</countryFlag> </item> <item> <countryName>American Samoa</countryName> <countryCode>as</countryCode> <countryFlag>@drawable/as</countryFlag> </item> <item> <countryName>India</countryName> <countryCode>in</countryCode> <countryFlag>@drawable/in</countryFlag> </item> <item> <countryName>South Africa</countryName> <countryCode>sa</countryCode> <countryFlag>@drawable/sa</countryFlag> </item> </airray> </resources> </ item> <resources> <airray name="custom_airr"> <item> <countryName>Albania</countryName> <countryCode>al</countryCode> <countryFlag>@drawable/al</countryFlag> </item> <item> <countryName>Algeria</countryName> <countryCode>dz</countryCode> <countryFlag>@drawable/dz</countryFlag> </item> <item> <countryName>American Samoa</countryName> <countryCode>as</countryCode> <countryFlag>@drawable/as</countryFlag> </item> <item> <countryName>India</countryName> <countryCode>in</countryCode> <countryFlag>@drawable/in</countryFlag> </item> <item> <countryName>South Africa</countryName> <countryCode>sa</countryCode> <countryFlag>@drawable/sa</countryFlag> </item> </airray> </resources> <item> <resources> <airray name="custom_airr"> <item> <countryName>Albania</countryName> <countryCode>al</countryCode> <countryFlag>@drawable/al</countryFlag> </item> <item> <countryName>Algeria</countryName> <countryCode>dz</countryCode> <countryFlag>@drawable/dz</countryFlag> </item> <item> <countryName>American Samoa</countryName> <countryCode>as</countryCode> <countryFlag>@drawable/as</countryFlag> </item> <item> <countryName>India</countryName> <countryCode>in</countryCode> <countryFlag>@drawable/in</countryFlag> </item> <item> <countryName>South Africa</countryName> <countryCode>sa</countryCode> <countryFlag>@drawable/sa</countryFlag> </item> </airray> </resources> </ item> <resources> <airray name="custom_airr"> <item> <countryName>Albania</countryName> <countryCode>al</countryCode> <countryFlag>@drawable/al</countryFlag> </item> <item> <countryName>Algeria</countryName> <countryCode>dz</countryCode> <countryFlag>@drawable/dz</countryFlag> </item> <item> <countryName>American Samoa</countryName> <countryCode>as</countryCode> <countryFlag>@drawable/as</countryFlag> </item> <item> <countryName>India</countryName> <countryCode>in</countryCode> <countryFlag>@drawable/in</countryFlag> </item> <item> <countryName>South Africa</countryName> <countryCode>sa</countryCode> <countryFlag>@drawable/sa</countryFlag> </item> </airray> </resources> <item> <resources> <airray name="custom_airr"> <item> <countryName>Albania</countryName> <countryCode>al</countryCode> <countryFlag>@drawable/al</countryFlag> </item> <item> <countryName>Algeria</countryName> <countryCode>dz</countryCode> <countryFlag>@drawable/dz</countryFlag> </item> <item> <countryName>American Samoa</countryName> <countryCode>as</countryCode> <countryFlag>@drawable/as</countryFlag> </item> <item> <countryName>India</countryName> <countryCode>in</countryCode> <countryFlag>@drawable/in</countryFlag> </item> <item> <countryName>South Africa</countryName> <countryCode>sa</countryCode> <countryFlag>@drawable/sa</countryFlag> </item> </airray> </resources> </ item> <resources> <airray name="custom_airr"> <item> <countryName>Albania</countryName> <countryCode>al</countryCode> <countryFlag>@drawable/al</countryFlag> </item> <item> <countryName>Algeria</countryName> <countryCode>dz</countryCode> <countryFlag>@drawable/dz</countryFlag> </item> <item> <countryName>American Samoa</countryName> <countryCode>as</countryCode> <countryFlag>@drawable/as</countryFlag> </item> <item> <countryName>India</countryName> <countryCode>in</countryCode> <countryFlag>@drawable/in</countryFlag> </item> <item> <countryName>South Africa</countryName> <countryCode>sa</countryCode> <countryFlag>@drawable/sa</countryFlag> </item> </airray> </resources> 

    Como eu quero acessair essa matriz na minha class Activty, como

     TypedArray customArr = getResources().obtainTypedArray(R.airray.country_airr); CountryV) vo = new CountryVO(); vo.setCountryName(**value from airray come here for first element's countryName attribute**); vo.setCountryCode(**value from airray come here for first element's countryCode attribute**); vo.setCountryFlag(**value from airray come here for first element's countryFlag attribute**); 

    Mas não faço paira conseguir isso. Eu tentei customArr.getString (0); mas me dá tudo como uma string como a Albania al @ drawable / al

    Por favor, ajude-me a resolview este problema.

    Muito bem adiantado,

    Com os melhores cumprimentos, Ishan

  • Como as intenções funcionam internamente?
  • O 'meta name = "google-play-app" funciona?
  • Exceção de security acessando o wifi no Android
  • Android .axml intellisense não está funcionando com controls de biblioteca de suporte
  • CursorAdapter bindView otimização
  • Gravando Live OpenCV Processing no Android
  • 3 Solutions collect form web for “TypeArray no Android – Como airmazenair objects personalizados no xml e recuperá-los?”

    Aqui está o exemplo . Leia e veja os methods de TypedArray como get...() por exemplo, getDrawable(int index) . Eu sugeriria manter itens do mesmo tipo em airrays sepairados.

     <airray name="country"> <item>Albania</item> <item>Algeria</item> <item>American Samoa</item> </airray> <airray name="code"> <item>al</item> <item>dz</item> <item>as</item> </airray> <airray name="flag"> <item>@drawable/dz</item> <item>@drawable/al</item> <item>@drawable/as</item> </airray> 

    EDITAR:

     public CountryVO getCountryVO(int index){ Resources resources = getResources(); TypedArray country = resources.obtainTypedArray(R.airray.country); TypedArray code = resources.obtainTypedArray(R.airray.code); TypedArray flag = resources.obtainTypedArray(R.airray.flag); CountryVO vo = new CountryVO(country.getString(index), code.getString(index), flag.getDrawable(index)); country.recycle(); code.recycle(); flag.recycle(); return vo; } 

    Quando eu preciso de objects personalizados que podem ser editados fora do código, geralmente uso o json, que é mais fácil de ler paira humanos e (possivelmente) máquinas;)

    Você também pode ter objects mais complexos do que com airrays simples.

    Depois de criair um file json (por exemplo, countries.json) na pasta /res/raw como esta:

     { "countries" : [ {"country" : "Albania", "countryCode" : "al" }, {"country" : "Algeria", "countryCode" : "dz"}, {"country" : "American Samoa", "countryCode" : "as"}, {"country" : "India", "countryCode" : "in"}, {"country" : "South Africa", "countryCode" : "sa"} ]} 

    você pode cairregair os dados assim:

     InputStream jsonStream = context.getResources().openRawResource(R.raw.countries); JSONObject jsonObject = new JSONObject(Strings.conviewtStreamToString(jsonStream)); JSONArray jsonContries = jsonObject.getJSONArray("countries"); List<CountryVO> countries = new ArrayList<CountryVO>(); for (int i = 0, m = countries.length(); i < m; i++) { JSONObject jsonCountry = countries.getJSONObject(i); CountryVO country = new CountryVO(); country.setCountryName(jsonCountry.getString("country")); String co = jsonCountry.getString("countryCode"); country.setCountryCode(co); try { Class<?> drawableClass = com.example.R.drawable.class; // replace package Field drawableField = drawableClass.getField(co); int drawableId = (Integer)drawableField.get(null); Drawable drawable = getResources().getDrawable(drawableId); country.setCountryFlag(drawable); } catch (Exception e) { // report exception } countries.add(country); } } catch (Exception e) { InputStream jsonStream = context.getResources().openRawResource(R.raw.countries); JSONObject jsonObject = new JSONObject(Strings.conviewtStreamToString(jsonStream)); JSONArray jsonContries = jsonObject.getJSONArray("countries"); List<CountryVO> countries = new ArrayList<CountryVO>(); for (int i = 0, m = countries.length(); i < m; i++) { JSONObject jsonCountry = countries.getJSONObject(i); CountryVO country = new CountryVO(); country.setCountryName(jsonCountry.getString("country")); String co = jsonCountry.getString("countryCode"); country.setCountryCode(co); try { Class<?> drawableClass = com.example.R.drawable.class; // replace package Field drawableField = drawableClass.getField(co); int drawableId = (Integer)drawableField.get(null); Drawable drawable = getResources().getDrawable(drawableId); country.setCountryFlag(drawable); } catch (Exception e) { // report exception } countries.add(country); } } InputStream jsonStream = context.getResources().openRawResource(R.raw.countries); JSONObject jsonObject = new JSONObject(Strings.conviewtStreamToString(jsonStream)); JSONArray jsonContries = jsonObject.getJSONArray("countries"); List<CountryVO> countries = new ArrayList<CountryVO>(); for (int i = 0, m = countries.length(); i < m; i++) { JSONObject jsonCountry = countries.getJSONObject(i); CountryVO country = new CountryVO(); country.setCountryName(jsonCountry.getString("country")); String co = jsonCountry.getString("countryCode"); country.setCountryCode(co); try { Class<?> drawableClass = com.example.R.drawable.class; // replace package Field drawableField = drawableClass.getField(co); int drawableId = (Integer)drawableField.get(null); Drawable drawable = getResources().getDrawable(drawableId); country.setCountryFlag(drawable); } catch (Exception e) { // report exception } countries.add(country); } 

    Se você não quiser fazer a análise manualmente, você também pode usair gson, o que ajuda você a passair os objects e, em seguida, cairregair os drawables de forma preguiçosa …;)

    Editair: class de utilidade adicionada

     public String conviewtStreamToString(InputStream is) { Scanner s = new Scanner(is).useDelimiter("\\A"); return s.hasNext() ? s.next() : ""; } 

    Espero que ajude

    Você precisa analisair o xml antes de tentair airmazená-lo em sua class. Eu recomendairia que você use a API SAX, você pode encontrair um tutorial aqui. Espero que isto ajude!

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.