Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从另一个方法更改表行_Android - Fatal编程技术网

Android 如何从另一个方法更改表行

Android 如何从另一个方法更改表行,android,Android,我在第一个方法中有两个文本视图的表行 final TableLayout tl = (TableLayout) findViewById(R.id.mainLayout); final TableRow tr = new TableRow(this); tl.setStretchAllColumns(true); tl.setShrinkAllColumns(true); tr.setGravity(Gravity.CENTER); tr.setLayoutParams(new TableRo

我在第一个方法中有两个文本视图的表行

final TableLayout tl = (TableLayout) findViewById(R.id.mainLayout);
final TableRow tr = new TableRow(this);
tl.setStretchAllColumns(true);
tl.setShrinkAllColumns(true);
tr.setGravity(Gravity.CENTER);
 tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT));

    TextView textview = new TextView(this);
    textview.setText(nameOne);
    textview.setTextColor(Color.BLACK);
    textview.setTextSize(20);
    textview.setGravity(Gravity.START);
    tr.addView(textview);


    TextView textview2 = new TextView(this);
    textview2.setText(nameTwo);
    textview2.setTextColor(Color.BLACK);
    textview2.setTextSize(20);
    textview2.setGravity(Gravity.END);
    tr.addView(textview2);

    tl.addView(tr, new TableLayout.LayoutParams(
            TableRow.LayoutParams.FILL_PARENT,
            TableRow.LayoutParams.WRAP_CONTENT));
然后我尝试获取/更改行中的第二个文本视图,但它不起作用。如何获取更改表行的权限

public void anotherMethod(){
    TableLayout tl = (TableLayout) findViewById(R.id.mainLayout);
    TableRow t = (TableRow) v;

    TextView secondTextView = (TextView) t.getChildAt(1);
    String secondText = secondTextView.getText().toString();
    Log.d(LOG_TAG, "second - " + secondText);
}

*对不起,我的英语不好,我希望你能理解我的问题

我希望下面的代码能帮助你

public void anotherMethod(){
    TableLayout tl = (TableLayout) findViewById(R.id.table_layout);
    int tableRowIndex = 0;
    TableRow tr = (TableRow) tl.getChildAt(tableRowIndex);
    int textViewIndex = 1;
    TextView textView = (TextView) tr.getChildAt(textViewIndex);
    textView.setText("New Text");
}

我会像这样设置TableRow的id:

public class yourClass { 

     int tableRowID = 123456;
     final TableRow tr = new TableRow(this);


     private void firstMethod() { 

          tr.setId(tableRowID);
          ......

     }

     private void secondMethod () { 
         TableRow tr = findViewByID(tableRowID);
         TextView secondTextView = (TextView) tr.getChildAt(1);
         ....

     }
}
您还可以设置tableRow的标记:

tr.setTag("TableRow");