how can I get selected value in spinner such as C# ComboBox? [duplicate]
Asked Answered
H

2

1

I trying to make a custom Spinner containing Id and Title. This Spinner displays Title but everywhere in my code I can access to selected Id. how can I do it. it's like ComboBox in C#. thanks.

Hummingbird answered 16/1, 2013 at 9:15 Comment(0)
H
1

I solved It! At first I created a custom adapter:

public class SimpleCustomeAdapter extends ArrayAdapter<String>{

Context c;
List<String> id;
List<String> title;
public SimpleCustomeAdapter(Context context, int textViewResourceId,List<String> id, List<String> title) {

    super(context, textViewResourceId,id);

    c=context;
    this.id=id;
    this.title=title;
}

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

    return getCustomView(position, convertView, parent);        
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    return getCustomView(position, convertView, parent);
}

public View getCustomView(int position, View convertView, ViewGroup parent) {
    this.setDropDownViewResource(R.layout.spinner_layout);

    LayoutInflater inflater=LayoutInflater.from(c);
    View row=inflater.inflate(R.layout.spinner_layout, parent, false);
    TextView idTextView=(TextView)row.findViewById(R.id.idCustomspinnertextView);
    idTextView.setText(id.get(position));

    TextView titleTextView=(TextView)row.findViewById(R.id.titleCustomspinnertextView);
    titleTextView.setText(title.get(position));

    Typeface externalFont=Typeface.createFromAsset(c.getAssets(), "font/BKOODB.TTF");
    titleTextView.setTypeface(externalFont);
    titleTextView.setTextSize(25);
    return row;
}

}

and then, I created a custom Spinner:

public class CustomSpinner extends Spinner {
public CustomSpinner(Context context, AttributeSet attrs, int defStyle,
        int mode) {
    super(context, attrs, defStyle, mode);
    // TODO Auto-generated constructor stub
}

public CustomSpinner(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

public CustomSpinner(Context context, int mode) {
    super(context, mode);
    // TODO Auto-generated constructor stub
}

List<String> idList;
List<String> titleList;
public void setAdapter(SimpleCustomeAdapter adapter) {
    // TODO Auto-generated method stub
    super.setAdapter(adapter);
    this.idList= adapter.id;
    this.titleList = adapter.title;
}

public void setSelectedId(long id){
    for (int i = 0; i < idList.size(); i++) {
        if(idList.get(i).contains(id+""))
        {
            this.setSelection(i);
            break;
        }
    }
    Log.d("SpinnerError", "This id is not in adapter!");
}

public CustomSpinner(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

}

Now in my activity code is like this:

    public class MyActivity extends Activity {

CustomSpinner customspinner1;
Button button1;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    customspinner1=(CustomSpinner)findViewById(R.id.customSpinner1);
    button1=(Button) findViewById(R.id.button1);

    List<String> payoffTypeList = new ArrayList<String>();
    List<String> payoffTypeIdList = new ArrayList<String>();

    DatabaseAccess dba = new DatabaseAccess(this);

    Cursor cur = dba.getAllPayofftype();
    if (cur.moveToFirst()) {
        do {
            payoffTypeList.add(cur.getString(1));
            payoffTypeIdList.add(cur.getString(0));
        } while (cur.moveToNext());
    }



    customspinner1.setAdapter(new SimpleCustomeAdapter(this, R.layout.spinner_layout,payoffTypeIdList, payoffTypeList));

    ///example
    customspinner1.setSelectedId(12);

    button1.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Log.d("myTag", "SlectedId = "+customspinner1.getSelectedId());
        }
    });

}
Hummingbird answered 19/1, 2013 at 7:18 Comment(0)
H
0

if you have id's and names in the separate arrays then based on the spinner selected item position you can get selected item id.

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

    public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) 
    {
        System.out.println(Id_arr[spinner.getSelectedItemPosition()]);// for spinner id
        System.out.println(spinner.getSelectedItem().toString());// for spinner item
    }

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

    }
});
Hance answered 16/1, 2013 at 9:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.