2016-12-22 1 views
-2

listview Zeile mit mehreren checkboxex, wenn ich die checkboxex dann wenn ich die Listenansicht scrollen, automatisch die Kontrollkästchen deaktiviert. Wie kann dieses Problem behoben werden? hier ist meine getView MethodeKontrollkästchen deaktiviert, wenn ich die benutzerdefinierte Listview Android

 public View getView(final int position, View convertView, ViewGroup parent) { 
      final QuestionRadioHolder questionRadioHolder; 
      final QuestionCheckboxHolder questionCheckboxHolder; 
      final Model rowItem = getItem(position); 
      final int type = getItemViewType(position); 

      if (type == TYPE1) { 
       if (convertView == null) { 
        convertView = mInflater.inflate(R.layout.radio_layout, parent, false); 
        questionRadioHolder = new QuestionRadioHolder(); 
        questionRadioHolder.txtQuestion = (TextView) convertView.findViewById(R.id.txtQue); 
        questionRadioHolder.rg = (RadioGroup) convertView.findViewById(R.id.rg); 

        Typeface face = Typeface.createFromAsset(getContext().getAssets(), "fonts/Raleway-SemiBold.ttf"); 
        questionRadioHolder.txtQuestion.setTypeface(face); 
        convertView.setTag(questionRadioHolder); 

       } else { 
        questionRadioHolder = (QuestionRadioHolder) convertView.getTag(); 
       } 



       List<String> arrayList = rowItem.getArrayList(); 
       questionRadioHolder.txtQuestion.setText(rowItem.getQuestionText()); 
       questionRadioHolder.rg.removeAllViews(); 
       questionRadioHolder.rg.clearCheck(); 
       for (int i = 0; i < arrayList.size(); i++) { 
        RadioButton radioButton = new RadioButton(getContext()); 
        radioButton.setId(i); 
        radioButton.setTextSize(12); 
        radioButton.setText(arrayList.get(i)); 
        radioButton.setTypeface(face1); 
        questionRadioHolder.rg.addView(radioButton); 
       } 

//    questionRadioHolder.rg.removeAllViews(); 
       questionRadioHolder.rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
        public void onCheckedChanged(RadioGroup rg, int checkedId) { 
         for (int i = 0; i < rg.getChildCount(); i++) { 
          RadioButton btn = (RadioButton) rg.getChildAt(i); 
          ArrayList<String> list = new ArrayList<String>(); 
          if (btn.getId() == checkedId) { 
           String text = btn.getText().toString(); 
           int id = mList.get(position).getQuid(); 
           System.out.println(position + " position 2"); 
           map.put(id, text); 
           list.add(0, text); 
           aMap1.put(id, list); 
           Log.d("radio", text); 
           Log.d("Queid:", "" + id); 
           return; 
          } 
         } 
        } 
       }); 
      } else if (type == TYPE2) { 
       if (convertView == null) { 
        convertView = mInflater.inflate(R.layout.checkbox_layout, parent, false); 
        questionCheckboxHolder = new QuestionCheckboxHolder(); 
        questionCheckboxHolder.txtQuestionChk = (TextView) convertView.findViewById(R.id.chkQues); 
        questionCheckboxHolder.relativeLayout = (LinearLayout) convertView.findViewById(R.id.chkrl2); 

        Typeface face = Typeface.createFromAsset(getContext().getAssets(), "fonts/Raleway-SemiBold.ttf"); 
        face1 = Typeface.createFromAsset(getContext().getAssets(), "fonts/Raleway-Regular.ttf"); 
        questionCheckboxHolder.txtQuestionChk.setTypeface(face); 

        convertView.setTag(questionCheckboxHolder); 
       } else { 
        questionCheckboxHolder = (QuestionCheckboxHolder) convertView.getTag(); 
       } 


       List<String> arrayList = rowItem.getArrayList(); 
       questionCheckboxHolder.txtQuestionChk.setText(rowItem.getQuestionText()); 

       questionCheckboxHolder.relativeLayout.removeAllViews(); 

       for (int i = 0; i < arrayList.size(); i++) { 
        checkBox = new CheckBox(getContext()); 
        checkBox.setId(i); 
        checkBox.setMinHeight(30); 
        checkBox.setMinimumWidth(30); 
        checkBox.setText(arrayList.get(i)); 
        checkBox.setTypeface(face1); 
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
         @Override 
         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 

          int position = (Integer) buttonView.getTag(); 


          ArrayList<String> arraylist2 = new ArrayList<String>(); 
          ArrayList<String> list = new ArrayList<String>(); 
          int id = mList.get(position).getQuid(); 
          arraylist2 = aMap1.get(id); 
          System.out.println(arraylist2); 

          if (arraylist2 == null) { 
           arraylist2 = list; 
          } 
          if (buttonView.isChecked()) { 
           arraylist2.add(buttonView.getText().toString()); 
           aMap1.put(id, arraylist2); 

          } else { 
           arraylist2.remove(buttonView.getText().toString()); 
           aMap1.put(id, arraylist2); 
          } 
          System.out.println(aMap1.get(id)); 

         } 
        }); 




        questionCheckboxHolder.relativeLayout.addView(checkBox); 
       } 
      } 

      return convertView; 
     } 

