다음 코드를 사용하여 3 개의 행이있는 테이블을 만듭니다. 자, 내가 필요한 것은 사용자가 원하는 행을 선택할 때마다 내가 선택한 행의 값을 토스트하는 것입니다. 이것은 샘플 응용 프로그램입니다. 실제로 벡터가 있고 벡터 값을 표 형식으로 표시하고 있습니다. 사용자가 tablerow를 선택할 때마다 onItemSelected를 구현하거나 해당 객체를 가져와야 할 필요가 있습니다.Android에서 표에서 선택한 행 객체를 가져 오는 방법은 무엇입니까?
아무도 도와 드릴 수 있습니까?
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를 사용하는 것이 좋을 것이다. watch [this] (http://developer.android.com/guide/topics/ui/declaring-layout.html#AdapterViews) – grine4ka