final Spinner spinner = (Spinner)findViewById(R.id.spinnerNumber);
List<Integer> list = new ArrayList<>();
for (int j = 0; j < 51; j++) {
list.add(j);
}
ArrayAdapter<Integer> dataAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
I want to set a selected Item in this spinner. Usually I do spinner.setSelection() inserting the position of the element I want to be setted, inside the setSelection(). This time everything I put inside the setSelection() doesn't produce any effect and my spinner remains set to 0...what am I missing?
try this one
Spinner spinner = (Spinner)findViewById(R.id.spinnerNumber);
final List<Integer> list = new ArrayList<Integer>();
for (int j = 0; j < 51; j++) {
list.add(j);
}
ArrayAdapter<Integer> dataAdapter = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
System.out.println("Selected item :: "+list.get(position));
System.out.println("Selected item Pos :: "+position);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
This will work
int spinnerPosition = adapter.getPosition(yourStringValue);
mSpinner.setSelection(spinnerPosition);
Related
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
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) {
}
});
I am trying to pull the items selected in a multiple checkbox ListView but getcheckitempositions() keeps returning null.
Relevant code:
Checkbox binding:
final ListView list = (ListView) whoView.findViewById(R.id.listView);
ArrayAdapter<String> adapter;
adapter = new ArrayAdapter(this.getActivity(), android.R.layout.simple_list_item_multiple_choice, friendList);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckedTextView checkedTextView = ((CheckedTextView) view);
checkedTextView.setChecked(!checkedTextView.isChecked());
}
});
Trying to pull the items:
ListView list = (ListView) whoView.findViewById(R.id.listView);
List<String> invited = new ArrayList<String>();
SparseBooleanArray checked;
if (list != null) {
checked = list.getCheckedItemPositions(); {
for (int i = 0; i <= checked.size(); i++) {
if (checked.valueAt(i)) {
invited.add(list.getAdapter().getItem(i).toString());
}
}
}
}
I suspect it has to do with the getcheckitempositions() pulling from the ListView and not the checkboxes, but I'm not sure how I would go about fixing this.
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
}
}
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 ***********
}
});
}