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

编程入门 行业动态 更新时间:2024-10-27 04:30:48
本文介绍了如何从Android中的表中获取选定的行对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

I am using following code to create a table with 3 rows. Now, what I need is whenever user selects a row I just want to Toast the value of selected row. This is a sample application, actually I have a vector and I am displaying the vector values in a tabular format. I want to implement some onItemSelected or some thing whenever user selects on a tablerow I need to get that object.

谁能帮忙解决这个问题.

Can anyone help on this.

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);
       }
}

谢谢.

推荐答案

您可能需要这样做:

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

这篇关于如何从Android中的表中获取选定的行对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 11:20:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1408887.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!