ExpandableList array resource implementation questions - android

I am not exactly clear on how to implement ExpandableList as array resource instead of hard-coded array (by replacing an existing hard-coded array with XML layout). Here is my strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">adaptor_ExpandableList1</string>
<string name="expandable_list_sample_action">Sample action</string>
<string-array name="Groups">
<item>People Names</item>
<item>Dog Names</item>
<item>Cat Names</item>
<item>Fish Names</item>
</string-array>
<string-array name="People Names">
<item>Arnold</item>
<item>Barry</item>
<item>Chuck</item>
<item>David</item>
</string-array>
<string-array name="Dog Names">
<item>Ace</item>
<item>Bandit</item>
<item>Cha-Cha</item>
<item>Deuce</item>
</string-array>
<string-array name="Cat Names">
<item>Fluffy</item>
<item>Snuggles</item>
</string-array>
<string-array name="Fish Names">
<item>Goldy</item>
<item>Bubbles</item>
</string-array>
</resources>
Here is what I changed in the Java file:
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
// Sample data set. children[i] contains the children (String[]) for groups[i].
private String[] groups = getResources().getStringArray(R.array.Groups);
private String[][] children = {getResources().getStringArray(R.array.People Names),
getResources().getStringArray(R.array.Dog Names),
getResources().getStringArray(R.array.Cat Names),
getResources().getStringArray(R.array.Fish Names)
};
Of course R.array.xxx Names does not work. So how can I correct the problem when there are statements in R.java such as this: public static final int Cat Names=0x7f040003;.
There must be a rule for this but I don't know it yet.
Browsing through numerous examples, I think I also need to have group and child layout files and perhaps a main layout file (I think I know how to implement them if I need to). Is this correct?
Thanks in advance for your help!

you have space in the name of string array
remove space from "people names" and other string array names.

Related

Could android store drawable ids like an integer-array?

I want a drawable id array of integer values which I can store like an integer-array in res/values/XXX.xml by using integer-array tag. Below is integer-array declared in strings.xml
<integer-array name="icons">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</integer-array>
But I want to store drawable image ids like #drawable/someImage as an integer array in xml.
OR Is there any alternatives to store drawable integer ids as an integer array in xml.
I think TypedArray is what you are looking for. I have samples using it. If you are interested, take a look at codes below:
First, integer-array in res/values/arrays.xml:
<integer-array name="frag_home_ids">
<item>#drawable/frag_home_credit_return_money</item>
<item>#drawable/frag_home_transfer</item>
<item>#drawable/frag_home_balance</item>
<item>#drawable/frag_home_charge</item>
<item>#drawable/frag_home_finance_cdd</item>
<item>#drawable/frag_home_finance_ybjr</item>
<item>#drawable/frag_home_more</item>
</integer-array>
Second, get resource integer values programmatically:
TypedArray tArray = getResources().obtainTypedArray(
R.array.frag_home_ids);
int count = tArray.length();
int[] ids = new int[count];
for (int i = 0; i < ids.length; i++) {
ids[i] = tArray.getResourceId(i, 0);
}
//Recycles the TypedArray, to be re-used by a later caller.
//After calling this function you must not ever touch the typed array again.
tArray.recycle();
Third, call the integer values like this:
holder.iv.setImageResource(ids[position]);
Of course, you can get integer values of string, color, integer, layout, menu...in this way.
I hope these codes will inspire you.
Take a look at documentation, specifically More Resource Types article. Quote:
Typed Array
A TypedArray defined in XML. You can use this to create an array of other resources, such as drawables.
EXAMPLE:
XML file saved at res/values/arrays.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="icons">
<item>#drawable/home</item>
<item>#drawable/settings</item>
<item>#drawable/logout</item>
</array>
<array name="colors">
<item>#FFFF0000</item>
<item>#FF00FF00</item>
<item>#FF0000FF</item>
</array>
</resources>
You can use a string array.
Extract:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="media_names">
<item>Big Buck Bunny</item>
<item>Elephants Dream</item>
<item>Sintel</item>
<item>Tears of Steel</item>
</string-array>
<string-array name="media_uris">
<item>http://archive.org/download/BigBuckBunny_328/BigBuckBunny_512kb.mp4</item>
<item>http://archive.org/download/ElephantsDream_277/elephant_dreams_640_512kb.mp4</item>
<item>http://archive.org/download/Sintel/sintel-2048-stereo_512kb.mp4</item>
<item>http://archive.org/download/Tears-of-Steel/tears_of_steel_720p.mp4</item>
</string-array>
</resources>
What is it that you want to achieve, I cannot 100% tell you if this is the best choice for you.

