3

I'm adding a checkbox in my application dynamically, and I want to delete the records which are checked. But, I'm not getting the ID of the checkbox. How can I do this?

Code:

package com.my.StudentInfoManagement;


public class ListData extends Activity{

    DataHelper dh;
    TableLayout tb;
    CheckBox[] ch=new CheckBox[50];
    EditText ed;
    int a[]=new int[50];
    int k=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listdataxml);
        dh=new DataHelper(this);
        System.out.println("in list data");
        List<String> names= this.dh.selectAll();
        ed=(EditText) findViewById(R.id.ed_id);
        tb=(TableLayout) findViewById(R.id.table);
        int i,j=1;
        TextView name1 = null,id,dob,gender,branch,email,address,mobile;
        String name11,id1 = null,dob1,gender1,branch1,email1,address1,mobile1;
        TableRow tr=new TableRow(this);

        tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        TextView tv=new TextView(this);

        String c = null;
        String data[]=new String[50];

        int cnt=0;
        for(String name:names)
            {       
                if((!name.equals("-999")))
                {
                    data[cnt]=name;
                    cnt++;
                    System.out.println("........."+name);
                }
                else
                {
                    cnt=0;
                    name1=new TextView(this);
                    name1.setText(data[1]+" ");
                    id=new TextView(this);
                    id.setText(data[0]+" ");
                    System.out.println("ID is...."+data[0]);
                    dob=new TextView(this);
                    dob.setText(data[3]+" ");
                    gender=new TextView(this);
                    gender.setText(data[2]+" ");
                    branch=new TextView(this);
                    branch.setText(data[4]+" ");
                    mobile=new TextView(this);
                    mobile.setText(data[5]+" ");
                    email=new TextView(this);
                    email.setText(data[6]+" ");
                    address=new TextView(this);
                    address.setText(data[7]+" ");

                    tr=new TableRow(this);
                    tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

                    ch[k]=new CheckBox(this);
                    System.out.println("sysout");

                    i=Integer.parseInt(data[0]);
                    ch[k].setId(i);

                    tr.addView(ch[k]);
                    a[k++]=i;

                    tr.addView(id);
                    tr.addView(name1);
                    tr.addView(dob);
                    tr.addView(gender);
                    tr.addView(branch);
                    tr.addView(mobile);
                    tr.addView(email);
                    tr.addView(address);
                tb.addView(tr,new TableLayout.LayoutParams(                         LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
                    j++;
                    System.out.println("count"+j);  
                }
            }
    }

    public void delete(View v){
        System.out.println("In delete");
        int bb=k,id ;
         for (int i=0; i <k; i++) 
        {
             final int j = a[i];
             System.out.println("in for loop"+j);

                ch[j].setOnCheckedChangeListener(new OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                            boolean isChecked) {
                        System.out.println("Checked ID :: " + ch[j].getId());
                    }
                });
            }
        System.out.println("00000000000000000..."+id);
        dh.deleteData(id);

    }
}
Artem Mostyaev
  • 3,874
  • 10
  • 53
  • 60
yuva ツ
  • 3,707
  • 9
  • 50
  • 78
  • You need to getId from buttonView, did you try that? – kosa Jul 05 '12 at 17:31
  • im not getting you... on click of delete button this function is executing.. – yuva ツ Jul 05 '12 at 17:35
  • It seems based on your code, this code is being executed on your check box "check/Uncheck", not on button click. – kosa Jul 05 '12 at 17:36
  • With updated code, inside delete, you are checking for "Checkbox" event which may not likely happen. What you may need to do would be from "View v" look for "Check box" component and it's state. – kosa Jul 05 '12 at 17:38
  • i have resolved this problem successfully and my and is at this link: http://stackoverflow.com/questions/11349461/getting-dynamically-added-checkbox-id-in-android – Bhunnu Baba Feb 10 '16 at 11:16

2 Answers2

3

strong textJust take a global variable

int chkId = 1001;

then at time of adding CheckBox dynamically, set its id as

ch.setId(++chkId);

then at time of deleting CheckBox, you can get id of Checked CheckBox simple by using

getId()

methhod

See Following Demo:

public class ChkBoxesActivity extends Activity {
    int chId = 1000;
    int chPos = -1;

    LinearLayout ll;
    String[] names = {"Tom", "Dick", "Keanu", "Harry", "Katrina", "Peter", "Julia", "Emma"};
    CheckBox[] ch;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ll = (LinearLayout) findViewById(R.id.ll);
        ch = new CheckBox[names.length];

