我正在使用以下代碼創建一個3行的表。現在,我需要的是每當用戶選擇一行時,我只想對選定行的值進行烘烤。這是一個示例應用程序,實際上我有一個矢量,並且以表格格式顯示矢量值。我想實現一些onItemSelected或一些事情,無論何時用戶選擇一個表我需要獲取該對象。如何從Android中的Table中獲取選定的行對象?
任何人都可以幫忙。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TableLayout table = (TableLayout) findViewById(R.id.TableLayout01);
table.removeAllViews();
String[] valuesList = {"1","2","3"};
for(int i=0;i<3;i++)
{
TableRow row = new TableRow(this);
// count the counter up by one
row.setLayoutParams(new LayoutParams(100,100));
// create a new TextView
TextView t = new TextView(this);
t.setTextColor(Color.BLACK);
// set the text to "text xx"
String value = valuesList[i];
t.setText(value);
row.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
view.setBackgroundColor(Color.DKGRAY);
}
});
View v = new View(this);
v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
v.setBackgroundColor(Color.rgb(51, 51, 51));
// add the TextView and the CheckBox to the new TableRow
TableLayout.LayoutParams tableRowParams=
new TableLayout.LayoutParams
(TableLayout.LayoutParams.FILL_PARENT,200);
int leftMargin=10;
int topMargin=5;
int rightMargin=10;
int bottomMargin=5;
tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
row.setLayoutParams(tableRowParams);
row.addView(t);
// add the TableRow to the TableLayout
table.addView(row);//,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
table.addView(v);
}
}
謝謝。
我認爲,這將是更好地使用ListView控件與適配器。觀看[this](http://developer.android.com/guide/topics/ui/declaring-layout.html#AdapterViews) – grine4ka 2013-03-13 08:46:30