如何在Android中从表中获取选定的行对象?

如何在Android中从表中获取选定的行对象?,android,tablelayout,Android,Tablelayout,我使用以下代码创建一个包含3行的表。现在,我需要的是,每当用户选择一行时,我只想对所选行的值进行调整。这是一个示例应用程序,实际上我有一个向量,我以表格格式显示向量值。每当用户在tablerow上选择我需要的对象时,我想实现一些onItemSelected或其他东西 有人能帮忙吗 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVi

我使用以下代码创建一个包含3行的表。现在,我需要的是,每当用户选择一行时,我只想对所选行的值进行调整。这是一个示例应用程序,实际上我有一个向量,我以表格格式显示向量值。每当用户在tablerow上选择我需要的对象时,我想实现一些onItemSelected或其他东西

有人能帮忙吗

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);
       }
}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TableLayout table=(TableLayout)findviewbyd(R.id.TableLayout01);
table.removeAllViews();
字符串[]valuesList={“1”、“2”、“3”};

对于(int i=0;i您可能需要执行以下操作:

 row.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                 Toast.makeText(getApplicationContext(), "value was "+value, 
                  Toast.LENGTH_LONG).show();
                    view.setBackgroundColor(Color.DKGRAY);
                }
                });

我想,最好使用带适配器的ListView。好的,让我检查一下。谢谢