Android以编程方式创建按钮问题

发布于 2024-12-12 04:05:54 字数 1552 浏览 0 评论 0原文

我正在尝试在我的 android 应用程序上以编程方式创建按钮,具体取决于我的 sqlite 数据库中有多少项目。按钮在那里,但我的问题是在每个按钮上设置 onClick 因为我想在用户单击按钮时显示不同的内容。我现在正在使用这段代码:

   for(cursorCol.move(0); cursorCol.moveToNext(); cursorCol.isAfterLast()){
          Id = Integer.parseInt(cursorCol.getString(cursorCol.getColumnIndex("id")));
          Log.i("Id","Id : "+Id);
                titleButton = cursorCol.getString(cursorCol.getColumnIndex("title"));
             Log.i("titleButton","titleButton : " + titleButton);
             elemOrder1 = Integer.parseInt(cursorCol.getString(cursorCol.getColumnIndex("elemOrder")));
               Log.i("elemOrder1 ","elemOrder1 : " + elemOrder1 );    

               btn = new Button(this); 
                  btn.setText("  " + titleButton + "  "); 
                  btn.setId(Id);
                  btn.setTextColor(Color.parseColor("#000000"));
                  btn.setTextSize(12);
                  btn.setPadding(10, 10, 10, 10);
                  btn.setBackgroundResource(R.drawable.gray_button);
                  btnlayout.addView(btn,params); 

                  btn.setOnClickListener(new View.OnClickListener() {

          @Override
          public void onClick(View v) {
           infoCard.removeAllViews();

           for(int i=0;i<=cursorCol.getCount();i++){

            Log.i("","titleButton : "+titleButton);

               }
          }
}

但问题是,当我单击按钮时,它只显示最后一个 titleButton。实际上我不需要显示 titleButton,我只是出于测试目的这样做。有什么想法如何为每个按钮创建不同的 onClick 方法吗?

i'm trying to create buttons programmatically on my android application depending on how many items I have on my sqlite database. The buttons are there, but my problem is to set onClick on every button because I want to show different content when user's click the buttons. I'm using this code for now :

   for(cursorCol.move(0); cursorCol.moveToNext(); cursorCol.isAfterLast()){
          Id = Integer.parseInt(cursorCol.getString(cursorCol.getColumnIndex("id")));
          Log.i("Id","Id : "+Id);
                titleButton = cursorCol.getString(cursorCol.getColumnIndex("title"));
             Log.i("titleButton","titleButton : " + titleButton);
             elemOrder1 = Integer.parseInt(cursorCol.getString(cursorCol.getColumnIndex("elemOrder")));
               Log.i("elemOrder1 ","elemOrder1 : " + elemOrder1 );    

               btn = new Button(this); 
                  btn.setText("  " + titleButton + "  "); 
                  btn.setId(Id);
                  btn.setTextColor(Color.parseColor("#000000"));
                  btn.setTextSize(12);
                  btn.setPadding(10, 10, 10, 10);
                  btn.setBackgroundResource(R.drawable.gray_button);
                  btnlayout.addView(btn,params); 

                  btn.setOnClickListener(new View.OnClickListener() {

          @Override
          public void onClick(View v) {
           infoCard.removeAllViews();

           for(int i=0;i<=cursorCol.getCount();i++){

            Log.i("","titleButton : "+titleButton);

               }
          }
}

But the problem is that when I click the button it's showing only the last titleButton. Actually I don't need to show titleButton, I just did it for testing purposes. Any ideas how can I create different onClick methods for every single button?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哆兒滾 2024-12-19 04:05:54

我认为问题在于这行代码:

btn = new Button(this);

您在循环中一遍又一遍地编辑同一个按钮,而不是真正创建一个新按钮。要创建一个新的,您需要执行以下操作:

Button new_btn = new Button(this);

每次迭代 for 循环时,都会创建一个全新的。

I think the problem lies with this line of code:

btn = new Button(this);

You are editing the same button over and over again in your loop and not acutally creating a new one. To create a new one you will need to do this:

Button new_btn = new Button(this);

This will create a brand new one every time you iterate through your for loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文