我有一個使用自定義適配器的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