Show the value of spinner in string instead of integer? - android

I want to show the value of spinner in string instead of integer. Right now it is showing integer like '0','1'.
Setting activity:
public class Setting extends Activity {
bloodtype = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.blood_groups_arrays));
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
bloodtype.setAdapter(adapter2);
bloodtype.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
/**
* Called when a new item is selected (in the Spinner)
*/
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
sharedpreferences.edit().putString(BloodType, parent.getItemAtPosition(position).toString()).commit();
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
});
if(sharedpreferences.contains(BloodType)) {
bloodtype.setSelection(sharedpreferences.getString(BloodType, "DEFAULT"));
}
}
Calling its value in main activity:
if(sharedpreferences.contains(Setting.BloodType)) {
show.setText(""+sharedpreferences.getString(Setting.BloodType, "DEFAULT"));
}

In order to store a value of array you populated your spinner with in SharedPreferences use the following line in onItemSelected() method:
sharedpreferences.edit().putString(BloodType, parent.getItemAtPosition(position).toString()).commit();
Note: BloodType has to be of String type, because it plays a role of a key in key-value pair of SharedPreferences.

Taken from my working project it should work:
adapterSpinner = new ArrayAdapter<String>(yourContenxt,
android.R.layout.simple_list_item_1, yourDataSource);
spinnerProfileType.setAdapter(adapterSpinner);
As you are using a string array you can set the spinner items in xml too by using:
android:entries = "#array/your_string_entry"

Related

How To Display Specific Amount in Spinner?

I want to get information using putExtra() on another page and I can do it, but my only problem is always the default value the first value sample_array but i want to by default classDayIntent value is displayed in spinner
tip: The putExtra() received in Spinner is there
First Activity:
Intent intent = new Intent();
intent.putExtra("className" , className);
intent.putExtra("uniName" , uniName);
intent.putExtra("classDay" , classDay);
intent.setClass(context , AddNewClass.class);
context.startActivity(intent);
Second Activity:
Spinner spinner = (Spinner)findViewById(R.id.spinnerDay);
String className = getIntent().getStringExtra("className");
String uniName = getIntent().getStringExtra("uniName");
String classDayInent = getIntent().getStringExtra("classDay"); // How To Display classDayInent In Spinner....
className_EditeText.setText(className);
uniName_EditeText.setText(uniName);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this , R.array.sample_array , android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String classDay = parent.getSelectedItem().toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Strings:
<resources>
<string name="app_name">ClassManager</string>
<string-array name="sample_array">
<item>شنبه</item>
<item>یکشنبه</item>
<item>دوشنبه</item>
<item>سه شنبه</item>
<item>چهارشنبه</item>
<item>پنجشنبه</item>
<item>جمعه</item>
</string-array>
you can use add function of adapter.
since CharSequence is an interface, and the String class implements CharSequence so you can directly add string to adapter
adapter.add(classDayInent);
spinner.setAdapter(adapter);
Another issue is , createFromResources creates a immutable list mean it cannot add more items.
Solution : create your own adapter with mutable list
List<String> list = new ArrayList<>(Arrays.asList(getResources().getStringArray(R.array.sample_array)));
ArrayAdapter<String> adapter = new ArrayAdapter<>(this , android.R.layout.simple_spinner_item,list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
list.add(0,classDayIntent);// to add received element at first position to display
spinner.setAdapter(adapter);
You can get your xml arraylist and then add to that:
List<String> ar = new LinkedList<String>(Arrays.asList(getResources().getStringArray(R.array.sample_array)));
ar.add("test");
i found correct answer :
String classDayInent = getIntent().getStringExtra("classDay");
List<String> list = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.sample_array)));
ArrayAdapter<String> adapter = new ArrayAdapter<>(this , android.R.layout.simple_spinner_item,list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
//find classDayIntent from List For get Position To Display
for (int i = 0; i < list.size(); i++)
{
if (classDayIntent.equals(list.get(i)))
{
spinner.setSelection(i);
}
}
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
classDay = parent.getSelectedItem().toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

How to add default value in Spinner other then adapter Value

i have some data coming from server and i have to show that value in Android Spinner.
Adapter that is attach to Spinner also getiing from server.
please let me know how to show initial value coming from server.and show adapter value after click on Spinner.
Spinner mySpinner = (Spinner) findViewById(R.id.householdspinner);
mySpinner.setAdapter(new ArrayAdapter<String>(Edit_Voter_Information.this,
android.R.layout.simple_spinner_dropdown_item,
householdIncome));
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int position, long arg3) {
String item = arg0.getItemAtPosition(position).toString();
house_Hold_Income = item;
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
Initially i stored some values in String[] names. when activity is run spinner position 0(zero)is selected by default.
Check my code below:
ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.spinner_style, names);
spinner.setAdapter(arrayAdapter);
spinner.getSelectedItemPosition();//which returns names[0] initially.
if click other position(i) it returns names[i], where i =0,1,2,..

