【问题标题】:Programmatically get selected rows data以编程方式获取选定的行数据
【发布时间】:2019-01-16 10:52:15
【问题描述】:

我正在开发一个示例应用程序来了解表中的循环数据应该如何工作。

我面临的下一个挑战是如何在点击特定行时获取数据。

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_profile"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.singatour.touristattraction.ProfileActivity">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TableLayout
            android:id="@+id/tlGuide"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left">


        </TableLayout>
    </ScrollView>
</LinearLayout>

简单的程序

package com.singatour.touristattraction;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class GuideActivity extends AppCompatActivity
{
    TextView tvGuideName = null;
    TableLayout tlGuide = null;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);

        tlGuide     = (TableLayout) findViewById(R.id.tlGuide);

        for(int i  = 0; i < 100; i++)
        {
            TableRow rows = new TableRow(this);
            TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
            rows.setLayoutParams(lp);

            tvGuideName =  new TextView(this);
            tvGuideName.setId(i);
            tvGuideName.setText("I am in row " + i);
            rows.addView(tvGuideName);

            tlGuide.addView(rows, i);
        }
    }
}

我可以循环 100 行数据,范围从

我在第 0 行

我在第 1 行

我在第 2 行

..

..

我在第 99 行

但是,有没有办法获取我在点击该行时设置的数据?

【问题讨论】:

  • 也许在 textview 上注册了一个 onClickListener?你怎么看?

标签: android android-tablelayout


【解决方案1】:

我认为你需要这样的东西:

 tr = new TableRow(this);
    tr.setOnClickListener(new View.OnClickListener() {
       public void onClick(View view) {
          TableRow t = (TableRow) view;
          TextView firstTextView = (TextView) t.getChildAt(i);
          String firstText = firstTextView.getText().toString();
       }
    });

答案来源: How do I get values from a dynamically created android TableRow?

How to get selected row object from Table in Android?

如果它对你有帮助 - 投票给这个答案。我需要提高我在 Stackoverflow 上的声誉:)

【讨论】:

  • 嗨,我可以知道 (i) 来自哪里吗?
  • TextView firstTextView = (TextView) t.getChildAt(i); -- 我是你的循环计数器
  • for(int i = 0; i
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-14
  • 2012-06-25
  • 1970-01-01
  • 1970-01-01
  • 2011-11-12
  • 1970-01-01
  • 2013-04-03
相关资源
最近更新 更多