本文介绍了越来越checkboxId动态创建的复选​​框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void getcheckbox() 
  {
    int cnter;
    linearMain = (LinearLayout) findViewById(R.id.linearLayout2);
    alphabet = new LinkedHashMap<String, String>();

    for (cnter = 0; cnter < str_arr;)

        if (!optionsarray.get(cnter).isEmpty()) 
        {
            cnter++;
        } else
            break;

    alphabet.put("1", Option_A_new);
    alphabet.put("2", Option_B_new);
    alphabet.put("3", Option_C_new);
    alphabet.put("4", Option_D_new);
    alphabet.put("5", Option_E_new);
    alphabet.put("6", Option_F_new);
    alphabet.put("7", Option_G_new);
    alphabet.put("8", Option_H_new);
    alphabet.put("9", Option_I_new);
    alphabet.put("10", Option_J_new);


    Set<?> set = alphabet.entrySet(); 
    Iterator<?> i = set.iterator(); 
    int cnt = 0;
    while (cnt < cnter) 
    {
        @SuppressWarnings("rawtypes")
        final Map.Entry me = (Map.Entry) i.next();
        checkBox = new CheckBox(this);
        checkBox.setId(Integer.parseInt(me.getKey().toString()));
        checkBox.setTextColor(getResources().getColor(R.color.black));
        checkBox.setText(me.getValue().toString());
        linearMain.addView(checkBox);
        cnt++;

    }
 checkBox.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v) {

            int checkBoxId = ((CheckBox) v).getId();

            switch(checkBoxId) {
            case 1:

                selectedcheckboxid ="A";        
                testAnswernew(selectedcheckboxid);
                break;

            case 2:

                selectedcheckboxid = "B";
                testAnswernew(selectedcheckboxid);
                break;                  
            case 3:
                selectedcheckboxid = "C";   
                testAnswernew(selectedcheckboxid);
                break;
            case 4:
                selectedcheckboxid = "D";
                testAnswernew(selectedcheckboxid);
                break;                  
            case 5:

                selectedcheckboxid = "E";   
                testAnswernew(selectedcheckboxid);
                break;
            case 6:
                selectedcheckboxid = "F";
                testAnswernew(selectedcheckboxid);
                break;                  
            case 7:
                selectedcheckboxid = "G";                   
                testAnswernew(selectedcheckboxid);
                break;
            case 8:
                selectedcheckboxid = "H";
                testAnswernew(selectedcheckboxid);
                break;  


            case 9:
                selectedcheckboxid = "I";                   
                testAnswernew(selectedcheckboxid);
                break;
            case 10:
                selectedcheckboxid = "J";
                testAnswernew(selectedcheckboxid);
                break;


            default:

        }
解决方案

This problem can be solved when we apply "setOnClickListener" on child check box of parent dynamic created Linear layout. I have added check box using "getLayoutInflater()" and set id for each check box.For it u can see below code.

for (int i = 0; i < JAS.length(); i++) {

   View child = getLayoutInflater().inflate(R.layout.return_child, null);
   chk = (CheckBox) child.findViewById(R.id.Product);
   chk.setText(c.optString("name"));
   chk.setId(i);

   ll.addView(child);

   chk.setOnClickListener(new View.OnClickListener() {
       public void onClick(View v) {
           String chk = null;
           chk = Integer.toString(v.getId());
           if (((CheckBox) v).isChecked()) {
               selchkboxlist.add(chk);

           } else {
               selchkboxlist.remove(chk);
           }
       }
   });
}

Here selchkboxlist is "List" same as below :

List selchkboxlist = new ArrayList();

Now On button click you can apply below code for accessing id of selected check box.

ReturnProducts.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        for (int p=0;p<selchkboxlist.size();p++){
            String Length = selchkboxlist.get(p);
            int ln = Integer.parseInt(Length);
            Toast.makeText(ReturnOrders.this, ""+ln, Toast.LENGTH_LONG).show();
        }

    }
});

这篇关于越来越checkboxId动态创建的复选​​框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 00:50