Android spinner item id related prob

I have an ArrayList of custom object. This custom object contains zoneCode(Integer) and zoneName(String), I want to set this data in an Spinner so that the zoneName comes in the list and onItemSelected, I can get the zoneCode of corresponding selection. How it is possible?
try zoneCod.get(position)
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int
position, long id) {
zoneCod.get(position);
}
}
As the data of both zoneName and zoneCode are in array list and you know how to get the position of selected item. if yes, then you can get the zoneCode as following:
zoneCode[position];
override toString method in your custom object class and make it return zoneName
class CustomObject{
public String zoneName;
public int zoneCode;
public String toString(){
return zoneName;
}
}
setting up the spinner
CustomObject[] objects = new CustomObject[10];
//initialize each object
ArrayAdapter<CustomObject> adapter = new ArrayAdapter<CustomObject>(this,android.R.layout.simple_spinner_item, objects);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

Order of OnItemSelectedListener Calls After OnCreate

I have two spinners in an Activity where the second Spinner's selection set is based on what the user picked for the first Spinner. I use a private class variable in the Activity which is set in the top Spinner's OnItemSelectedListener and then referenced in the bottom Spinner's OnItemSelectedListener to obtain the correct selection set.
This almost always works, but sometimes (mainly when app was run, not exited, and then started again by a user click some long time later) I get a null pointer exception in the second Spinner's OnItemSelectedListener due to this local variable not being set. This indicates to me that after the OnCreate that the second Spinner's OnItemSelectedListener was called before the first Spinner's.
Is there any method to force a certain order in the listeners being fired or is there a better design approach to handle this second Spinner's dependency on the first Spinner?
Example code:
package com.crashtestdummylimited.navydecoder;
public class Test extends Activity {
// Variable that at times is still null
private ReferenceData referenceData;
private void setupSpinnerFromArray (int spinnerId, String stringArray[], OnItemSelectedListener listener) {
Spinner spinner = (Spinner) findViewById(spinnerId);
ArrayAdapter <CharSequence> adapter = new ArrayAdapter <CharSequence>(
this, android.R.layout.simple_spinner_item, stringArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(listener);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_screen);
// Setup Top (main) Spinner
Spinner spinner1 = (Spinner) findViewById(R.id.mainDecodeSpinner);
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
this, R.array.level0_list_array, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener(new MainDecoderItemSelectedListener());
// Setup Bottom (dependent) Spinner
setupSpinnerFromArray(R.id.secondaryDecodeSpinner, R.array.level1_list_array, new SecondaryDecoderItemSelectedListener());
}
public class MainDecoderItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
String selectedString = parent.getItemAtPosition(pos).toString();
if (selectedString.equals("AAA")){
// Problem variable is set
referenceData = new RatingCodes();
setupSpinnerFromArray(R.id.secondaryDecodeSpinner, referenceData.getKeys(), new SecondaryDecoderItemSelectedListener());
}
else if (selectedString.equals("BBB")){
// Problem variable is set
referenceData = new IMSCodes();
setupSpinnerFromArray(R.id.secondaryDecodeSpinner, referenceData.getKeys(), new SecondaryDecoderItemSelectedListener());
}
// TODO: Improve what occurs if no match which should not occur
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
}
public class SecondaryDecoderItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
String key = parent.getItemAtPosition(pos).toString();
// **** referenceData being null at this point has caused crashed ****
String value = referenceData.getValue(key);
// ... Update text on activity screen ...
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
}
}
public void onItemSelected(AdapterView<?> parent,View view, int pos, long id)
{
if(pos == 0)
{ //when it loads on onCreate() then pos is always 0
//donothing
}
else
{ //If user manually select item
//do what you need to do on manual user selection
}
}

How to get multiple spinner in android where value of one spinner depends on value of other spinner?

I want to have dynamic spinner in android e.g one spinner for country and depending upon the value selected in Country, i want to get another spinner for states.
I believe you can solve this with the OnItemSelectedListener.
public void onCreate() {
....
Country[] mCountries = ... ;
final Spinner spinner1 = ...;
final Spinner spinner2 = ...;
spinner1.setAdapter(new ArrayAdapter(mCountries);
spinner1.setOnItemSelectedListener( new OnItemSelectedListener() {
void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Country country = (Country) parent.getAdapter().getItem(position);
spinner2.setAdapter(new ArrayAdapter(country.getStates());
}
void onNothingSelected(AdapterView<?> parent) {
spinner2.setAdapter(null);
}
});
....
}

Categories

Resources