Question

I implemented custom spinner:

 public class MyAdapter extends ArrayAdapter<String>
        {

                public MyAdapter(Context context, int textViewResourceId,
                            String[] objects) {
                      super(context, textViewResourceId, objects);
                      // TODO Auto-generated constructor stub
                }
                @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) {

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

                ImageView icon=(ImageView)row.findViewById(R.id.imageView1);
                icon.setImageResource(images[position]);

                return row;
                }

       }

And set to a spinner like this:

spinner_choose_network_spinner.setAdapter(new MyAdapter(this, R.layout.spinner, data1));

My question is how do i get the textview value of the current selected item of my spinner?

Was it helpful?

Solution

Object selection = 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

public String getItem(int position){
return data1[position];

}

then String selected = spinner.getSelectedItem();

will return data1[selectedPosition]

For simplicity i would suggest you create a new object

public class MyCustomObject{

    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

MyCustomObject selection = spinner.getSelectedItem();

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

OTHER TIPS

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

spnrCategories.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView adapterView, View view, int i, long l) {

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

        @Override
        public void onNothingSelected(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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top