Android以编程方式创建按钮问题
我正在尝试在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题在于这行代码:
您在循环中一遍又一遍地编辑同一个按钮,而不是真正创建一个新按钮。要创建一个新的,您需要执行以下操作:
每次迭代 for 循环时,都会创建一个全新的。
I think the problem lies with this line of code:
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:
This will create a brand new one every time you iterate through your for loop.