简体   繁体   中英

Android TextView/EditText generated dynamically

In my app i generate TextViews/Edits dynamically during the execution. I also have a TableLayout so each set of TextEdit/TextViews will be on a new row. I want to use the row number (1...N) as a sort of ID to identify each set of textEdit/TextViews (Example, row 1 will have TEdit1, Tview1, row 2 will have TEdit2, TView2..etc).

I have functions that are called on the onchange of each tEdit. For instance, TEdit 1 changes the value of TView1 when the onChange method is called. The function is set up so that i can pass the TView id and it changes it properly. My problem is, i dont know how to use a variable as the tView id. The function takes for parameter, among others, the rowId (1, 2, 3..etc) so i want to know if there's a way to set up a variable like this:

String VariableName = "tEdit" + i; (i is the rowId that its passed through as a parameter)

And then use it for something like this:

MyActivity.VariableName.setText("Change proper text");

Obviously, that one doesnt work. I have done some research and i've seen something like getIdentifier, but i've had no luck implementing that one. Any help would be greatly appreciated.

If I have well understood your question maybe you can find useful "Tag" property. I mean that when you dynamically add your new row to the table you can simply add a tag identifier to this new row with his index position inside the table

TableRow tr = new TableRow(this);
tr.setTag(indexPosition);

So, when you need to know the position of the current row you'll simply need to call

int position = tr.getTag();

I hope that this was your question!

您可以使用Textview的数组,而不是尝试根据行号等定义自定义名称。

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