Getting item of customized Spinner

こ雲淡風輕ζ 提交于 2020-04-15 06:19:07

问题


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?


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/23348875/getting-item-of-customized-spinner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!