2013-03-05 87 views
1

的滾動狀態改變我的ListView行中使用Sectioned ListView在我的應用 我有TextViewRadioGroup. 所以當我點擊單選按鈕,通常它被點擊和工作正常。 但是,當我點擊並滾動視圖時,所選視圖的 消失,並且變得未選中。單選按鈕上的分區域列表視圖

這是我使用

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/relativelay" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/list_bg" > 

    <TextView 
     android:id="@+id/tvname" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:maxLines="1" 
     android:padding="10dp" 
     android:textColor="#000000" 
     android:textSize="18dp" 
     android:textStyle="bold" /> 

    <RadioGroup 
     android:id="@+id/radioGroup1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:orientation="horizontal" > 

     <RadioButton 
      android:id="@+id/check_present" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_marginRight="20dp" 
      android:onClick="onPresentClick" /> 

     <RadioButton 
      android:id="@+id/check_absent" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toLeftOf="@id/check_present" 
      android:onClick="onPresentClick" /> 
    </RadioGroup> 

</RelativeLayout> 

的XML這是我使用適配器的Sectioned ListView ....

這是我更新的代碼....

public class NamesAdapter extends ArrayAdapter<Item> { 

    private ArrayList<Item> items; 
    private LayoutInflater vi; 
    private Item objItem; 
    private ArrayList<Items> presentSelected = new ArrayList<Items>();; 
    private ArrayList<Items> absentSelected = new ArrayList<Items>();; 
    private Map<Integer, Boolean> map = new HashMap<Integer, Boolean>(); 
    ViewHolderSectionName holderSection; 
    ViewHolderName holderName; 
    Items item; 

    public NamesAdapter(Context context, ArrayList<Item> items) { 
     super(context, 0, items); 

     this.items = items; 

     vi = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 

     objItem = items.get(position); 

     if (objItem.isSectionItem()) { 
      ItemsSections si = (ItemsSections) objItem; 

      if (convertView == null 
        || !convertView.getTag().equals(holderSection)) { 
       convertView = vi.inflate(R.layout.alphabet_separator, null); 

       holderSection = new ViewHolderSectionName(); 
       convertView.setTag(holderSection); 
      } else { 
       holderSection = (ViewHolderSectionName) convertView.getTag(); 
      } 

      holderSection.section = (TextView) convertView 
        .findViewById(R.id.alphabet_letter); 

      holderSection.section 
        .setText(String.valueOf(si.getSectionLetter())); 

     } else { 
      Items ei = (Items) objItem; 

      if (convertView == null || !convertView.getTag().equals(holderName)) { 
       convertView = vi.inflate(R.layout.row, null); 

       holderName = new ViewHolderName(); 

       holderName.name = (TextView) convertView 
         .findViewById(R.id.tvname); 

       holderName.radioGroup = (RadioGroup) convertView 
         .findViewById(R.id.radioGroup1); 
       holderName.checkAbsent = (RadioButton) convertView 
         .findViewById(R.id.check_absent); 
       holderName.checkPresent = (RadioButton) convertView 
         .findViewById(R.id.check_present); 

       Boolean present = map.get(position); 

       Log.e("Names Adapter", "is Selected " + present); 

       if (present != null && present.booleanValue()) { 
        // set present checkbox selected 
        holderName.checkPresent.setSelected(true); 
       } else { 
        holderName.checkAbsent.setSelected(true); 
        // set absent checkbox selected 
       } 

       holderName.radioGroup 
         .setOnCheckedChangeListener(new OnCheckedChangeListener() { 

          @Override 
          public void onCheckedChanged(RadioGroup group, 
            int checkedId) { 
           // TODO Auto-generated method stub 
           switch (checkedId) { 
           case R.id.check_absent: 
            item = (Items) holderName.checkAbsent 
              .getTag(); 
            absentSelected.add(item); 
            map.put(position, true); 
            break; 

           case R.id.check_present: 
            item = (Items) holderName.checkPresent 
              .getTag(); 
            presentSelected.add(item); 
            map.put(position, true); 
            break; 

           default: 
            break; 
           } 
          } 
         }); 

       /* 
       * holderName.checkAbsent .setOnCheckedChangeListener(new 
       * CompoundButton.OnCheckedChangeListener() { 
       * 
       * @Override public void onCheckedChanged(CompoundButton 
       * buttonView, boolean isChecked) { // TODO Auto-generated 
       * method stub holderName.checkAbsent.setSelected(isChecked); // 
       * selected1.add(); 
       * 
       * } }); 
       * 
       * holderName.checkPresent .setOnCheckedChangeListener(new 
       * CompoundButton.OnCheckedChangeListener() { 
       * 
       * @Override public void onCheckedChanged(CompoundButton 
       * buttonView, boolean isChecked) { // TODO Auto-generated 
       * method stub holderName.checkAbsent.setSelected(isChecked); // 
       * selected2.add(); 
       * 
       * } }); 
       */ 
       convertView.setTag(holderName); 

      } else { 
       holderName = (ViewHolderName) convertView.getTag(); 
      } 

      if (holderName.name != null) 
       holderName.name.setText(ei.getName()); 
      convertView.setOnCreateContextMenuListener(null); 
     } 
     return convertView; 
    } 

    public static class ViewHolderName { 
     public TextView name; 
     public RadioGroup radioGroup; 
     public RadioButton checkPresent; 
     public RadioButton checkAbsent; 
    } 

    public static class ViewHolderSectionName { 
     public TextView section; 
    } 

} 

回答

3

您應該維護/保留「已檢查」單選按鈕的狀態,以保持向上和向下滾動時進行檢查,因爲在滾動列表視圖上/下時,每個列表項/視圖都由列表重新創建適配器。

您應該通過在單選按鈕上設置監聽器來保存選中的單選按鈕的位置/索引。

您可以使用hashMap將list-item保存爲s KEY和'Checked'狀態作爲值。 當用戶勾選標記框時,添加鍵值到地圖和當用戶取消選擇框而不是從地圖刪除鍵。

在呈現列表視圖中使用此地圖。代碼如下:

private Map<Integer, Boolean> map = new HashMap<Integer, Boolean>();  

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 

    Boolean present = map.get(position); 
    if(present != null && present.booleanValue()){ 
     // set present checkbox selected 
    } else { 
     // set absent checkbox selected 
    } 

    holderName.radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
       switch (checkedId) { 
        case R.id.check_absent: 
         map.put(position, false) 
         break; 
        case R.id.check_present: 
         map.put(position, true) 
         break; 
       } 
     } 
    }); 
} 
+0

讓我知道如果你不能讓我的觀點,這樣我就可以張貼代碼爲您 – 2013-03-05 12:53:36

+0

是@Rakesh Bhalani我得到你的意思..讓我試一次,如果我沒有找到解決方案,然後我會要求你的幫助..並感謝暗示的解決方案... – 2013-03-05 13:13:41

+0

我用你的代碼,但我無法解決這個問題..它不適合我。請幫助我..我正在盡我所能解決它從昨天.... – 2013-03-06 07:34:43