我有一个使用自定义适配器的ListView。 ListView中的每个元素都包含一个RadioButton和一个TextView。无法选择ListView项目
这里是我的适配器,它需要员工的ArrayList(目前只包含一个名称的对象):
public class EmployeeAdapter extends ArrayAdapter<Employee> {
private ArrayList<Employee> listEmployees;
// Give the adapter the context and layout we are operating it, as well as a list of employees to put in the list.
public EmployeeAdapter(Context context, int layout, ArrayList<Employee> listEmployees) {
super(context, layout, listEmployees);
this.listEmployees = listEmployees;
}
// We override the basic getView function since our list is custom.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
// If there is nothing left to put in the view, inflate the view in the rowemployee layout.
if (v == null){
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.rowemployees, null);
}
Employee i = listEmployees.get(position);
// Check if there's still an employee in the list.
if (i != null){
TextView name = (TextView) v.findViewById(R.id.text_employeename);
name.setText(i.getName());
}
// Alternate row colors.
if (position % 2 == 0) {
v.setBackgroundColor(Color.parseColor("#FFFFFF"));
} else {
v.setBackgroundColor(Color.parseColor("#e5fff4"));
}
return v;
}
}
这里是我的XML布局列表视图声明:
<ListView android:id="@+id/employees_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="2dp"
android:paddingRight="1dp"
android:background="@drawable/borderlist"
android:choiceMode="singleChoice"
android:listSelector="#48ad82"
android:layout_below="@id/employees_header">
</ListView>
这里是ListView的每个项目的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/row_employee"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="13dp"
android:paddingBottom="13dp"
android:descendantFocusability="blocksDescendants">
<RadioButton android:id="@+id/radio_employee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:focusable="false"/>
<TextView
android:id="@+id/text_employeename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_toRightOf="@id/radio_employee"
android:layout_marginTop="3dp"
android:layout_marginLeft="5dp"
android:focusable="false" />
</RelativeLayout>
我输入一堆Emp loyee对象转换为一个ArrayList,我将其推送到适配器,然后在列表上设置一个setOnItemClickListener。收听者包含此代码:
OnItemClickListener onItemClickListener_employee
= new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
Toast.makeText(getApplicationContext(), String.valueOf(view.isSelected()) , Toast.LENGTH_SHORT).show();
}
};
view.isSelected()总是返回false。为什么?我一直在试图弄清楚如何在列表中选择包含其他内容而不仅仅是TextView的元素几个小时,并且SDK文档不是很有帮助,这已经变老了。
当我按列表中的项目时,它似乎像TextView或RadioButton被按下而不是。不应该focusable =“false”阻止这()?
看起来像不应该在你的情况下选择视图 - 这里http://stackoverflow.com/questions/12293615/setonitemclicklistener-vs-setonitemselectedlistener-in-listview/12293697#12293697关于选定状态特定的一些解释。此外,http://developer.android.com/reference/android/widget/AbsListView.html#attr_android:choiceMode不是关于选择,而是选择。 – sandrstar
那么如何检查视图(或本例中的列表项)是否已被选中? android:choiceMode =“singleChoice”已经设置在我的ListView上。或者说,我如何拿起被选中的物品?我没有使用Android很长时间,有时我很困惑。 – Sefam
似乎你可以使用http://developer.android.com/reference/android/widget/AbsListView.html#getCheckedItemPosition%28%29 – sandrstar