Android:动态添加OnClickListeners到按钮

发布于 2025-01-08 07:50:22 字数 1213 浏览 1 评论 0原文

在我的活动中,我查看 SQLite 数据库(_id、名称、编号)并添加与数据库中的行一样多的按钮。对于每个按钮,我动态创建 onClickListener ,它将启动另一个活动,我需要将数据从相应的数据库行传递到其中。我的代码如下:

db.open();
String name;
String number;
Cursor c = db.getAllContacts();
c.moveToFirst();
int count = db.getCount();
for (int i = 0; i < count; i++) {
    ImageButton button = new ImageButton(this);
    name = c.getString(1);
    number = c.getString(2);
    button.setOnClickListener(new onClickListener() {
       public void onClick(View v) {
          Intent intent = new Intent(This_class.this, Other_class.class);
          intent.putExtra("name_bundle", name);
          intent.putExtra("number_bundle", number);
          startActivity(intent);
       }
    });
c.moveToNext();
}
db.close();

在由 ImageButton 启动的新 Activity 中,我使用传递的数据,如下所示:

Bundle extras = getIntent().getExtras();
name = extras.getString("name_bundle");
number = extras.getString("number_bundle");

所有按钮均已正确创建,但我的问题是所有按钮都传递相同的信息。我的意图是,当单击第三个按钮时,数据库中第三行的数据(代码中的第 1 列和第 2 列)将被传递(在新启动的 Activity 中,我将它们显示在 TextView 中)。但发生的情况是所有按钮都传递相同的数据!准确地说,数据来自数据库的最后一行。当我尝试向数据库添加另一行时,创建了一个额外的按钮,并且所有按钮再次显示来自新创建的数据库行(因此是最后一行)的相同数据。我怀疑我的代码中存在愚蠢的错误或完全错误的方法。有什么想法吗?谢谢!!

In my Activity, I look into a SQLite database (_id, name, number) and add as many buttons as I have rows in the database. For each button I dynamicly create onClickListener that will start another activity into which I need to pass data from the coresponding database row. My code follows:

db.open();
String name;
String number;
Cursor c = db.getAllContacts();
c.moveToFirst();
int count = db.getCount();
for (int i = 0; i < count; i++) {
    ImageButton button = new ImageButton(this);
    name = c.getString(1);
    number = c.getString(2);
    button.setOnClickListener(new onClickListener() {
       public void onClick(View v) {
          Intent intent = new Intent(This_class.this, Other_class.class);
          intent.putExtra("name_bundle", name);
          intent.putExtra("number_bundle", number);
          startActivity(intent);
       }
    });
c.moveToNext();
}
db.close();

In the new Activity started by the ImageButton I work with the passed on data like this:

Bundle extras = getIntent().getExtras();
name = extras.getString("name_bundle");
number = extras.getString("number_bundle");

All the buttons are created correctly, but my problem is that all of them pass on the same information. My intention was that when third button is clicked, data (columns 1 and 2 in the code) from the third row in the database are passed (In the newly started Activity I display them in a TextView). But what happens is that all the buttons pass on the same data! Data from the last row of the database to be precise. When I tried to add yet another row to the database, one extra button was created and all of them again displayed the same data from the newly created (therefore the last) row of the database. I suspect either a dum mistake in my code or completly bad approach. Any ideas? Thanks!!

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

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

发布评论

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

评论(1

拍不死你 2025-01-15 07:50:22

在新的 Activity 中使用 removeExtra()

Bundle extras = getIntent().getExtras();
name = extras.getString("name_bundle");
number = extras.getString("number_bundle");
getIntent().removeExtra("name_bundle");
getIntent().removeExtra("number_bundle");

当您启动 Activity 时,旧的额外内容不会被新的替换。

In your new Activity use removeExtra()

Bundle extras = getIntent().getExtras();
name = extras.getString("name_bundle");
number = extras.getString("number_bundle");
getIntent().removeExtra("name_bundle");
getIntent().removeExtra("number_bundle");

When you start activity old extras are not replaced by new.

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