【问题标题】:How to get spinner's selected item's view如何获取微调器的选定项目的视图
【发布时间】:2017-11-28 03:44:08
【问题描述】:

我正在尝试获取对我的微调器(布局)的选定项目的视图的引用。我正在使用带有自定义布局的SimpleCursorAdapter。这是我的适配器的一些代码:

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);

这里是 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>

我试图获取对RelativeLayout 的引用的原因是,例如,我可以访问TextView 孩子的text 属性。我知道有一个名为getItemSelected() 的方法可以在我的Spinner 上使用,但是这个方法返回一个对象,我不知道它是什么。

【问题讨论】:

  • 您好,我认为您有一个自定义适配器,并且您想从该适配器获取微调器值
  • 嗨。我正在使用 SimpleCursorAdapter (developer.android.com/reference/android/widget/…)。此处微调器的“值”没有意义,因为显示的是我的自定义布局,其中包含一个 ImageView 和两个 TextView。我想要的是所选项目的 TextView 子项之一的文本属性。如果可以从适配器中获取它,那么当然可以。
  • 您是否尝试过从视图中获取价值?
  • 这就是我想要做的。但我需要一个参考。
  • 好的,我看过了

标签: java android xml android-spinner android-adapter


【解决方案1】:

而不是使用,

  spinner.getSelectedItem()

返回null,所以使用这个:

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

我认为它对你有用。

在这种情况下,您还将拥有整个选定的视图,我认为这是您的要求

  spinner.getSelectedView()

【讨论】:

  • 是的,这就是我想要的。我们几乎同时发布了我们的答案。我接受你的。谢谢。
【解决方案2】:

您可以使用此代码帮助在微调器索引顶部显示所选值

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

【讨论】:

  • 对不起,我不明白你的回答。这如何给我一个布局参考?另外,你的 arraylist 变量是什么?
  • 这里的数组列表引用了您存储整个列表数据的位置。
  • selectedString 显示你想在微调器的第一个位置选择哪个字符串或数据
  • 我没有使用数组,而是使用带有 SimpleCursorAdapter 的 SQLite 数据库,正如我在问题中所说的那样。但无论如何,这如何给我一个布局参考?
  • 很抱歉,但这不是我要求的:/
【解决方案3】:

如果您想在布局中获取对文本视图的引用,我假设您可以根据用户选择的选定微调器项目设置字符串资源,将此代码放在您的 onCreate 方法中微调器:

// 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)
        {
        }
    });
}

需要注意的是,在您的 strings.xml 文件中,为您的微调器定义您的字符串数组,如下所示:

<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>

【讨论】:

  • 这段代码应该去哪里?它无法进入布局包含微调器的活动,因为我尝试访问的 TextView 与微调器不属于同一布局。
  • @JalilCompaoré 哦,我明白你现在在说什么了。但我认为这不重要,因为您通过 id 引用了文本视图,您尝试过吗?
  • @JalilCompaoré 所以,我认为这段代码仍然适用,你想把它放在有微调器的活动 onCreate 方法中
  • 不能申请。当我的代码运行时,我的微调器中有多个项目,这意味着我有多个具有相同 ID 的 TextView。 findViewById(R.id.myTextViewId) 会返回哪一个?
  • @JalilCompaoré 你不能完全按原样使用所有代码,你可以使用大部分代码,但你必须用你想要的文本视图的实际 id 替换 myTextViewId 之类的东西。和 R.string.option1 以及您想要引用的实际字符串引用,以及 myStringResource,以及您想要的实际字符串资源。所以如果你想引用@+id/note 文本视图,那么说TextView textView = (TextView) findViewById(R.id.note);与 Spinner 参考相同。
【解决方案4】:

我已经解决了我的问题。 Spinner 类扩展了 AdapterView&lt;SpinnerAdapter&gt;,它有一个名为 getSelectedView() 的方法,正如它的名字所暗示的那样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多