如何从动态创建的Android TableRow中获取值?
作者:互联网
我使用下面的代码动态创建一个包含内容的TableRow.它工作正常,但我希望得到TableRow中的值.以下是示例代码(我是从Google获得的),它在TableRow中有两个文本值.当我在任何位置单击TableRow时,它会在TableRow中给出相应的值(我希望类似于ListView).
All_CustomsActivity.java
public class All_CustomsActivity extends Activity {
String companies[] = { "Google", "Windows", "iPhone", "Nokia", "Samsung",
"Google", "Windows", "iPhone", "Nokia", "Samsung", "Google",
"Windows", "iPhone", "Nokia", "Samsung" };
String os[] = { "Android", "Mango", "iOS", "Symbian", "Bada", "Android",
"Mango", "iOS", "Symbian", "Bada", "Android", "Mango", "iOS",
"Symbian", "Bada" };
TableLayout tl;
TableRow tr;
TableRow mTable = null;
TextView companyTV, valueTV;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tl = (TableLayout) findViewById(R.id.maintable);
// addHeaders();
addData();
}
/** This function add the headers to the table **/
public void addHeaders() {
/** Create a TableRow dynamically **/
tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
/** Creating a TextView to add to the row **/
TextView companyTV = new TextView(this);
companyTV.setText("Companies");
companyTV.setTextColor(Color.GRAY);
companyTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
companyTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
companyTV.setPadding(5, 5, 5, 0);
tr.addView(companyTV); // Adding textView to tablerow.
/** Creating another textview **/
TextView valueTV = new TextView(this);
valueTV.setText("Operating Systems");
valueTV.setTextColor(Color.GRAY);
valueTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
valueTV.setPadding(5, 5, 5, 0);
valueTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(valueTV); // Adding textView to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// we are adding two textviews for the divider because we have two
// columns
tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
/** Creating another textview **/
TextView divider = new TextView(this);
divider.setText("-----------------");
divider.setTextColor(Color.GREEN);
divider.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
divider.setPadding(5, 0, 0, 0);
divider.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(divider); // Adding textView to tablerow.
TextView divider2 = new TextView(this);
divider2.setText("-------------------------");
divider2.setTextColor(Color.GREEN);
divider2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
divider2.setPadding(5, 0, 0, 0);
divider2.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(divider2); // Adding textView to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
/** This function add the data to the table **/
public void addData() {
for (int i = 0; i < companies.length; i++) {
/** Create a TableRow dynamically **/
tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// ImageView im = new ImageView(this);
// im.setBackgroundResource(R.drawable.sample_image);
// im.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
// LayoutParams.WRAP_CONTENT));
// tr.addView(im);
/** Creating a TextView to add to the row **/
companyTV = new TextView(this);
companyTV.setText(companies[i]);
companyTV.setTextColor(Color.RED);
companyTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
companyTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, 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(os[i]);
valueTV.setTextColor(Color.GREEN);
valueTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
valueTV.setPadding(5, 5, 5, 5);
valueTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(valueTV); // Adding textView to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
// tr.setOnClickListener(new View.OnClickListener() {
// public void onClick(View view) {
// view.setBackgroundColor(Color.DKGRAY);
// }
// });
//
// tr.setOnLongClickListener(new View.OnLongClickListener() {
// public boolean onLongClick(View v) {
// mTable = (TableRow) v; // assign selected TableRow gobally
// openContextMenu(v);
// return true;
// }
// });
}
}
// @Override
// public void onCreateContextMenu(ContextMenu menu, View v,
// ContextMenu.ContextMenuInfo menuInfo) {
// super.onCreateContextMenu(menu, v, menuInfo);
// menu.add(0, v.getId(), 0, "Do YourStuff");
//
// }
//
// @Override
// public boolean onContextItemSelected(MenuItem item) {
// int ccount = (mTable).getChildCount();
// String[] str = new String[ccount];
// for (int i = 0; i < ccount; i++) {
// TextView tv = (TextView) (((TableRow) mTable)).getChildAt(i);
// str[i] = tv.getText().toString(); // set selected text data into the
// // String array
// }
// Toast.makeText(All_CustomsActivity.this, Arrays.toString(str), 2)
// .show();
// return true;
// }
}
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none" >
<TableLayout
android:id="@+id/maintable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0,1" >
</TableLayout>
</ScrollView>
</LinearLayout>
在上面的代码中注释了一些行,这些行是我已经尝试获取行值但它失败了.谁能帮我这个?
解决方法:
我想你正在谈论在TableRow点击上获取这些值.如果是这种情况,您可以向TableRow添加一个监听器,并使用getChildAt获取两个TextView并获取数据:
//...
tr.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
TableRow t = (TableRow) view;
TextView firstTextView = (TextView) t.getChildAt(0);
TextView secondTextView = (TextView) t.getChildAt(1);
String firstText = firstTextView.getText().toString();
String secondText = secondTextView.getText().toString();
}
});
//...
标签:android,android-tablelayout,tablerow 来源: https://codeday.me/bug/20190714/1461676.html