2014-02-18 93 views
0

我想在ArrayAdapter中設置ToggleButtons的RadioGroup。我在列表中有兩個選項,所以當我選擇一個ToggleButton打開時,我想另一個關閉。ArrayAdapter中ToggleButtons的Radiogroup

這裏是我的ArrayAdapter代碼:

public class MyAdapter extends ArrayAdapter<String>{ 

    public MyAdapter(Context context, int textViewResourceId, ArrayList<String> objects) { 
     super(context, textViewResourceId, objects); 
    } 

    @Override 
    public View getDropDownView(int position, View convertView,ViewGroup parent) { 
     return getCustomView(position, convertView, parent); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     return getCustomView(position, convertView, parent); 
    } 

    public View getCustomView(final int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = getActivity().getLayoutInflater(); 
     final ViewHolder holder = new ViewHolder(); 
     View row = inflater.inflate(R.layout.psip_settings_callquality_listitem, parent, false); 

     holder.itemTitle = (TextView) row.findViewById(R.id.txtCQViewItem); 
     holder.itemDesc = (TextView) row.findViewById(R.id.txtCQViewDesc); 
     holder.itemToggle = (ToggleButton) row.findViewById(R.id.togCQViewItem); 
     holder.itemToggle.setTag(position); 

     holder.itemToggle.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton togButton, boolean isChecked) { 

      } 

     }); 

     holder.itemTitle.setText(sSettingTitles.get(position)); 
     holder.itemDesc.setText(sSettingDesc.get(position)); 

     return row; 
    } 

    private class ViewHolder { 
     TextView itemTitle; 
     TextView itemDesc; 
     ToggleButton itemToggle; 
    } 
} 

XML

<TextView 
    android:id="@+id/txtCQViewItem" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textStyle="bold" 
    android:textColor="@color/MiscBtnClick" 
    android:layout_margin="5dp" /> 

<TextView 
    android:id="@+id/txtCQViewDesc" 
    android:layout_width="wrap_content" 
    android:layout_below="@+id/txtCQViewItem" 
    android:layout_height="wrap_content" 
    android:textColor="@color/MiscBtnClick" 
    android:layout_toLeftOf="@+id/togCQViewItem" 
    android:layout_margin="5dp" /> 

<ToggleButton 
    android:id="@+id/togCQViewItem" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/txtCQViewItem" 
    android:layout_alignParentRight="true" /> 

任何幫助將是巨大的。謝謝

+0

RadioGroup在哪裏?請分享xml –

+0

尚未實施RadioGroup。那就是我需要幫助的。 – BigT

+0

如果右側列表中的另一個切換按鈕被按下,您想取消選擇切換按鈕? – Triode

回答

0
private int selection = -1; 
ViewHolder holder = null; 
if(convertView == null){ 
    holder = new ViewHolder() 
    LayoutInflater inflater = getActivity().getLayoutInflater(); 
    View row = inflater.inflate(R.layout.psip_settings_callquality_listitem, parent, false); 
    holder.itemTitle = (TextView) row.findViewById(R.id.txtCQViewItem); 
    holder.itemDesc = (TextView) row.findViewById(R.id.txtCQViewDesc); 
    holder.itemToggle = (ToggleButton) row.findViewById(R.id.togCQViewItem); 
    convertView = row; 
    convertView.setTag(holder); 
}else{ 
    holder = (ViewHolder)convertView.getTag(); 
} 

holder.itemToggle.setTag(position); 
holder.itemToggle.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
    @Override 
    public void onCheckedChanged(CompoundButton togButton, boolean isChecked) { 
     if(isChecked && position != selection){ 
      selection = position; 
      notifyDataSetChnaged(); 
     }else if(!isChecked && position == selection){ 
      selection = -1 
     } 
    } 
}); 
holder.itemToggle.setChecked(position == selection); 
holder.itemTitle.setText(sSettingTitles.get(position)); 
holder.itemDesc.setText(sSettingDesc.get(position)); 
return convertView; 

由於ListView使用視圖,您不需要每次都創建視圖。按照我的建議遵循代碼,並讓我知道它正在工作與否。

+0

如何保留togglebutton的選擇狀態?我知道我不能讓'選擇'變量靜態 – BigT

+0

保留?我沒有明白。選擇變量將保存當前選擇,這樣如果您滾動列表,它也將保持選擇 – Triode

+0

就像我離開頁面並返回它一樣。現在這兩個複選框都被關閉。如果我選擇一個,我希望下一次選擇該選項,我將進入頁面 – BigT