无法选择ListView项目

时间:2013-07-11 03:02:11

标签: java android

我有一个使用自定义适配器的ListView。 ListView中的每个元素都包含一个RadioButton和一个TextView。

这是我的适配器,它需要一个Employees的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声明:

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

我将一堆Employee对象输入到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”阻止这个(Android custom ListView unable to click on items)?

1 个答案:

答案 0 :(得分:1)

要选择项目,请将以下方法添加到适配器(未经测试的代码):

public Employee getItemAt(int position){
    return listEmployees.get(position);
}

然后在你的处理程序中,将位置传递给上面的新适配器方法

public void onItemClick(AdapterView<?> parent, View view, int position, long id){
    Employee e = adapter.getItemAt(position); //or whatever your adapter instance is named
    Toast.makeText(getApplicationContext(), e.getName(), Toast.LENGTH_SHORT).show();
}

由于视图仅用于显示数据,因此您实际上并不关心视图本身(除非您想要删除它,hilight它等)。