简体   繁体   中英

How to get to know cell position in dynamic Table Layout in android

Here I am using dynamic table layout but i am not able to get cell position. My question is "How to get to row and column position" in dynamic table Layout in android. Please help me.

Thanks.

for (int i = 1; i <= rows; i++) {

        TableRow row = new TableRow(this);
        row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT));

        // inner for loop
        for (int j = 1; j <= cols; j++) {

             TextView tv = new TextView(this);
            tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                     LayoutParams.WRAP_CONTENT));
            tv.setBackgroundResource(R.drawable.cell_shape);
            tv.setPadding(5, 5, 5, 5);
            tv.setId(j);
            tv.setText("R " + i + ", C" + j);

            //mOrangeToolTipView = mToolTipFrameLayout.showToolTipForView(toolTip, tv);
            tv.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(final View v) {

                    Log.d("Row and Column position ", v.getTouchables()+"");

                }
            });

            row.addView(tv);

        }

        table_layout.addView(row);

    }

Probably its too late for you, but... To get to row and column position from where?? From the inside of your inner for-loop the row position is i and the column position is j . Outside try something like

((TextView)((TableRow)tableLayout.getChildAt(row)).getChildAt(column)).setText("MyText");

If you want to set different OnClickListeners for different cells, trying to access i or j from the inner class (new View.OnClickListener(){...}), I have bad news for you, its not possible. But you create every cell in a loop, so you can do it like

...
if (i == 1 && j == 1){
                    tv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            // do what cell (1,1) have to do
                        }
                    });
                } else {
                    tv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            // do what other cells have to do
                        }
                    });
                }
...

I hope it will help you or some others ;)

Got stuck in same problem ...found a solution which worked for me but .....it is not applicable in all cases .

Changed this line : tv.setId(j); To : tv.setId((i*100)+j);

Logic of how it works:

To get row index : row_index = id/100; To get column index : column_index = id%100; Note : it works give i and j are 2 digit number.(Guess as it is indices to locate a textview in a tablelayout,it would be within 99 ,at max)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM