动态setOnCLickListener [英] dynamic setOnCLickListener

查看:114
本文介绍了动态setOnCLickListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是行的表布局来自SQLite数据库,所以我添加了行动态地添加意见一行。现在,我可以处理该行版以及其他它添加到SQLite数据库的每一行中添加的ImageButton 。我想,如果我去了正确的道路,所以如果有添加 SetOnClickListener 来生成的每个的ImageButton ?

I'm using a table layout with rows that comes from an SQLite database, so I add the rows dynamically adding views to a row. Now, I added and imageButton in each row that handle the row edition and other that add it to the SQLite database. I would like if I go for the right path, so if there is a way to add a SetOnClickListener to each generated imageButton?

推荐答案

我用这个code添加点击事件动态生成按钮

I used this code for adding click events for dynamically generated button

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");



                });

这篇关于动态setOnCLickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