Azher Aleem
Azher Aleem

Reputation: 786

Set Spinner selection programmatically

I have an issue in setting the spinner selection. Here is what I am doing: I have an XML in which I have a button and a spinner that is populated with country names. I have not used adapter to populate the spinner instead I have named an array in the string file in the values folder which has a lot of items as country names. Now on selecting a country lets say I select England and click on the button. On clicking a new activity is to start which also has a spinner which is populated with the same list in the same way. But what I want is that the second activity spinner should be showing the selected country that was England in this case as the current item in the spinner. But I can't solve it. Here is piece of code to what I have done

//First activity XML

<Spinner
   android:id="@+id/spinner1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:entries="@array/sehirler"
   android:prompt="@string/sehirsec" />

//Second activity XML

<Spinner
   android:id="@+id/spinner2"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:entries="@array/sehirler"
   android:prompt="@string/sehirsec" />

//Firstactivity code

spinner = (Spinner)findViewById(R.id.spinner1);

button.setOnClickListener(new OnClickListener() { 
    @Override
    public void onClick(View arg0) {
        spinnerItem = spinner .getSelectedItem().toString();

        Intent intent = new Intent(firstactivity.this, secondactivty.class); 
        intent.putExtra("name", spinnerItem);
        startActivity(intent);
    }
}); 

//Second Activity Code

sehir = (Spinner)findViewById(R.id.spinner2);
Intent intent = getIntent();
String name2 = intent.getExtras().getString("name");

sehir.setSelection(getIndex(sehir, name2));

private int getIndex(Spinner spinner, String myString) {
    int index = 0;

    for (int i=0;i<spinner.getCount();i++) {
        if (spinner.getItemAtPosition(i).equals(myString)) {
           index = i;
        }
    }
        return index;
    }
}

Upvotes: 3

Views: 1870

Answers (1)

Andrew El-Masry
Andrew El-Masry

Reputation: 129

It seems that your spinner2 is not in the second activity but in a completely different layout. Check and see if you have an adapter. If you have a custom adapter, then you have to pass that value of position to the constructor of your custom adapter.

In your second activity, pass the position value to the CustomAdapter constructor:

Intent intent = getIntent();
position = intent.getIntExtra("position", 0);

CustomAdapter jsonCustomAdapter = new CustomAdapter(SecondActivity.this, list, position);

In your CustomAdapter, set it up like this:

public class CustomAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<ItemObject> list;
    private int spinnerPosition;

    public CustomAdapter(Context context, List<ItemObject> list, int position) {
        inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.list = list;
        spinnerPosition = position;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder listViewHolder;
        if(convertView == null){
            listViewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.list, parent, false);
            listViewHolder.spinner = (Spinner)convertView.findViewById(R.id.spinner2);
            convertView.setTag(listViewHolder);
        } else {
           listViewHolder = (ViewHolder)convertView.getTag();
        }

        listViewHolder.spinner.setSelection(spinnerPosition);
        return convertView;
    }

    static class ViewHolder{
        Spinner spinner;
    }
}

Upvotes: 1

Related Questions