Android 动态setOnCLickListener
我使用了一个表布局,其中的行来自SQLite数据库,所以我动态地添加行,并将视图添加到行中。现在,我在处理行版本的每一行以及将其添加到SQLite数据库的其他行中添加了和Android 动态setOnCLickListener,android,listener,imagebutton,Android,Listener,Imagebutton,我使用了一个表布局,其中的行来自SQLite数据库,所以我动态地添加行,并将视图添加到行中。现在,我在处理行版本的每一行以及将其添加到SQLite数据库的其他行中添加了和imageButton。如果我选择了正确的路径,那么是否有办法向每个生成的imageButton添加SetOnClickListener 我使用此代码为动态生成的按钮添加单击事件 for (int position=0; position < parseInt; position++) {
imageButton
。如果我选择了正确的路径,那么是否有办法向每个生成的imageButton
添加SetOnClickListener
我使用此代码为动态生成的按钮添加单击事件
for (int position=0; position < parseInt; position++)
{
TableRow tableRow= new TableRow(this);
tableRow.setBackgroundColor(006400);
// ArrayList<Object> row = data.get(position);
TextView idText = new TextView(this);
idText.setText(Integer.toString(position + 1));
idText.setGravity(Gravity.CENTER);
idText.setTextColor(Color.BLACK);
idText.setWidth(10);
idText.setHeight(30);
idText.setBackgroundResource(R.drawable.textbg);
// idText.setPadding(0, 0, 1,0);
tableRow.addView(idText);
//THE CLICK EVENT OF BUTTON
Button textOne = new Button(this);
textOne.setText("CLUB");
textOne.setBackgroundResource(R.drawable.textbg);
textOne.setGravity(Gravity.CENTER);
textOne.setTextColor(Color.BLACK);//left top right bottom
// textOne.setPadding(2, 1, 1,0);
// textOne.setB;
textOne.setWidth(10);
textOne.setHeight(30);
textOne.setId(1+position);
tableRow.addView(textOne);
// textOne.setOnClickListener(this);
textOne.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// do something when the button is clicked
final Button button = (Button) arg0;
System.out.println("button is clicked");
});
for(int-position=0;position
您的做法完全错了(在前面的问题中,我看到您使用了表布局并从数据库中填充行)…对于像这样的东西,使用ListView和adapter…Onitemclick方法已经存在…使用google和search ListView+sqlite db sampleIt是一个想法如果可以更改列表视图以拥有更多列的表,我有一个自定义的ListView,其中的db用于同一应用程序上的其他数据phpMyadmin窗口中的行谢谢你的想法,但如果我现在不知道我必须创建多少个按钮,因为这取决于DB行数,我没有动态侦听器,这就是我一直在寻找的。顺便说一句,我认为这样做需要做很多工作…我已经放置了一个带有游标适配器的列表视图,以及一个onitemclick来管理一点选择菜单。