正从含有多种textviews动态创建的表行数据 [英] getting data from dynamically created table row containing many textviews
问题描述
我创建了一个表行动态,包含三个textviews.now我想拿到三TextView的价值的价值上点击tablerow的(TR).means我想要得到的companyTV,valueTV&安培; YearTV的价值。
谢谢你。
的for(int i = 0; I< match.length;我++){
/ **创建一个动态的TableRow ** /
tr.setBackground(getResources()getDrawable(R.drawable.tv_bg));
TR =新的TableRow(本);
tr.setLayoutParams(新TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.FILL_PARENT));
/ **创建一个TextView添加到行** /
companyTV =新的TextView(本);
companyTV.setText(匹配[I]);
companyTV.isClickable();
companyTV.setTextColor(Color.RED);
companyTV.setTypeface(Typeface.DEFAULT,Typeface.BOLD);
companyTV.setLayoutParams(新TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT));
companyTV.setPadding(5,5,5,5);
tr.addView(companyTV); //添加的TextView为tablerow。 / **创建另一个TextView的** /
valueTV =新的TextView(本);
(kanR的[I])valueTV.setText;
valueTV.isClickable();
valueTV.setTextColor(Color.RED);
valueTV.setLayoutParams(新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); //添加的TextView为tablerow。
YearTV =新的TextView(本);
YearTV.setText(ORT [I]); YearTV.setTextColor(Color.RED);
YearTV.setLayoutParams(新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); //添加的TextView为tablerow。
//将的TableRow添加到TableLayout
tl.addView(TR,新TableLayout.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
}
首先,你必须在你的code有一个错误在设置表行的背景你创建它。你应该先创建新表行,然后设置它的背景:
的for(int i = 0; I< match.length;我++){
/ **创建一个动态的TableRow ** /
TR =新的TableRow(本);
tr.setBackground(getResources()getDrawable(R.drawable.tv_bg));
您可以通过索引访问表行的孩子的意见在你的 OnClickListener
tr.setOnClickListener(新View.OnClickListener()
{
@覆盖
公共无效的onClick(视图v)
{
TR的TableRow =(的TableRow)V;
TextView的companyTV,valueTV,yearTV;
字串公司,价值,一年;
companyTV =(TextView的)tr.getChildAt(0);
valueTV =(TextView的)tr.getChildAt(1);
yearTV =(TextView的)tr.getChildAt(2);
。公司= companyTV.getText()的toString();
。值= valueTV.getText()的toString();
。一年= yearTV.getText()的toString();
}
});
如果你想跟踪哪些表行当前选择,您可以为您的活动创造的全局变量
的TableRow selectedTR;
然后设置里面 OnClickListener
selectedTR =(的TableRow)V;
当您使用 selectedTR
变量确保你的情况下,检查它的空值不存在尚未作出选择。
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));
}
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.
这篇关于正从含有多种textviews动态创建的表行数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!