java 如何在动态生成的表格中获取textview的文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5537239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get text of the textview in the dynamically generated table?
提问by Sourabh
i have written something like this....
我写过这样的东西......
TableRow tr1;
TableRow tr2;
TextView txt9;
...
TableLayout tl = (TableLayout)findViewById(R.id.myTableLayout);
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, LayoutParams.FILL_PARENT);
for (int i = 0; i < strarr.length-1; i++)
{
strinarr = fun1.split(strarr[i].trim(),"^");
tr1 = (TableRow) new TableRow(this);
txt1=new TextView(this);
txt9.setText(strinarr[0]);
txt9.setBackgroundColor(intblue);
txt9.setTextColor(intwhite);
txt9.setClickable(true);
txt9.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Log.i("pagename",strpagename);
String currenttext = txt9.getText().toString());
}
}
});
tr1.addView(txt9);
tl.addView(tr1,new TableLayout.LayoutParams(layoutParams));
}
i am able to get text on click but the stupid thing is that i am getting text of last textview in the table on all the textview click event... if somebody could tell me how to catch textview's text on focus or touch it would be really help full...
我能够在点击时获取文本,但愚蠢的是我在所有 textview 点击事件的表格中获取最后一个 textview 的文本...如果有人能告诉我如何在焦点或触摸上捕捉 textview 的文本真的很有帮助...
回答by pankajagarwal
change String currenttext = txt9.getText().toString());
to String currenttext = ((TextView)v).getText().toString());
更改String currenttext = txt9.getText().toString());
为String currenttext = ((TextView)v).getText().toString());
回答by DKIT
You should probably maintain a list of ID's in the table and a list of corresponding texts. When the user clicks on the widget with the given ID, simply look up the text in the textlist and set it.
您可能应该维护表中的 ID 列表和相应文本的列表。当用户单击具有给定 ID 的小部件时,只需在文本列表中查找文本并进行设置即可。
回答by Sam
ArrayList qty = new ArrayList();
for(int i = 1; i <= 10; i++)
{
qty.add(i);
}
for(int i = 0; i < 5; i++)
{
TableRow tr = new TableRow(this);
Spinner tvQtySpinner = new Spinner(this);
tvQtySpinner.setOnItemSelectedListener(this);
ArrayAdapter<String> aa =
new ArrayAdapter<String> this, android.R.layout.simple_spinner_item, qty);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
tvQtySpinner.setAdapter(aa);
tr.addView(tvQtySpinner);
TextView tvRate = new TextView(this);
tvRate.setText(value.getPrice().toString());
tvRate.setTextColor(Color.YELLOW);
tr.addView(tvRate);
}
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id)
{
Integer value = (Integer) parent.getItemAtPosition(pos);
Float result = value*50;
tvRate.setText(String.valueOf(result));
}
Here I am having 5 rows, each row having 5 dynamic spinners and text views. I am getting the selected value from spinner and doing calculation and am trying to set that calculated result in particular rows TextView. But when I set the calculated value in TextView it will set on last rows TextView.
这里我有 5 行,每行有 5 个动态微调器和文本视图。我正在从微调器获取选定的值并进行计算,并尝试在特定行 TextView 中设置该计算结果。但是当我在 TextView 中设置计算值时,它将设置在最后一行 TextView。