Skip to content Skip to sidebar Skip to footer

Getting Item Of Customized Spinner

I implemented custom spinner: public class MyAdapter extends ArrayAdapter { public MyAdapter(Context context, int textViewResourceId,

Solution 1:

Objectselection= sp.getSelectedItem();

this returns the selected item which you can query the info from

in your adapter you need to implement getItem(int position)

and return the needed item, selection will be getItem(selectedPosition)

suppose your getItem is

publicStringgetItem(int position){
return data1[position];

}

then String selected = spinner.getSelectedItem();

will return data1[selectedPosition]

For simplicity i would suggest you create a new object

publicclassMyCustomObject{

    String title;
    int imageResource;

}

and your data array will then be

ArrayList<MyCustomObject>data = new ArrayList<MyCustomObject>();

populate this array somehow

and now your row will be

public View getCustomView(int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater=getLayoutInflater();
            View row= inflater.inflate(R.layout.spinner, parent, false);
            TextView label=(TextView)row.findViewById(R.id.textView1);
            label.setText(data.get(position).title);

            ImageView icon=(ImageView)row.findViewById(R.id.imageView1);
            icon.setImageResource(data.get(position).imageResource);

            return row;
            }

and then your getItem will be

public MyCustomObject getItem(int position){

return data.get(position);
}

and then your selection will be

MyCustomObjectselection= spinner.getSelectedItem();

and you can see selection.title will be available as well as selection.imageResource if you need it

Solution 2:

I found a solution that works for me, onItemSelectedListener looks like this:

spnrCategories.setOnItemSelectedListener(newAdapterView.OnItemSelectedListener() {
        @OverridepublicvoidonItemSelected(AdapterView adapterView, View view, int i, long l) {

            Stringitem= ((TextView)view.findViewById(R.id.spinnerTextViewName)).getText().toString();
            Toast.makeText(MainActivity.this, item , Toast.LENGTH_LONG).show();
        }

        @OverridepublicvoidonNothingSelected(AdapterView adapterView) {
        }
    });

This is my custom spinner layout:

<ImageView
    android:id="@+id/spinnerImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:src="@drawable/baseline_add_black_18dp"
    android:layout_weight="0"
    android:padding="10dp"/>

<TextView
    android:id="@+id/spinnerTextViewName"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/spinnerImageView"
    android:padding="11dp"
    android:ellipsize="end"
    android:maxLines="1"
    android:text="Add category1234"
    android:textColor="#000000"
    android:layout_weight="1"
    android:textSize="20dp" />

<TextView
    android:id="@+id/spinnerTextViewNumber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:padding="11dp"
    android:text="(0)"
    android:textColor="#000000"
    android:textSize="20dp" />

found my solution at this site: https://www.zoftino.com/android-spinner-custom-adapter-&-layout

Post a Comment for "Getting Item Of Customized Spinner"