2013-07-11 60 views
1

我有一个使用自定义适配器的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”阻止这()?

+1

看起来像不应该在你的情况下选择视图 - 这里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

+0

那么如何检查视图(或本例中的列表项)是否已被选中? android:choiceMode =“singleChoice”已经设置在我的ListView上。或者说,我如何拿起被选中的物品?我没有使用Android很长时间,有时我很困惑。 – Sefam

+1

似乎你可以使用http://developer.android.com/reference/android/widget/AbsListView.html#getCheckedItemPosition%28%29 – sandrstar

回答

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(); 
} 

由于该视图仅用于显示数据,因此您并不关心视图本身(除非您想将其删除,将其视为高亮等)。

+0

好吧,我想按下单选按钮并突出显示视图。 – Sefam

+0

然后在onItemClick方法中,添加如下代码:view.setBackgroundResource(R.color.hilightColor);和改变单选按钮状态的代码。您还应该查看[StateListDrawable](http://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html)类,而不必在代码中执行此操作。 –

+0

我与StateListDrawable的问题是,我不知道什么状态我应该用于选择的项目。我尝试了state_pressed和state_selected,都没有工作。 – Sefam