I have multiple different categories and in each category I use one string resource to populate a ListView like so:
<string-array name="list">
<item>item1</item>
<item>item2</item>
<item>item3</item>
</string-array>
I call them like so:
String[] list = getResources().getStringArray(R.array.list);
How can I combine these into one String[] to use as an "All" list.
you can combine two Array List into a single one Like this:
ArrayList<String> first;
ArrayList<String> second;
second.addAll(first);
Since you use Simple Array there a multiple way to change arrays to arrayList, you can fetch for it on the net. But why you are not storing them on a single array in xml file that you call it global_content for exemple. You are waisting time and memory for this!!
Related
I am creating the currency format feature with list preference.
List of entries are as follows:
<string-array>
.....
<item>Australia</item>
<item>Canada</item>
<item>United Kingdom</item>
<item>United States</item>
<item>Uruguay</item>
.....
</string-array>
And the corresponding list of values:
<string-array>
.....
<item>$</item>
<item>$</item>
<item>£</item>
<item>$</item>
<item>$U</item>
.....
</string-array>
When I select Australia, the United States becomes selected. This is because both entries have the same value and the system chooses the last item if there are duplicate values. How should we overcome this issue easily? I can use unique value with a prefix or suffix to solve the duplicity but this will lead me to do more work to encode and decode the value whenever needed.
I have tried to set the preference dynamically with no luck:
....
CharSequence[] entries = currencyPreference.getEntries();
for (int index = 0; index < entries.length; index++) {
if (entries[index].equals(entryCurrency)) {
currencyPreference.setValueIndex(index);
}
}
.....
Updated:
After searching a lot I have concluded that I had to use another list to accomplish this.
<string-array name="entry_values_currency">
<item>0</item>
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</string-array>
<string-array name="currency_symbols">
<item>$</item>
<item>$</item>
<item>£</item>
<item>$</item>
<item>$U</item>
</string-array>
And get the symbol as follows:
String currency = getResources().getStringArray(R.array.currency_symbols)[Integer.parseInt(currencyPreference.getValue())];
In your case, you can use HASHSET java collection library. Hashset is basically used when you need to store unique data.
- Declare a hashet of type String.
- Extract the strings from the list array and store them one by one in the hashset using the for loop with condition size and increment.
- Then declare an ArrayList of type String.
- Create the for loop with condition hashset size and increment and use arraylist 'addAll()' to store the hashset data into your new arraylist.
- The above step is because hashset doesnt store data in an indexing way and so it becomes trouble while getting the index specific data.
Hashset<String> hashset = new Hashset<>();
hashset.add("your list array data");
Arraylist<String> arraylist = new Arraylist<>();
arraylist.addAll(hashset);
These is how you will declare and initialize the hashset and arraylist.
I have an string array like this:`
<string-array name="converterlist">
<item>Angle</item>
<item>Area</item>
<item>Bits and Bites</item>
<item>Density</item>
<item>Electric Current</item>
<item>Energy</item>
<item>Force</item>
<item>Length</item>
<item>Mass</item>
<item>Power</item>
<item>Pressure</item>
<item>Speed</item>
<item>Temperature</item>
<item>Time</item>
<item>Volume</item>
</string-array>
When I set it on an android spinner, it is showing as it is on the array. Now, I have another spinner where I want to set the list reverse or from second item. How can I do it?
In your onCreate method for the activity that has the spinner, load the string array from resources getResources().getStringArray(...), reverse that array, and set it as the data source to the reverse spinner using an ArrayAdapter.
try this.
String[] arr=getResources.getStringArray(R.id.converterlist);
//reversed list
List<String> convertList=Collections.reverse(new ArrayList<String>(Arrays.asList(arr)));
I am working on android app, Here i have to array resources in xml file and two listview in different activities first array list is of state in India while second is for districts of India. i am showing states in first listview and i want to show the district in second list view but my problem is i can not think for how to filter district based on selected state. for ex when user selects Chhattisgarh as state then the second listview should show only districts from Chhattisgarh not others.
Thank you in advance
You can define states as a single array list and different array lists for districts in values/arrays.xml
<string-array name="states">
<item>state1</item>
<item>state2</item>
</string-array>
<string-array name="state1-districts">
<item>district1</item>
</string-array>
<string-array name="state2-districts">
<item>district1</item>
</string-array>
In code, you can access your states array:
activity.getResources().getStringArray(R.array.states);
And you can access districts of a specific state:
String districtResourceName = "state1-districts";
int districtId = getResources().getIdentifier(districtResourceName, "array", "com.your.project");
activity.getResources().getStringArray(districtId);
I have the following problem:
I created a Char Sequence and was able to name 4 units. I would however rather use strings from my XML file for localization purposes. Is there any way to achieve that?
final CharSequence[] choices =
//want to add strings here i.e. R.strings.lemonade
{"Coke", "Pepsi" , "Sprite" , "Seven Up" };
builderType.setSingleChoiceItems( choices, selected, new OnClickListener()
{.......
Error message:
Type mismatch: cannot convert from int to CharSequence
There's another overload of AlertDialog.Builder.setSingleChoiceItems() that takes in an int resource id for a string array of items. Put the following in an xml in res/values e.g. strings.xml:
<string-array name="choices">
<item>Coke</item>
<item>Pepsi</item>
<item>Sprite</item>
<item>Seven Up</item>
</string-array>
Then you can use it as:
builderType.setSingleChoiceItems(R.array.choices, selected, new OnClickListener(), ...
For generic cases, you can also load string array resources with Resources.getStringArray() as suggested by #Egor.
Create a string-array in strings.xml
<string-array name="choices">
<item>Coke</item>
<item>Pepsi</item>
<item>Sprite</item>
<item>Seven Up</item>
</string-array>
Then fetch it from resources
String[] choices = context.getResources().getStringArray(R.array.choices);
Then use it in setSingleChoiceItems() as is, since String implements CharSequence.
I have a ListView showing names of countries.
I have stored the names in strings.xml as a string-array called country_names.
In populating the ListView, I use an ArrayAdapter which reads from strings.xml:
String[] countryNames = getResources().getStringArray(R.array.country_names);
ArrayAdapter<String> countryAdapter = new ArrayAdapter<String>(this, R.layout.checked_list, countryNames);
myList.setAdapter(countryAdapter);
Now I also have a CountryCode for each country. When a particular country name is clicked on the ListView, I need to Toast the corresponding CountryCode.
I understand implementing a HashMap is the best technique for this. As far as I know, the HashMap is populated using put() function.
myMap.put("Country",28);
Now my questions are:
Is it possible to read the string.xml array and use it to populate the Map? I mean, I want to add items to the Map, but I must be able to do so by reading the items from another array. How can I do this?
The basic reason I ask is because I want to keep the country names and codes in a place where it is easier to add/remove/modify them.
The string-arrays are stored in strings.xml. Where must similar integer arrays be stored? In values folder, but under any specific XML file?
As one of the possibilities, you may store 2 different arrays in XML: string array and integer array, and then programmatically put them in the HashMap.
Definition of arrays:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="countries_names">
<item>USA</item>
<item>Russia</item>
</string-array>
<integer-array name="countries_codes">
<item>1</item>
<item>7</item>
</integer-array>
</resources>
And code:
String[] countriesNames = getResources().getStringArray(R.array.countries_names);
int[] countriesCodes = getResources().getIntArray(R.array.countries_codes);
HashMap<String, Integer> myMap = new HashMap<String, Integer>();
for (int i = 0; i < countriesNames.length; i++) {
myMap.put(countriesNames[i], countriesCodes[i]);
}
It may be a file with any name. See this