如何获取微调器的所选项目的视图
How to get spinner's selected item's view
我正在尝试获取对我的微调器(布局)的所选项目的视图的引用。我正在使用带有自定义布局的 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
上使用,但是这个方法 returns 是一个对象,我不知道它是什么。
您可以使用此代码帮助在微调器索引顶部显示所选值
spinnner.setSelection(arraylist.indexOf(selectedString));
如果您想在布局中获取对文本视图的引用,我假设您可以根据用户选择的选定微调项设置字符串资源,将此代码放入您的 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>
我的问题已经解决了。 Spinner
class extends AdapterView<SpinnerAdapter>
有一个名为 getSelectedView()
的方法,它的功能完全符合它的名字。
而不是使用,
spinner.getSelectedItem()
其中 return 为空,所以使用此 :
((TextView)(spinner.getSelectedView().findViewById(R.id.title))).getText()
我认为它适合你。
在这种情况下,您还将拥有整个选定视图,我认为这是您的要求:
spinner.getSelectedView()
我正在尝试获取对我的微调器(布局)的所选项目的视图的引用。我正在使用带有自定义布局的 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
上使用,但是这个方法 returns 是一个对象,我不知道它是什么。
您可以使用此代码帮助在微调器索引顶部显示所选值
spinnner.setSelection(arraylist.indexOf(selectedString));
如果您想在布局中获取对文本视图的引用,我假设您可以根据用户选择的选定微调项设置字符串资源,将此代码放入您的 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>
我的问题已经解决了。 Spinner
class extends AdapterView<SpinnerAdapter>
有一个名为 getSelectedView()
的方法,它的功能完全符合它的名字。
而不是使用,
spinner.getSelectedItem()
其中 return 为空,所以使用此 :
((TextView)(spinner.getSelectedView().findViewById(R.id.title))).getText()
我认为它适合你。
在这种情况下,您还将拥有整个选定视图,我认为这是您的要求:
spinner.getSelectedView()