        for(int i=0; i<names.length; i++) {
            ch[i] = new CheckBox(this);
            ch[i].setId(chId++);
            System.out.println("CHID :: "+chId);
            System.out.println("I :: "+i);
            ch[i].setText(names[i]);
            ll.addView(ch[i]);
        }

        for (int i = 0; i < names.length; i++) {
            final int j = i;
            ch[j].setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    System.out.println("Checked ID :: " + ch[j].getId());
                }
            });
        }
    }
}
Chintan Raghwani
  • 3,370
  • 4
  • 22
  • 33
  • did that also.. i have set id to ch in previous function at the time of displaying data.each record have one checkbox.if i check one chechbox between that record..i want the checkbox id.. – yuva ツ Jul 05 '12 at 17:43
  • for(String name:names){ if((!name.equals("-999"))){ sb.append(name+" "); } else { tr=new TableRow(this); tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); ch=new CheckBox(this); ch.setText(sb); tr.addView(ch); c=sb.charAt(0)+""; i=Integer.parseInt(c); ch.setId(i); a[k++]=i; sb=new StringBuilder(); tb.addView(tr,new TableLayout.LayoutParams( LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); } } – yuva ツ Jul 05 '12 at 17:53
  • Do you want to delete CheckBox at the time of Checking it? – Chintan Raghwani Jul 05 '12 at 18:25
  • I have updated my answer and getting ID for all checkboxes, just check it, may be helpful for you. – Chintan Raghwani Jul 06 '12 at 11:50
  • its giving null ptr exceptn. at- ch[j].setOnCheckedChangeListener(new OnCheckedChangeListener() { – yuva ツ Jul 07 '12 at 11:05
  • Just do one thing, Just COPY my whole activity in your project and run it. – Chintan Raghwani Jul 07 '12 at 11:07
  • for(String name:names){ if((!name.equals("-999"))){ data[cnt]=name; cnt++; }else { cnt=0; name1=new TextView(this); name1.setText(data[1]); id=new TextView(this); id.setText(data[0]); tr=new TableRow(this); tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); ch[k]=new CheckBox(this); i=Integer.parseInt(data[0]); ch[k].setId(i); tr.addView(ch[k]); a[k++]=i; tr.addView(id); tr.addView(name1); tb.addView(tr,new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); j++; } } – yuva ツ Jul 07 '12 at 11:44
  • in above code i'm adding checkbox..n also assignin id to cehckbox..now see in delete function - – yuva ツ Jul 07 '12 at 11:46
  • public void delete(View v){ System.out.println("In delete"); int bb=k,id = -1; // TODO Auto-generated method stub for(int bb2=0;bb – yuva ツ Jul 07 '12 at 11:49
  • What is variable K here? Post your whole Activity code here, if possible. – Chintan Raghwani Jul 07 '12 at 11:53
  • k is integer..total no. of records r stored in dat – yuva ツ Jul 07 '12 at 11:57
  • Datahelper is .java file.. where sqlite query is executed – yuva ツ Jul 07 '12 at 12:33
  • Yes, I know that But I need That BDHelper class and also Layout XML – Chintan Raghwani Jul 07 '12 at 12:59
  • that files are large i couldn't copy here – yuva ツ Jul 07 '12 at 13:28
2

Views added dynamically do not have an ID until you specifically set them. So you need to call view.setId() on the view when you're adding to your layout, else you won't be able to reference it with view.getId().

I experienced a similar issue a while back when trying to dynamically create a RelativeLayout and positions the inner views relative to each other; they wouldn't align how they should have because the ID's didn't exist until I explicitly set one for them.

Also, why are you doing this:

ch.getId();
ch.setId(1);

That makes absolutely no sense. Take it out.

And also, you're going to need a reference linking the CheckBox to the View you want to delete. In this case, I would make a new ArrayList of Objects that have both a CheckBox and a View inside of it, IE.

public Class MyRow{        
    CheckBox c;
    View v;

    public MyRow() {   }        
}

Then when adding your views dynamically, add them to your MyRow or whatever class, then add that class to an ArrayList, and boom, you now have references between them and can remove the correct ones.

Cruceo
  • 6,763
  • 2
  • 31
  • 52
  • that getId n setid i was trying somthing else..at time of copying code that was also included – yuva ツ Jul 05 '12 at 17:58
  • I would still take it out and make an ArrayList of an object that references both your View and the CheckBoc associated with it as they're being created. – Cruceo Jul 05 '12 at 18:01