2013-06-26 97 views
0

我在保持複選框狀態在列表視圖中有問題。我已經嘗試了很多關於這個問題的stackoverflow問題。但還沒有找到任何解決方案。請有人幫助我。這是我的自定義適配器中的getView方法。在列表視圖中保持複選框狀態

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 



     View rowView = inflater.inflate(R.layout.row_sub_packages, parent, 
       false); 

     TextView title = (TextView) rowView.findViewById(R.id.textView1); 

     TextView desc = (TextView) rowView.findViewById(R.id.textView2); 

     TextView sub = (TextView) rowView.findViewById(R.id.TextView01); 
     TextView book = (Button) rowView.findViewById(R.id.button2); 

     CheckBox fav = (CheckBox) rowView.findViewById(R.id.check1); 

     Flag.clear(); 
     Flag = dh.getData(6, "packages"); 

     boolean flag = false; 

     for (int i = 0; i < Flag.size(); i++) { 

      if (Flag.get(i) 
        .equals(SubPackagesFragment.Sub_cat_Id[position])) { 

       Log.i("Flag true", "Flag true " + Flag.get(i) + " " 
         + SubPackagesFragment.Sub_cat_Id[position]); 

       flag = true; 
      } 

      else { 
       flag = false; 
      } 

     } 

     if (flag == true) { 
      fav.setChecked(true); 
     } 

     else { 
      fav.setChecked(false); 
     } 

     fav.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, 
        boolean isChecked) { 
       // TODO Auto-generated method stub 

       if (isChecked == true) { 

        dh.addfav(Title[position], sub_cat_Sub[position], 
          sub_cat_des[position], Images[position], 
          "packages", 
          SubPackagesFragment.Sub_cat_Id[position]); 

        // Toast.makeText(getContext(), 
        // Title[position]+"+"+SubPackagesFragment.Sub_cat_Id[position], 
        // Toast.LENGTH_SHORT).show(); 

       } else { 
        // Toast.makeText(getContext(), "off", 
        // Toast.LENGTH_SHORT).show(); 

        dh.delete(SubPackagesFragment.Sub_cat_Id[position], 
          "packages"); 

       } 

      } 
     }); 


     title.setText(Title[position]); 




     return rowView; 

} 
+0

http://stackoverflow.com/questions/17267859/how-to-obtain-the-checked-rows-in-a-custom-view-list/17268392#17268392。嘗試使用'SparseBooleanArray'。或這個http://stackoverflow.com/questions/17234399/in-gridview-checkbox-is-unchecked-while-scrolling-gridview-up-and-down/17234461#17234461 – Raghunandan

回答

1

您通過ArrayList循環,而問題是,當你發現你true仍將循環和檢測false如果true聲明是沒底。因此,在檢測到true後,請嘗試設置break;

+0

這工作!謝謝:) –

+0

沒有probz隊友,接受答案,如果它幫助你!乾杯! – Carnal

相關問題