Android: Spinner not showing the selected value - android

I have a spinner and drop down list, the value for the spinner is getting from JSON parsing.My problem is the value is setting into the spinner but when i select a value form the drop down it is not showing in the spinner as selected, it is always blank.
I initialize the spinner as
final Spinner spinner = (Spinner) v.findViewById(R.id.spinner);
final List<String> money = new ArrayList<String>();
Assync task Api parsing onSuccess
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
String amount = c.getString("amount");
money.add(amount+" "+euro);
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, money);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
I notice that before parsing if set a value in spinner like money.add("0"+" "+euro); , at the time all the value is showing in the spinner.
Can anyone please tell me where am wrong, why it is not showing the selected value in the spinner

This will helps you :-
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, long id) {
// selectedItem = money[position];
//OR
spinner.setSelection(arrayAdapter.getPosition(pos));
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});

Related

Creating dynamic spinners and getting value of every spinner, Whenever a spinner item changed, get all spinner values

I'm developing a e-com app. I'm trying to create dynamic spinner. Spinner is dependent upon product attributes. I'm able to create spinners also mapped data on them but I want to get all spinners selected item whenever a spinner change its data so I can match to correct variant of product.
Here is my code snippet
final List<Attribute> attributes = product_.getAttributes();
for (i = 0; i < attributes.size(); i++) {
ArrayList<String> spinnerArray = (ArrayList<String>) attributes.get(i).getOptions();
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, spinnerArray);
Spinner spinner = new Spinner(getActivity());
spinner.setAdapter(spinnerArrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
dynamicProductProperties.addView(spinner);
}
First, you need to set same instance of OnItemSelectListener to your all spinners. Let your activity or fragment implement OnItemSelectListener then call spinner.setOnItemSelectListener(this);
In onItemSelected method you can run this code to have selecteds.
ArrayList<String> selectedList = new ArrayList();
for (int i = 0; i < dynamicProductProperties.getChildCount(); i++) {
View view = dynamicProductProperties.getChildAt(i);
if (view instanceof Spinner){
String selected = (String) ((Spinner) view).getSelectedItem();
selectedList.add(TextUtils.isEmpty(selected) ? "" : selected);
}
}
Good luck
Emre

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 setSelection calls onItemSelected immidiately or not?

I have 2 spinners in an activity.
Based on the selection of one item in spinner1, relevant data should be loaded in spinner2. Consider spinner1 has data related to country and spinner2 has data related to state.
I should be able to get this done once the activity is created and if the user changes the selection in spinner1.
But I am stuck with populating the spinner2 data based on the saved value of spinner1.
I am calling spinner1.setSelection(indexSaved) but since I am only loading spinner2 in the onItemSelectedOf of spinner1, setSelection of spinner1 is not firing the onItemSelected.
Please let me know how I can achieve this.
I have done same thing in my project without any problem:
Below is code may be it help you:
On Create Method filled state list:
//initialize spinners
spinnerStateList = (Spinner) findViewById(R.id.spinnerStateList);
spinnerDistrictsList = (Spinner) findViewById(R.id.spinnerDistrictsList);
fillStateList();
public void fillStateList()
{
states = manage_states.fetchAllStates(getApplicationContext());
//add new element for select name
states.add(0,new RowItem("-1", "Select State"));
String[] spinnerArray = new String[states.size()];
for(int i= 0;i<states.size();i++)
{
spinnerArray[i] = states.get(i).getName();
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,spinnerArray);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerStateList.setAdapter(dataAdapter);
spinnerStateList.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int position, long arg3) {
String selectedStateID = states.get(position).getId();
fillDistrictsSelectionChangeState(selectedStateID);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
public void fillDistrictsSelectionChangeState(String selectedStateID)
{
if(selectedStateID.equalsIgnoreCase("-1"))
{
linearLayoutDistrictsList.setVisibility(LinearLayout.GONE);
linearLayoutBlocksList.setVisibility(LinearLayout.GONE);
linearLayoutPHCList.setVisibility(LinearLayout.GONE);
linearLayoutSHCList.setVisibility(LinearLayout.GONE);
}
else
{
linearLayoutDistrictsList.setVisibility(LinearLayout.VISIBLE);
districts = manage_districts.fetchAllDistrictsByStateId(getApplicationContext(), selectedStateID);
//add new element for select name
districts.add(0,new RowItem("-1", "Select District"));
String[] spinnerArray = new String[districts.size()];
for(int i= 0;i<districts.size();i++)
{
spinnerArray[i] = districts.get(i).getName();
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,spinnerArray);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerDistrictsList.setAdapter(dataAdapter);
spinnerDistrictsList.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int position, long arg3) {
String selectedDistrictID = districts.get(position).getId();
fillBlocksSelectionChangeDistrict(selectedDistrictID);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
}

Android: Pass iteration value to onItemSelectedListener

Hi I am creating a list of spinners dynamically based on a user choice. Here I am also implementing OnItemSelectedListener for each of the spinners, since there are multiple spinners I want to know which spinner's method is currently being accessed. Here's the code,
for (int i = 0; i < count; i++) {
ArrayList<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("one");
spinnerArray.add("two");
spinnerArray.add("three");
spinnerArray.add("four");
spinnerArray.add("five");
Spinner spinner = new Spinner(this);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Object item = parent.getItemAtPosition(position);
Log.d("vij-debug", "selector1 no is " + item);
Log.d("vij-debug", "selector1 id is"+ view.getId());
//medicineArray1[i][1]=(String)item;
// here I want to access the iteration value i
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_dropdown_item,
spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
Can anyone suggest a suitable solution?
Set the Spinner's tag to i upon creation, then retrieve it from the AdapterView<?> parent parameter in onItemSelected().
Spinner spinner = new Spinner(this);
spinner.setTag(i);
...
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
int spinnerNumber = parent.getTag();
}
...
}
);
you could use a List and store your spinner there. in your for loop
//inside for loop
List<Spinner> spinnerList=new ArrayList<Spinner>();
Spinner spinner = new Spinner(this);
spinnerlist.add(spinner);
//end
//outside for loop
for(int i=0;i<spinnerList.size();i++){
setListenerToSpinner(spinnerList.get(i));
}
//end
//function
public void setListenerToSpinner(Spinner spinner){
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Object item = parent.getItemAtPosition(position);
Log.d("vij-debug", "selector1 no is " + item);
Log.d("vij-debug", "selector1 id is"+ view.getId());
//medicineArray1[i][1]=(String)item;
// here I want to access the iteration value i
}
}

Dependent Spinner using webservice

I used two spinners, country and state which i get from web-service.
i get Country in first web-service then i should pass selected country from spinner to the second web-service, How can i call the dependent Spinner in Web-service ?
Give me the example,
Thanks.
First Spinner
int i = 0;
String[] stringArray = new String[List.length];
for (i = 0; i < List.length; i++) {
stringArray[i] = Arrays.asList(List[i].Country);
}
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, stringArray);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
I had the same query some days back I guess this will solve your problem.
Add this after your spinner1 code
// here onitemselected call your second method
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
statewebservice((String) parent.getItemAtPosition(pos));
}
And for state spinner in your second web service
int i = 0;
String[] stringArray2 = new String[List2.length];
for (i = 0; i < personList1.length; i++) {
stringArray2[i] = Arrays.asList(List2[i].State);
}
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, stringArray2);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(final AdapterView<?> parent,
View view, final int pos1, long id) {
//************Code after selecting second spinner ***********
}
});
}

Categories

Resources