
我在我的应用程序中动态添加了一个复选框,我想删除已检查的记录.但是,我没有得到复选框的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); }}解决方法:
强文本只需要一个全局变量
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()); } }); } }} 总结 以上是内存溢出为你收集整理的在Android中动态添加复选框ID全部内容,希望文章能够帮你解决在Android中动态添加复选框ID所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)