在Android中动态添加复选框ID

时间:2012-07-05 17:29:18

标签: android

我在我的应用程序中动态添加了一个复选框,我想删除已检查的记录。但是,我没有得到复选框的ID。我怎么能这样做?

代码:

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);

    }
}

2 个答案:

答案 0 :(得分:3)

强文本只需全局变量

int chkId = 1001;

然后在添加CheckBox 动态时,将其ID设置为

ch.setId(++chkId);

然后在删除CheckBox时,您可以使用

简单地获取Checked CheckBox的ID
getId()

methhod

参见以下演示:

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());
                }
            });
        }
    }
}

答案 1 :(得分:2)

在您专门设置之前,动态添加的视图没有ID。因此,当您添加到布局时,需要在视图上调用view.setId(),否则您将无法使用view.getId()来引用它。

在尝试动态创建RelativeLayout并将内部视图相对于彼此定位时,我经历了类似的问题;他们不会对齐他们应该如何,因为在我明确为他们设置ID之前,ID不存在。

另外,你为什么要这样做:

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

这绝对没有意义。拿出来。

而且,您还需要一个将CheckBox链接到要删除的视图的引用。在这种情况下,我会创建一个新的对象ArrayList,其中包含一个CheckBox和一个View,IE。

public Class MyRow{        
    CheckBox c;
    View v;

    public MyRow() {   }        
}

然后在动态添加视图时,将它们添加到MyRow或任何类中,然后将该类添加到ArrayList,然后繁荣,现在它们之间有引用,可以删除正确的。