How to handle duplicate values for list preference? - android

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.

Related

How to set StringArray value on click on array item in android?

I'm working with an android spinner. Here I have two arrays in my XML like below and I'm showing car array in mine spinner.
<string-array name="car">
<item>128i Coupe</item>
<item>M3 Coupe</item>
<item>M5 Sedan</item>
</string-array>
<string-array name="value">
<item>1</item>
<item>0</item>
<item>2</item>
</string-array>
Now I can call the any of them in my Java file like this
String[] BMW_Model = MainActivity.this.getResources().getStringArray(R.array.car);
I can get/print the value of this item on click by this way
String td = spinnerManufacture.getSelectedItem().toString();
Now what I need is when I click on an item of my car array I should display the value of my second value array according to the position. As an example.
If I click on M5 Sedan from my spinner item it should show 2 in a Toast message from my second array.
Any kind of suggestion will be highly appreciated.
Since you have completely different arrays, the solution to your problem could be to get the position of the pressed spinner element and get the value of the second array at this index.
int position = spinner.getSelectedItemPosition()
String[] valueArray = context.getResources().getStringArray(R.array.value);
Toast.makeText(context, valueArray[position],Toast. LENGTH_SHORT).show();
If you need to display toast on click, then you need to set clickListener
spinner.setOnItemSelectedListener(new

How to filter array from resource

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);

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

Combine Multiple string-array Resources

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!!

Using HashMap to map a String and int

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

Categories

Resources