Wenn ich die Listenansicht bewegen, ihr automatisch nicht markiert das Kontrollkästchen, wie kann das Kontrollkästchen aktiviert.

+0

Sie müssen Checkboxes Wert in der Modellklasse speichern und wenn scrooling passieren einfach Wert von Modell erhalten und Wunschkästen überprüfen –

+0

dies lesen> http://lalit3686.blogspot.in/2012/06/today-iam-going- to-show-how-to-deal.html –

+3

Mögliches Duplikat von [Kontrollkästchen nicht aktiviert, wenn ich in Android auf Listenansicht blättern] (http://stackoverflow.com/questions/10895763/checkbox-unchecked-when-i-scroll-listview- in-android) –

Antwort

0

Wenn Sie auf checkbox geprüft, können Sie es in ein HashMap(key:position, value:true/false) und in Verfahren sparen getView() Sie können unterscheiden, welche aktiviert oder deaktiviert ist, dann machen Sie das Kontrollkästchen zeigen ihren Zustand.

0

versuchen, eine Modellklasse oder Array Unter Verwendung der Werte von checkbox zu halten und es auf onclick zu aktualisieren, wenn das checkbox Ihr Problem lösen kann, denke ich.

holder.checkBox.setChecked(mCheckList.get(position)); 
      holder.checkBox.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       mCheckList.set(position, !mCheckList.get(position)); 
       notifyDataSetChanged(); 
      } 
      }); 
0

Speichern Sie Ihre Checkliste in ArrayList<Boolean>, und laden Sie es in getView():

ArrayList<Boolean> listCheck; 

public YourAdapter(Context context, int resource, ArrayList<T> list) { 
      super(context, resource, list); 
    listCheck = new ArrayList<Boolean>; 
    for (int i = 0; i < list.size(); i++) { 
     listCheck.add(false); // init listCheck 
    } 
} 

public View getView(final int position, View convertView, ViewGroup parent) { 
    yourCheckBox.setChecked(listCheck.get(position)); // set checkbox 
    yourCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      listCheck.set(position, isChecked); // update listChecked 
     } 
    } 
} 
+0

aber ich habe mehrere Check-in Listview Zeile. wie macht man? kannst du mich führen? –

+0

Meinst du das in jeder Reihe hast du mehrere Schecks? Kannst du deine Bedürfnisse oder Bilder beschreiben? – hyperloop

1

auf diese Weise versuchen dies für mich gearbeitet

public class CustomAdapter extends BaseAdapter { 
    private final LayoutInflater inflater; 
    private final Context context; 
    private List<ModelCls> listData; 

    public CustomAdapter(Context mainActivity, List<ModelCls> listData) { 
     context = mainActivity; 
     this.listData = listData; 
     inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    @Override 
    public int getCount() { 
     return listData.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return listData.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

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

     if (convertView == null) { 
      holder = new ViewHolder(); 
      convertView = inflater.inflate(R.layout.list_item_poojaselection, null); 
      holder.tv = (TextView) convertView.findViewById(R.id.list_item_poojaname); 
      holder.checks = (CheckBox) convertView.findViewById(R.id.list_item_poojacheck); 
      convertView.setTag(holder); 
     }else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 
     holder.checks.setOnCheckedChangeListener(null); 
     holder.checks.setFocusable(false); 

     if (listData.get(position).isselected) { 
      holder.checks.setChecked(true); 
     } else { 
      holder.checks.setChecked(false); 
     } 

     holder.checks.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton cb, boolean b) { 

       if (checkMaxLimit()) { 

        if (listData.get(position).isselected && b) { 
         holder.checks.setChecked(false); 
         listData.get(position).isselected = false; 

        } else { 
         holder.checks.setChecked(false); 
         listData.get(position).isselected = false; 
         Toast.makeText(context, "Max limit reached", Toast.LENGTH_SHORT).show(); 
        } 
       } else { 
        if (b) { 
         listData.get(position).isselected = true; 
        } else { 
         listData.get(position).isselected = false; 
        } 
       } 
      } 
     }); 

     holder.tv.setText(listData.get(position).getLISTING_NAME()); 
     return convertView; 
    } 

    public boolean checkMaxLimit() { 
     int countermax = 0; 
     for(ModelCls item : listData){ 
      if(item.isselected){ 
       countermax++; 
      } 
     } 
     return countermax >= 5; 
    } 

    public class ViewHolder { 
     TextView tv; 
     public CheckBox checks; 
    } 
} 

und wenn Sie diese

recyclerview wollen versuchen,

http://android-pratap.blogspot.in/2015/01/recyclerview-with-checkbox-example.html

Verwandte Themen