2015-07-11 71 views
0

嗨我有一個列表視圖中的複選框 我有八個選擇,我希望用戶能夠只檢查四個,然後鎖定複選框,除非他取消選中。如何確定可以在Android中檢查的複選框的數量?

這是我公司主營:

public class Ch2 extends Activity implements OnItemClickListener{ 
    ListView lv; 
    ArrayAdapter<Model> adapter; 
    List<Model> list = new ArrayList<Model>(); 
    int num; 

    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     setContentView(R.layout.activity_ch2); 

     lv = (ListView) findViewById(R.id.listView2); 
     adapter = new cusCh2(this,getModel()); 
     lv.setAdapter(adapter); 
     lv.setOnItemClickListener(this); 
    } 

    @Override 
    public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) { 
     TextView label = (TextView) v.getTag(R.id.label); 
     final CheckBox checkbox = (CheckBox) v.getTag(R.id.check); 
     checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){ 

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

        Toast.makeText(getApplicationContext(),"hi",Toast.LENGTH_LONG); 

        // disable checkbox 
       } 
      } 
     }); 
     Toast.makeText(v.getContext(), label.getText().toString()+" "+isCheckedOrNot(checkbox), Toast.LENGTH_LONG).show(); 

    } 

    private String isCheckedOrNot(CheckBox checkbox) { 
     if(checkbox.isChecked()) { 
      num++; 
      if (num == 4) { 
       checkbox.setEnabled(false); 

      } 
      return "is checked"; 

     } else 
      return "is not checked"; 
    } 


    private List<Model> getModel() { 
     list.add(new Model("a")); 
     list.add(new Model("b")); 
     list.add(new Model("c")); 
     list.add(new Model("d")); 
     list.add(new Model("e")); 
     list.add(new Model("f")); 
     list.add(new Model("m")); 
     list.add(new Model("o")); 
     return list; 
    } 

,這我ArrayAdapter

public class cusCh2 extends ArrayAdapter<Model> { 

    private final List<Model> list; 
    private final Activity context; 
    boolean checkAll_flag = false; 
    boolean checkItem_flag = false; 


    public cusCh2(Activity context, List<Model> list) { 
     super(context, R.layout.row, list); 
     this.context = context; 
     this.list = list; 
    } 

    static class ViewHolder { 
     protected TextView text; 
     protected CheckBox checkbox; 


    } 

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



    ViewHolder viewHolder = null; 
    if (convertView == null) { 
     LayoutInflater inflator = context.getLayoutInflater(); 
     convertView = inflator.inflate(R.layout.row, null); 
     viewHolder = new ViewHolder(); 
     viewHolder.text = (TextView) convertView.findViewById(R.id.label); 
     viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.check); 
     final ViewHolder finalViewHolder = viewHolder; 
     final ViewHolder finalViewHolder1 = viewHolder; 
     final ViewHolder finalViewHolder2 = viewHolder; 
     viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag. 
       list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state. 

      } 
     }); 
     convertView.setTag(viewHolder); 
     convertView.setTag(R.id.label, viewHolder.text); 
     convertView.setTag(R.id.check, viewHolder.checkbox); 
    } else { 
     viewHolder = (ViewHolder) convertView.getTag(); 
    } 
    viewHolder.checkbox.setTag(position); // This line is important. 

    viewHolder.text.setText(list.get(position).getName()); 
    viewHolder.checkbox.setChecked(list.get(position).isSelected()); 

    return convertView; 
} 
} 

,這我的模型

public class Model { 

    private String name; 
    private boolean selected; 

    public Model(String name) { 
     this.name = name; 
    } 

    public String getName() { 
     return name; 
    } 

    public boolean isSelected() { 
     return selected; 
    } 

    public void setSelected(boolean selected) { 
     this.selected = selected; 
    } 
當然

row.xml文件

按我說的,我想要讓我的用戶最多可以查看四個框。

的解決方案,我試過:

@Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
     int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag. 
     list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state. 
     if(!isChecked){ 
      num++; 

      if(num>3){ 
       finalViewHolder.checkbox.setChecked(false);} 
      if(!isChecked){ 
       num--; 
       if(num<3){ 
        finalViewHolder.checkbox.setChecked(true);} 
      } 


     } 
    } 
}); 

它的工作爲停止檢查,但是當我滾動列表,我失去了什麼,我已經選中。

