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
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'm using xml array with value and its Id.I want to get Id of value after user selects it.Kindly tell me how can I get that?
<string-array name="medicine">
<item id="21058">ALPHA LIPOIC ACID</item>
<item id="19699">B 50 VITAMIN</item>
<item id="19470">B100 ULTRA 50S</item>
</string-array>
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_item, getResources().getStringArray(R.array.medicine));
AutoCompleteTextView actv= (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
actv.setOnClickListener(this);
actv.setThreshold(1);
actv.setAdapter(adapter);
I have found its answer myself. I made a model for ArrayList, I assign that ArrayList to the AutoCompleteTextView Adapter and then, the OnClick event of the AutoCompleteTextView gives me the exact id and value.
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 a List Preference for Accelerometer mode in my preference activity which I'm loading purely from xml. If a users device does not have or support an Accelerometer I wont to completely remove the item in the array. Do I have to create a separate list preference for this one option to remove it? (I would prefer to keep them all in one.) How can it be removed with the below setup?
Thanks for the tip.
Jason
<string-array name="mode_text">
<item>Others Modes</item>
<item>Others Modes</item>
<item>Accelerometer Mode</item>
</string-array>
<string-array name="mode_values">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>
</resources>
When you say you want to "completely remove the item in the array", do you mean you want the ListPreference to not show in the PreferenceScreen? Or you want it to return a null value when you try to get the set value?
Okay, I understand now. I think you can't remove just that item in the array. You have to set new arrays for the entries and entryValues for your ListPreference, programatically.
You find the ListPreference by its key.
Here is an example:
if (accelerometerNotSupported) {
ListPreference accelMode = (ListPreference) findPreference("acceleratorMode");
accelMode.setEntries(new String[]{"Others Modes","Others Modes"});
accelMode.setEntryValues(new String[]{"0", "1"});
}
That will change your list preference to have only those two options.