Best way to use a static array

In my little app, I have a long list of data. Before, I used an editor to let the user enter the data - but now I would like to put the data static in program code/string list value Folder.
What is the best way to achieve this?
If you already know the values inside array you can crate string array inside strings.xml file which is present under values.
example:
XML file saved at res/values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="sports_array">
<item>Football</item>
<item>Cricket</item>
<item>Hockey</item>
<item>Tennis</item>
</string-array>
</resources>
This application code retrieves a string array:
Resources res = getResources();
String[] sportlist = res.getStringArray(R.array.sports_array);
so now your sportlist will contain all the items specified in sports_array which is declared inside strings.xml
for more information pls see
http://developer.android.com/guide/topics/resources/string-resource.html

How to get the count of array from resources file?

I have multiple arrays in resources like this:
<resources>
<string-array name="myArray1">
<item>String1</item>
<item>String2</item>
<item>String3</item>
</string-array>
<string-array name="myArray2">
<item>String1</item>
<item>String2</item>
<item>String3</item>
</string-array>
<string-array name="myArray3">
<item>String1</item>
<item>String2</item>
<item>String3</item>
</string-array>
</resources>
Now how to get the count of these array dynamically?currently I have 3 arrays.
I can get the particular array items like this
String[] resourceString =getResources().getStringArray(R.array.myArray);
But how to get the count?
int length =getResources().getStringArray(R.array.myArray).length;
The XML file is not for counting the arrays inside it. It is for storing information that doesn't belong in your code in a static way.
This way you can alter menu's, lists dynamically.
What you can do is instead of creating multiple arrays, is store it ** multidimensional**. link
Now you can count the arrays like user1969053 is suggesting

How to get String-Array referenced by another String-Array?

For example if I have the following String_Array
<string-array name="recipe_ingredients">
<item>#array/m1_ingredients</item>
<item>#array/m2_ingredients</item>
</string-array>
How can I access #array/m1_ingredients where it is another String_Array
EDIT :
No, every item of a StringArray must always be a String, of course. So it can't be another StringArray
check the Documentation on resources, scroll to StringArray
name
String. A name for the array. This name will be used as the resource ID to reference the array.
here's an eg
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>
</resources>
This application code retrieves a string array:
Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);

Android Resource - An Array to store Country-Name/Country Code

I am storing some informaiton in my android app and per unit of info, I need to store multiple data for each item in string-array or what ever appropriate.
For example if we take a list of countries, the XML file could be defined as follows. Here we have only one value, which is the name of the country that is listed in a string-array.
<resources>
<string-array name="countries_array">
<item>Bahrain</item>
<item>Bangladesh</item>
<item>Barbados</item>
<item>Belarus</item>
<item>Belgium</item>
<item>Belize</item>
<item>Benin</item>
</string-array>
</resources>
My requirement is to save more info, such as the country name and the country code in one resource.
My intention is to iterate over a resource and then for each country, I be able to get its sub items such as country name and country code. This way I can save objects that I need in the application and generate them from parsing the file.
At the moment, I am considering a string split and get the values or by using 2 arrays, but wondering if there is any eligent way to do it. XML and relavent Java code is appreciated.
Thanks in advance.
<resources>
<string-array name="countries_array">
<item>
<name>Bahrain</name>
<code>12345</code>
</item>
...
</string-array>
</resources>
CountryInfo.java:
public class CountryInfo {
public String mName;
public String mCode;
public CountryInfo(String name, String code) {
mName = name;
mCode = code;
}
}
ArrayList<CountryInfo> countryList = new ArrayList<CountryInfo>();
Then parse the xml file with an XML parser(SAX or DOM, examples can be found here) and then you can add each country info into your array like this:
CountryInfo tmp = new CountryInfo(name, code);
countryList.add(tmp);

Categories

Resources