Best way to code: Dynamically allocated rows in TableLayout with onClickListener() required for each row?

时间:2017-06-12 17:02:56

标签: android android-tablelayout

I am trying to add rows in a tableLayout dynamically with click events trigerring some action for each row. The data is dynamic because it is coming from the server.

I have come upon two different ways to use the dynamic approach and registering the click events.

For dynamic approach the easiest way would be to put the rowAdditon() as a dedicate function like

tl.addView(row1);
tl.addView(row2);

and to integrate click events i would require each TableRow object for onCLickListener().

But i am not able to find the best way to integrate them together.

First thing that comes to my mind is a ArrayList. Is there any other easy way to acheive this.

i tried to implement this like the following

    ArrayList a1 = new ArrayList();
            DataBaseReceiverForConnectToTeachers received=new DataBaseReceiverForConnectToTeachers(this);
            int count=received.getNoOfData();
            for(int i=0;i<count;i++){
                a1.add("t"+i);
            }

            for(int i=0;i<count;i++){
              TableRow  a1.get(i)=new TableRow(this);
//getting error in this line
//cannot resolve method get(int)
//a1 is already define in the scope
            }

The solution was creating a for loop that runs for the size of the items in database.

1 个答案:

答案 0 :(得分:0)

为什么要放入表格视图,在线性布局中添加相同的内容,方法是在xml中创建它,每当你想添加按钮调用一个方法并保持这个按钮的id是静态的,这样你就可以点击每个按钮方法单独

如果我的答案完成您的查询,请告诉我,否则我可以为您提供此代码