編輯2:

 viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

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

       int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag. 
       list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state. 

       Toast toast= Toast.makeText(getContext(), String.valueOf(getPosition), Toast.LENGTH_SHORT); 

       if (isChecked) { 
        num++; 
        l.add(String.valueOf(getPosition)); 
        if(l.size() > 3){  
         finalViewHolder.checkbox.setChecked(false); 
        }   
        toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0); 
        toast.show(); 
       } 

        else if(!isChecked){ 
        finalViewHolder.checkbox.setChecked(true); 
        } 

      } 

     }); 

還當我滾動我不知所措,我已經檢查

編輯2:

viewHolder.checkbox.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       int getPosition = (Integer) v.getTag(); // Here we get the position that we have set for the checkbox using setTag. 
       list.get(getPosition).setSelected(finalViewHolder.checkbox.isChecked()); // Set the value of checkbox to maintain its state. 
       Toast toast= Toast.makeText(getContext(), String.valueOf(getPosition), Toast.LENGTH_SHORT); 

       if (finalViewHolder.checkbox.isChecked()) { 

        l.add(String.valueOf(getPosition)); 
        toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0); 
        toast.show(); 

        if (l.size() > 4) { 
         finalViewHolder.checkbox.setChecked(false); 
        } 


        if (!finalViewHolder.checkbox.isChecked()) { 
         finalViewHolder.checkbox.setChecked(true); 

        } 

       } 
      } 

     } 
    ); 

我希望有人能幫助我,謝謝。

回答

0

玉傢伙THX爲你的時候,我發現我的解決方案

即使你所寫的checkbox.setChecked(假)的問題; 即使不與我的Arraylist相比,Android仍然算作檢查成爲增加有史以來檢查或不是我停止這個使列表刪除最後一項時,成爲setChecked(false); 現在它的做工精細 這個解決方案的代碼

 viewHolder.checkbox.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       int getPosition = (Integer) v.getTag(); 

       if (finalViewHolder.checkbox.isChecked()) { 

        l.add(getItem(getPosition).getName()); 
        Toast toast= Toast.makeText(getContext(),String.valueOf(l.size()), Toast.LENGTH_SHORT); 
        toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0); 
        toast.show(); 

        if (l.size() > 4) { 
         finalViewHolder.checkbox.setChecked(false); 
         l.remove(getItem(getPosition).getName()); 



        } 

       } 
        else if (!finalViewHolder.checkbox.isChecked()) { 
         l.remove(getItem(getPosition).getName()); 

        } 

       list.get(getPosition).setSelected(finalViewHolder.checkbox.isChecked()); 
      } 

     } 
    ); 
0

這是因爲,當您滾動列表setOnCheckedChangeListener將called.evrytime時getView叫...而不是此複選框上使用

onClickListener
checkbox.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 

        if (checkbox.isChecked()) { 

        } else if (!checkbox.isChecked()) { 

         } 
        } 

       } 
      }); 
+0

以及本作另一個問題 鎖後,如果嘗試校驗,那麼然後滾動背框將被選中自動 – chaos

+0

CNü分享我們的代碼...ü必須失去somethng ..我想再次調用你的活動的創建者..如果你鎖定或解鎖 – Meenal

+0

我編輯我的問題,並添加我的代碼,工作正常,但一個問題,當鎖定並嘗試檢查一個是的,我不能但當滾動和回到一個我試過它檢查自動 – chaos

0

您可以使用存儲檢查items.You名單可以在OnCheckChangelistner/OnClicklistner.iF中將選中的項目放入列表中,用戶取消選中該項目並且它已經在列表中,而不是從列表中移除它。並且總是檢查列表的大小以計算項目。您可以選擇列表類型喜歡: - 列表或任何存儲ID的東西。

 holder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      // TODO Auto-generated method stub 
       Log.e("checked : ", "Clicked on Checkbox: " +contactList.get(position).getName() +" is " + isChecked); 




       ContactPhone cp = contactList.get(position); 
       if(isChecked){ 
        cp.setState(isChecked); 
        addToSelected(cp); 

       }else{ 
        cp.setState(isChecked); 
        removeSelected(cp); 
       } 

       if(selected_contats.size() > 4){ 

       }else{ 

        } 
       } 

     } 
    }); 

完蛋了......

+0

它的工作,但我有同樣的問題當鎖定和我滾動取消選中所有複選框 – chaos

+0

你評論代碼(複選框desable)後檢查它代碼 –

+0

我更新我的問題,並添加我試圖 因爲我說這是工作,但當滾動我失去了我檢查 – chaos

相關問題