Android: Setselection Having No Effect on Spinner

Android: setSelection having no effect on Spinner

Try moving the call to setSelection() after the call to setAdapter().

Android Spinner.setSelection() doesn't work

Try Spinner#setSelection (int position, boolean animate) with animate = false. I remember a while back I had a similar problem and this did the trick. The internal implementation seems to differ apart from the difference coming from the animate part.

Spinner setSelection() not work

Have you tried to set the spinner by using two arguments, the second using a boolean:

detailsOptionSpinner.setSelection(finalCount, true); 

From the developers page it shows:

setSelection(int position, boolean animate)
//Jump directly to a specific item in the adapter data.

spinner set selection doesn't working

Make sure your setSelection() is called after you done with spinner's setAdapter().

Spinner methods (setSelection) not working for only one spinner in activity

Put your setSelection before setting adapter.

After seeing your code you forgot to put the second parameter of setSelection().

It accepts two selection setSelection(position in int, Boolean)

E.g.

setSelection(boardNumber - 7, true)

Spinner.setSelection doesn't trigger OnItemSelectedListener properly

Have you tried to set the spinner by using two arguments, the second using a boolean:

.setSelection(mProvincesCode.indexOf(mUser.getProvince()), true); 

From the developers page it shows:

setSelection(int position, boolean animate)
//Jump directly to a specific item in the adapter data.

SetSelection on a spinner crashes when layout_weight is assigned to spinner

spin1.setSelection(0);  //does not crash
spin1.setSelection(getResources().getStringArray(R.array.feet).length - 1);

change to

spin1.setSelection(0, true); 
spin1.setSelection(getResources().getStringArray(R.array.feet).length - 1, true);

I also had this question and solved

Spinner selection not working Android

I'll solve like this.Try this. This is just example Activity.

public class MainActivity extends Activity {
SpinnerAdapter ratingAdapter;
ListView listView;
List<String> listString;
ArrayAdapter<String> dataAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listString = new ArrayList<String>();
for(int listCount = 0; listCount < 20 ; listCount++){
listString.add("ListCount"+listCount);
}

List<String> spinnerData = new ArrayList<String>();
for (int i = 0; i < listString.size(); i++) {
spinnerData.add("MyTest="+i);
}

dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1, spinnerData);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

fillAdapter();
}

private void fillAdapter() {
ratingAdapter = new SpinnerAdapter(getApplicationContext());
listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(ratingAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

class ViewHolder {
Spinner spinner;
}

class SpinnerAdapter extends ArrayAdapter {
Context context;
HashMap<Integer,Integer> selectedItems = new HashMap<Integer, Integer>();

public SpinnerAdapter(Context context) {
super(context, R.layout.item_list_spinner_adaoter, listString);
this.context = context;
}

public int getCount() {
return listString.size();
}
public Object getItem(int position) {
return listString.get(position);
}
public long getItemId(int position) {
return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.item_list_spinner_adaoter, null);
holder.spinner = (Spinner) convertView.findViewById(R.id.spinner1);

convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

holder.spinner.setAdapter(dataAdapter);
if ( selectedItems.get( position ) != null ) {
holder.spinner.setSelection( selectedItems.get( position ) );
}
holder.spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
selectedItems.put( position, arg2 );
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
});

return convertView;
}
}
}

In activity_main.xml:

    <ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:stackFromBottom="false"
android:transcriptMode="disabled" >
</ListView>

</LinearLayout>

In item_list_spinner_adaoter.xml:


<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />



Related Topics



Leave a reply



Submit