I have a spinner, which contains language options and its items are named from string resources depending on system language. I need something like an identifier for these languages attached to each item. For example, I need to know that the user selected "English" as an option (or "en" if we are talking about identifiers) even though when he selected it, he chose "Englisch" (if the user is german). So how can I attach these kinds of identifiers to my array-list items?
I'd suggest making a simple class to hold the required data.
public class Language {
private String identifier;
private String name; //localised name
...
//we want to show the localised data in the spinner
#Override
public String toString() {
return name;
}
Then just make a simple ArrayAdapter and overwrite the onItemSelected function of the spinner or just get the selected item whenever you wish to. Something like:
List listWithLanguages = getLanguages();
ArrayAdapter<Language> adapter = new ArrayAdapter<>(this, R.layout.spinner_item, listWithLanguages);
spinner.setAdapter(adapter)
...
String identifier = (Language) spinner.getSelectedItem().getIdentifier();
You can treat your string-array items as an identifier.
And can use get the selected item from setting up the listener
AdapterView.OnItemSelectedListener
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
Use a custom adapter that encapsulates the information you care about, then instead of building an adapter that creates a list of localized strings, you create an adapter that creates a list of objects that have the identifier and the localized string together.
Kotlin pseudo code:
val strings = loadLocalizedStringsFromArrayItems()
val identifiers = loadIndentifierStringsFromArrayItems()
val listOfPairs = identifiers.zip(string)
val adapter = ArrayAdapter<List<Pair<String, String>>>(listOfPairs)
val spinner.setAdapter(adapter)
// When clicking on the spinner item:
val pair = adapter.getItemAtPosition(clickedPosition)
val identifier = pair.first
val localizedString = pair.second
Related
how to store only selected item list data in recycler view from other activity list.
i used this code -- successfully select data but don't know how to add only selected data items in new activity recycler view -
i used this code snippet ite working fine-
//
https://en.proft.me/2018/03/3/multi-selection-recyclerview/
StringBuilder stringBuilder = new StringBuilder();
for (ExcercisesSelectedModel.DataBean data : getList()) {
if (selectedIds.contains(data.getId()))
stringBuilder.append("\n").append(data.getName());
Change
public interface OnClickAction {
public void onClickAction();
}
to
public interface OnClickAction {
public void onClickAction(Item item);
}
In adapter class, on item click
receiver.onClickAction(item);
In Activity class
private List<Item> selectedItem = new ArrayList()
public void onClickAction(Item item) {
selectedItem.add(item);
}
Now use this selected item list in new activity.
Edit: Don't forget to use or rename the interface to Callback, something along the lines of ActivityCallback or OnClickCallback.
This is for the sake of naming conventions.
A simple flag isSelected in parent list will save your day.
So whenever user select/unselect the item just change the value of isSelected flag to true or false.
Now you have a final list from which you can easily identify selected items, simple store in separated list named selectedItemList.
At last use the selectedItemList and fill your Recycleview. Hope it make sense.
I have a spinner that have 3 values populated from array-list.
I want to save the spinner selected value to shared preferences so it can be loaded back again when the use comes back to the app. What is the correct way to do this (to avoid future problems)
1- Save position of the selection
2- Save the text of the selection
3- Get the position/text of the selection, get the corresponding enum, and save the enum name.
I am leaning towards the 3rd option incase the positions/texts changed in later updates but I am wondering what is the correct way of doing such task
Thank you
Save position (1 variant) and text (2 variant) are bad practice. Because text for your spinner items may will change in the future and their position can be changed.
I think, that you need to create enum or #TypeDef element and save it to sharedPreferences. #TypeDef is more performance but enum is more functionality (if you use Kotlin you can use sealed classes). For this solution just write mapper that can map enum to spinner item.
If you use enum, the best way is to save it name ENUM.name().
Read carefully and understand. Get the post and use at your own understanding.
Declare your spinner and sharedPreferences
public Spinner crimeType;
SharedPreferences sharedPreferencesFirstTime;
//////
sharedPreferencesFirstTime = getPreferences
(Context.MODE_PRIVATE);
String firstTime = getResources().getString(R.string.saved_first_time);
firstTimekey = sharedPreferencesFirstTime.getString
(getString(R.string.saved_first_time),
firstTime);
crimeType = v.findViewById(R.id.crimeType);
Initializing a String Array
String[] plants = new String[]{
"Antisocial behaviour",
"Arson",
"Burglary"
};
Initializing an ArrayAdapter
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<>(
getActivity(), android.R.layout.simple_spinner_item, plants
);
Sets the layout resource to create the drop down views.
/*
Parameters : resource
the layout resource defining the drop down views
*/
spinnerArrayAdapter.setDropDownViewResource
(android.R.layout.simple_spinner_dropdown_item);
Sets the Adapter used to provide the data which backs this
/*
setAdapter(SpinnerAdapter adapter)
Sets the Adapter used to provide the data which backs this Spinner.
*/
crimeType.setAdapter(spinnerArrayAdapter);
crimeType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Get the text content
crime_string= parent.getItemAtPosition(position).toString();
//Get Position of the crime
selectionPosition=
parent.getItemAtPosition(position);
SharedPreferences.Editor editor = sharedPref.edit();
String key2 = crime_string;
editor.putString(getString(R.string.saved_login_key), key2);
editor.apply();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
//Another interface callback
}
});
I have a List<Service> received from getParcelableArrayListExtra() Now I need to populate the received objects into a android ListView
I have try to populate the ListView in the following way. The ListView is populated to show the Object reference ids. Instead of showing contants.
How do i show the contents in this list.
The implementation is as follow :
List<Service> serviceCart = getIntent().getParcelableArrayListExtra("serviceCart");
ListView listView = (ListView) findViewById(R.id.lv_choosed_services);
ArrayAdapter<Service> serviceArrayAdapter = new ArrayAdapter<Service>(this,android.R.layout.simple_list_item_1,serviceCart);
listView.setAdapter(serviceArrayAdapter);
Open ArrayAdapter and go to read from line no 405 to 410
final T item = getItem(position);
if (item instanceof CharSequence) {
text.setText((CharSequence) item);
} else {
text.setText(item.toString());
}
What you are doing:
If your item is String Adpater.getView(params) will use it, otherwise it will use item.toString(). In your Service.java you don't have a toString() so application uses default default toString()
What you should do:
Its best to create custom ArrayAdapter and populate your view
Override toString() in your Service.java and use only those values you want to populate in your adapter like service_name and/or service_code and default implementation will do the rest.
Note:
Please read Naming Conventions, its not recommended to name your objects like service_name it should be serviceName.
I have an array of Strings I'm populating a Spinner object with. However, I'd like to attach an ID to each element of the Spinner, so when the user selects an item, I have its ID to use to save to some other piece of data. How can I do this?
Create a class StringWithTag and use in place of the string name in the list like so :-
public class StringWithTag {
public String string;
public Object tag;
public StringWithTag(String stringPart, Object tagPart) {
string = stringPart;
tag = tagPart;
}
#Override
public String toString() {
return string;
}
}
in the add items to spinner part :-
List<StringWithTag> list = new ArrayList<StringWithTag>();
list.add(new StringWithTag("Oldman", "12345"));
list.add(new StringWithTag("Umpire", "987654"));
list.add(new StringWithTag("Squad", "ABCDEE"));
ArrayAdapter<StringWithTag> adap = new ArrayAdapter<StringWithTag> (this, android.R.layout.simple_spinner_item, list);
....
....
in the listener :-
public void onItemSelected(AdapterView<?> parant, View v, int pos, long id) {
StringWithTag s = (StringWithTag) parant.getItemAtPosition(pos);
Object tag = s.tag;
}
voila!
}
What do you mean by id. You can use ArrayAdapter to populate the Spinner. When item is selected just get the element from the adapter and save the data you want.
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<MyObject> adapter = ... // initialize the adapter
adapter.setDropDownViewResource(android.R.layout.some_view);
spinner.setAdapter(adapter);
and when item is selected
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
MyObject selected = parent.getItemAtPosition(pos);
// save any data relevant with selected item
}
If you are storing your data in db you can use CursorAdapter and in onItemSelected to fetch the selected item id from the cursor.
I don't think you can attach an arbitrary ID to elements of a text array resource, if that's what you're using.
I think the simplest way to attach such an ID would be to either hard-code (if you're using a static text resource) or dynamically build (if you get the strings at runtime) a mapping from (String position in array)->(primary key).
EDIT: On the other hand, Mojo Risin has a point - you should check to see if the CursorAdapter API already does what you need for you.
Andrew Hi, it's been a long time but it's worth to write.
You can set a tag for each row when you'r inflating spinnerLayout in SpinnerAdapter:
spinnerView = inflater.inflate(spinnerLayout, parent, false);
spinnerView.setTag("Your Tag");
And then you can get the tag with:
yourSpinner.getSelectedView().getTag();
I think The best solution is to add one more spinner and fill it with the ids but make the visibility of it to gone
I declare my Spinner in the following manner (it's very static so I
have 2 string arrays in array.xml for titles and values)
<Spinner
android:id="#+id/searchCriteria"
android:entries="#array/searchBy"
android:entryValues="#array/searchByValues" />
I expect spinner.getSelectedItem() to return an array [title, value]
but in fact it returns just a title String. Is it ignoring
android:entryValues? How do I get a value, not a title from it? Is
this doable with XML only or do I need to create adapter and do it
programmatically?
Rather than the dual array method, why not fill your ArrayAdapter programmatically with objects of a known type and use that. I've written a tutorial of a similar nature (link at the bottom) that does this. The basic premise is to create an array of Java objects, tell the spinner about the, and then use those objects directly from the spinner class. In my example I have an object representing a "State" which is defined as follows:
package com.katr.spinnerdemo;
public class State {
// Okay, full acknowledgment that public members are not a good idea, however
// this is a Spinner demo not an exercise in java best practices.
public int id = 0;
public String name = "";
public String abbrev = "";
// A simple constructor for populating our member variables for this tutorial.
public State( int _id, String _name, String _abbrev )
{
id = _id;
name = _name;
abbrev = _abbrev;
}
// The toString method is extremely important to making this class work with a Spinner
// (or ListView) object because this is the method called when it is trying to represent
// this object within the control. If you do not have a toString() method, you WILL
// get an exception.
public String toString()
{
return( name + " (" + abbrev + ")" );
}
}
Then you can populate a spinner with an array of these classes as follows:
// Step 1: Locate our spinner control and save it to the class for convenience
// You could get it every time, I'm just being lazy... :-)
spinner = (Spinner)this.findViewById(R.id.Spinner01);
// Step 2: Create and fill an ArrayAdapter with a bunch of "State" objects
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, new State[] {
new State( 1, "Minnesota", "MN" ),
new State( 99, "Wisconsin", "WI" ),
new State( 53, "Utah", "UT" ),
new State( 153, "Texas", "TX" )
});
// Step 3: Tell the spinner about our adapter
spinner.setAdapter(spinnerArrayAdapter);
You can retrieve the selected item as follows:
State st = (State)spinner.getSelectedItem();
And now you have a bona fide Java class to work with. If you want to intercept when the spinner value changes, just implement the OnItemSelectedListener and add the appropriate methods to handle the events.
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
// Get the currently selected State object from the spinner
State st = (State)spinner.getSelectedItem();
// Now do something with it.
}
public void onNothingSelected(AdapterView<?> parent )
{
}
You can find the whole tutorial here:
http://www.katr.com/article_android_spinner01.php
So if you came here because you want to have both label and value in the Spinner - here's how I did it:
Just create your Spinner the usual way
Define 2 equal size arrays in your array.xml file. One for labels, one for values
Set your Spinner with android:entries="#array/labels"
In your code - when you need a value do something like this (no you don't have to chain it)
String selectedVal = getResources().getStringArray(R.array.values)[spinner
.getSelectedItemPosition()];
And remember - these 2 arrays have to match each other as far as number slots and positions
Abort, abort! I don't know what got into me but Spinner does not support android:entryValues attribute. That one is actually from ListPreference which does a similar thing (displays list of items in pop-up dialog). For what I need I will have to (alas) use the SpinnerAdapter