草庐IT

android - 在 Android 中获取动态添加的复选框 ID

coder 2023-12-28 原文

我正在我的应用程序中动态添加一个复选框,我想删除选中的记录。但是,我没有得到复选框的 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);

    }
}

最佳答案

strong text就取一个全局变量

int chkId = 1001;

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

ch.setId(++chkId);

然后在删除 CheckBox 时,您可以通过使用简单地获取 Checked 的 idCheckBox

getId()

方法

请参阅以下演示:

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 - 在 Android 中获取动态添加的复选框 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11349461/

有关android - 在 Android 中获取动态添加的复选框 ID的更多相关文章

  1. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  2. ruby - 将 Bootstrap Less 添加到 Sinatra - 2

    我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它

  3. ruby - 续集在添加关联时访问many_to_many连接表 - 2

    我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以

  4. ruby - 简单获取法拉第超时 - 2

    有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url

  5. ruby - 从 Ruby 中的主机名获取 IP 地址 - 2

    我有一个存储主机名的Ruby数组server_names。如果我打印出来,它看起来像这样:["hostname.abc.com","hostname2.abc.com","hostname3.abc.com"]相当标准。我想要做的是获取这些服务器的IP(可能将它们存储在另一个变量中)。看起来IPSocket类可以做到这一点,但我不确定如何使用IPSocket类遍历它。如果它只是尝试像这样打印出IP:server_names.eachdo|name|IPSocket::getaddress(name)pnameend它提示我没有提供服务器名称。这是语法问题还是我没有正确使用类?输出:ge

  6. ruby - 获取模块中定义的所有常量的值 - 2

    我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c

  7. ruby-on-rails - 获取 inf-ruby 以使用 ruby​​ 版本管理器 (rvm) - 2

    我安装了ruby​​版本管理器,并将RVM安装的ruby​​实现设置为默认值,这样'哪个ruby'显示'~/.rvm/ruby-1.8.6-p383/bin/ruby'但是当我在emacs中打开inf-ruby缓冲区时,它使用安装在/usr/bin中的ruby​​。有没有办法让emacs像shell一样尊重ruby​​的路径?谢谢! 最佳答案 我创建了一个emacs扩展来将rvm集成到emacs中。如果您有兴趣,可以在这里获取:http://github.com/senny/rvm.el

  8. Ruby 从大范围中获取第 n 个项目 - 2

    假设我有这个范围:("aaaaa".."zzzzz")如何在不事先/每次生成整个项目的情况下从范围中获取第N个项目? 最佳答案 一种快速简便的方法:("aaaaa".."zzzzz").first(42).last#==>"aaabp"如果出于某种原因你不得不一遍又一遍地这样做,或者如果你需要避免为前N个元素构建中间数组,你可以这样写:moduleEnumerabledefskip(n)returnto_enum:skip,nunlessblock_given?each_with_indexdo|item,index|yieldit

  9. ruby - Net::HTTP 获取源代码和状态 - 2

    我目前正在使用以下方法获取页面的源代码:Net::HTTP.get(URI.parse(page.url))我还想获取HTTP状态,而无需发出第二个请求。有没有办法用另一种方法做到这一点?我一直在查看文档,但似乎找不到我要找的东西。 最佳答案 在我看来,除非您需要一些真正的低级访问或控制,否则最好使用Ruby的内置Open::URI模块:require'open-uri'io=open('http://www.example.org/')#=>#body=io.read[0,50]#=>"["200","OK"]io.base_ur

  10. ruby - 可以通过多少种方法将方法添加到 ruby​​ 对象? - 2

    当谈到运行时自省(introspection)和动态代码生成时,我认为ruby​​没有任何竞争对手,可能除了一些lisp方言。前几天,我正在做一些代码练习来探索ruby​​的动态功能,我开始想知道如何向现有对象添加方法。以下是我能想到的3种方法:obj=Object.new#addamethoddirectlydefobj.new_method...end#addamethodindirectlywiththesingletonclassclass这只是冰山一角,因为我还没有探索instance_eval、module_eval和define_method的各种组合。是否有在线/离线资

随机推荐