2013-07-11 66 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