简体   繁体   中英

How to get spinner's selected item's view

I'm trying to get a reference to the view of the selected item of my spinner (the layout). I'm using a SimpleCursorAdapter with a custom layout. Here is some of the code for my adapter:

adapter = new SimpleCursorAdapter(getActivity(),
            R.layout.task_row, null,
            new String[] { DbHandler.TASK_TITLE, DbHandler.TASK_NOTE},
            new int[] { R.id.title, R.id.note}, 0);
spinner.setAdapter(adapter);

Here is task_row.xml :

<RelativeLayout>

<ImageView
    android:id="@+id/avatar"
    android:src="@mipmap/ic_launcher"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@string/avatar" />

<TextView
    android:id="@+id/title"
    android:textStyle="bold"
    android:layout_toRightOf="@id/avatar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/note"
    android:layout_toRightOf="@id/avatar"
    android:layout_below="@id/title"
    android:layout_alignParentBottom="true"
    android:gravity="bottom"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</RelativeLayout>

The reason I'm trying to get a reference to the RelativeLayout is so that I can access the text attribute of a TextView child for example. I'm aware that there is a method called getItemSelected() that I can use on my Spinner , but this method returns an object and I don't know what it is.

Instead of using,

  spinner.getSelectedItem()

which return null, so use this :

  ((TextView)(spinner.getSelectedView().findViewById(R.id.title))).getText()

I think it work for you.

In this case, you will be having whole selected view also, which is you requirement i think by:

  spinner.getSelectedView()

您可以使用此代码来帮助将选定的值显示在微调框索引的顶部

                    spinnner.setSelection(arraylist.indexOf(selectedString));

If you want to get a reference to a text view in your layout, I'm assuming so that you can set a string resource based on the selected spinner item the user selects, Put this code in your onCreate method that has the spinner:

// create a reference to the spinner, replace mySpinnerId with actual spinner id
final Spinner mySpinner = (Spinner) findViewById(R.id.mySpinnerId);
// create a reference to the text view, replace myTextViewId with actual text view id
final TextView textView = (TextView) findViewById(R.id.myTextViewId);

    mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    {
        @Override
        public void onItemSelected(AdapterView<?> parameter, View view, int position, long id)
        {
            // get the string value of the spinner item that is selected
            String selectedSpinnerItem = String.valueOf(mySpinner.getSelectedItem());

            // set the description in the Text View if it matches some
            // string resource in your array, replace option1 with actual
            // string resource to obtain
            if (selectedSpinnerItem.equals(getString(R.string.option1)))
            {
                // replace myStringResource with actual string resource name
                textView.setText(R.string.myStringResource);
            }

        @Override
        public void onNothingSelected(AdapterView<?> parent)
        {
        }
    });
}

The caveat for this to work this is that, in your strings.xml file, define your string-array for your spinner like so:

<string name="option1">An option:</string>
<string name="option2">Another option:</string>
<string-array name="spinner_options">
    <item>@string/option1</item>
    <item>@string/option2</item>
</string-array>

I have solved my problem. The Spinner class extends AdapterView<SpinnerAdapter> which has a method called getSelectedView() that does exactly what its name suggests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM