0

i created a table row dynamically, That contains three textviews.now i want to get the value of three textview's value on click on tablerow(tr).means i want to get the companyTV,valueTV & YearTV's value. Thanks.

    for (int i = 0; i < match.length; i++) {
        /** Create a TableRow dynamically **/
        tr.setBackground(getResources().getDrawable(R.drawable.tv_bg));
        tr = new TableRow(this);
        tr.setLayoutParams(new TableRow.LayoutParams(
                TableRow.LayoutParams.FILL_PARENT,
                TableRow.LayoutParams.FILL_PARENT));


        /** Creating a TextView to add to the row **/
        companyTV = new TextView(this);
        companyTV.setText(match[i]);
        companyTV.isClickable();
        companyTV.setTextColor(Color.RED);
        companyTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
        companyTV.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        companyTV.setPadding(5, 5, 5, 5);
        tr.addView(companyTV);  // Adding textView to tablerow.

        /** Creating another textview **/
        valueTV = new TextView(this);
        valueTV.setText(kanr[i]);
        valueTV.isClickable();
        valueTV.setTextColor(Color.RED);
        valueTV.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        valueTV.setPadding(5, 5, 5, 5);
        valueTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
        tr.addView(valueTV); // Adding textView to tablerow.


        YearTV = new TextView(this);
        YearTV.setText(ort[i]);

        YearTV.setTextColor(Color.RED);
        YearTV.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        YearTV.setPadding(5, 5, 5, 5);
        YearTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
        tr.addView(YearTV); // Adding textView to tablerow.


        // Add the TableRow to the TableLayout
        tl.addView(tr, new TableLayout.LayoutParams(
                TableRow.LayoutParams.FILL_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT));
    }

1 Answers1

3

First you have one error in your code when you are setting background of table row before you have created it. You should first create new table row, and then set its background:

for (int i = 0; i < match.length; i++) {
    /** Create a TableRow dynamically **/
    tr = new TableRow(this);
    tr.setBackground(getResources().getDrawable(R.drawable.tv_bg));

You can access table row children views by index in your OnClickListener

        tr.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                TableRow tr = (TableRow)v;
                TextView companyTV, valueTV, yearTV;
                String company, value, year;
                companyTV = (TextView) tr.getChildAt(0);
                valueTV = (TextView) tr.getChildAt(1);
                yearTV = (TextView) tr.getChildAt(2);
                company = companyTV.getText().toString();
                value = valueTV.getText().toString();
                year = yearTV.getText().toString();
            }
        });

If you want to track which table row is currently selected, you can create global variable for your activity

TableRow selectedTR;

and then set that inside OnClickListener

selectedTR = (TableRow)v;

When you use that selectedTR variable make sure that you check it for null value in case there is no selection made yet.

Dalija Prasnikar
  • 27,212
  • 44
  • 82
  • 159