Android的动态复选框问题 [英] Android Dynamic Checkbox issue

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

问题描述

我在试图动态复选框添加到我的活动过程。但是作为一个初学者,我不能避开的是能够添加复选框,并删除它们的基础知识。这里是我的code ....

I am in the process of trying to add dynamic checkbox to my activity. However being a beginner i cant get round the basics of being able to add checkboxes and remove them. Here is my code....

私人无效createCheckbox(){

private void createCheckbox() {

 for(int i=0; i<5; i++){
    cb  = new CheckBox(this);
    ll.addView(cb); 
    cb.setText("Test");
 }

 ll.addView(submit); 

  submit.setOnClickListener(new View.OnClickListener() {
     public void onClick(View v) {
        for(int i = 0; i < 5; i++) { 
            ll.removeView(cb); 
        }  
        ll.removeView(submit);
        Questions();
 }});       

}
L1是linerlayout对象。我们的想法是,当code运行时,5复选框出现,然后一旦提交按钮用户点击他们得到清除。目前箱被人发现,但是当提交按钮pssed $ P $只有五分之一的被删除。我不明白我在做什么错了?

} ll is a linerlayout object. The idea is when the code runs, 5 checkboxes appear and then once the user clicks on the submit button they get removed. Currently the boxes are being seen, but when the submit button is pressed only one of the five is being removed. I don't understand what i am doing wrong?

的想法是,复选框将取决于与数据库中的值来创建和这个值可能会改变这就是为什么复选框不pdefined因为可能有4,5 $ P $,或15.我不知道如何使每个复选框有自己的标识,因为本次发行后我将需要单独区分它们,因为我需要从数据库中添加文本给他们,然后一旦用户检查几个按钮我需要把这个保存到一个单独的表。很迷茫!帮助!

The idea is that the checkboxes will be creating depending on a value with the database and this value could change thats why the checkboxes are not predefined as there could be 4, 5, or 15. I dont know how to make each checkbox have its own identifier, because after this issue i will need to identify them individually because i will need to add text to them from the database and then once the user has check a few buttons i will need to save this into a seperate table. Very confused !!! help!

推荐答案

您需要存储这些复选框引用的地方,但不能重复使用相同的变量。

You need to store the references to those checkboxes somewhere, but not reusing the same variable.

CheckBox[] cbs = new CheckBox[5];
for(int i=0; i<5; i++){
    cbs[i]  = new CheckBox(this);
    ll.addView(cb); 
    cbs.setText("Test");
 }

 ll.addView(submit); 

  submit.setOnClickListener(new View.OnClickListener() {
     public void onClick(View v) {
        for(int i = 0; i < 5; i++) { 
            ll.removeView(cbs[i]); 
        }  
        ll.removeView(submit);
        Questions();
 }});

这篇关于Android的动态复选框问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