我得到了一个Spinner
元素,我用Cursor
使用SimpleCursorAdapter
填充数据。另外我使用setViewBinder
作为Spinner
的自定义行布局。一切正常,Spinner
获取数据和Spinner
项目使用自定义布局。使用SimpleCursorAdapter和ViewBinder为微调项目单击监听器的方法
但点击从Spinner
下拉列表查看的项目不会做任何事情。它不会将选定项目设置为选中状态,并且不会关闭下拉视图。我不知道我必须做什么,所以从列表中选择的项目被传递给Spinner
逻辑并按照它应该运行。下面是我使用的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:clickable="true"
android:orientation="horizontal">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:layout_weight="1"
android:textColor="#424242"
android:gravity="center_vertical"
android:text="Textfield" />
</LinearLayout>
</LinearLayout>
和这里的ViewBinder
:
static final ViewBinder VIEW_BINDER = new ViewBinder(){
public boolean setViewValue(View view, Cursor cursor, int columnIndex){
if (view.getId() == R.id.text){
String local = view.getResources().getString(cursor.getInt(columnIndex));
((TextView) view).setText(local);
return true;
}
if (view.getId() == R.id.icon){
int icon = cursor.getInt(columnIndex);
((ImageView) view).setImageResource(icon);
return true;
}
return false;
}
};
,这里是我如何将数据添加到Spinner
:
private Spinner spinner;
private DBHandler dbhandler;
private SimpleCursorAdapter adapter;
private final String[] from = new String[]{dbhandler.LIB_LOCAL, dbhandler.LIB_ICON};
private final int[] to = { R.id.text, R.id.icon };
@Override
protected void onResume(){
super.onResume();
Cursor cursor = dbhandler.getLibEntries();
adapter = new SimpleCursorAdapter(this, R.layout.spinner_row, cursor, from, to);
adapter.setViewBinder(VIEW_BINDER);
spinner.setAdapter(adapter);
}
添加OnItemSelectedListener
类似建议在这篇文章中实现了如下,但并没有解决问题。另外我不知道怎么setOnItemSelectedListener
可以帮助我得到我需要后来的数据字段:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
好吧,它不是一个自定义的微调,它只是使用自定义视图的项目。当使用标准微调器时,无论是否设置了监听器,都会执行选择。我不想要任何特殊的活动来执行,但只有我能够选择一个项目,并且当我调用getSelectedItem()时,下拉框关闭并返回选定的项目。是否有必要为此使用自己的听众? +如果是这样,我必须通知下拉列表已关闭并选择了该项目? – masi
对不起,误解了你的问题。我编辑了我的答案。 –
好吧,我设置了一个OnItemSelectedListener。但那不是问题。事情就是 - >不可能< - 从下拉列表中选择一个项目。我唯一能做的就是在布局中添加一个onClick,但是没有办法让这个事件达到微调。我编辑了一些更多的代码和图像,使其更加清晰。 – masi