如何在Android中获取TableLayout单元格值

问题描述 投票:0回答:2

我有3x3行和col的TableView

    var tableLayout = FindViewById<TableLayout>(Resource.Id.tableLayout);
    rowCount = 3; columnCount = 3;
    TableLayout table = new TableLayout(this);
    for (int i = 0; i < rowCount; i++)
    {
        TableRow row = new TableRow(this);
        for (int j = 0; j < columnCount; j++)
        {
            var cell = new TextView(this);
            cell.SetText("(" + GetRandomItem() + ")", TextView.BufferType.Normal);
            row.AddView(cell);
        }
        tableLayout.AddView(row);
    }

并分配随机值。使用以下功能

private string GetRandomItem()
        {
            var charList = new List<string> { "A", "B", "C" };
            var random = new Random();
            int index = random.Next(0,charList.Count);
            return charList[index];
        }

现在我如何在index上获得基于TableLayout的值

android xamarin xamarin.android
2个回答
1
投票

遍布整个表格

for (int i = 0; i < rowCount; i++)
   {
     Android.Widget.TableRow row = (Android.Widget.TableRow)tableLayout.GetChildAt(i);
     for (int j = 0; j < columnCount; j++)
        {
          TextView cell = (TextView) row.GetChildAt(j);
          var value = cell.Text;
        }
   }

获得任何特定的细胞价值

例如得到单元格(1,2)的值

Android.Widget.TableRow row = (Android.Widget.TableRow)tableLayout.GetChildAt(1);
TextView cell = (TextView) row.GetChildAt(2);
var value = cell.Text;

1
投票

我想你正在寻找这个解决方案,

var tableLayout = FindViewById<TableLayout>(Resource.Id.tableLayout);
          int  rowCount = 3;int  columnCount = 3;
            TableLayout table = new TableLayout(this);
            for (int i = 0; i < rowCount; i++)
            {
                TableRow row = new TableRow(this);
                for (int j = 0; j < columnCount; j++)
                {
                    var cell = new TextView(this);
                    cell.SetText("(" + GetRandomItem(j) + ")", TextView.BufferType.Normal);
                    row.AddView(cell);
                }
                tableLayout.AddView(row);
            }

private string GetRandomItem(int index)
        {
            var charList = new List<string> { "A", "B", "C" };
            //var random = new Random();
            //int index = random.Next(0, charList.Count);
            return charList[index];
        }

输出是:

(A)(B)(C)

(A)(B)(C)

(A)(B)(C)

© www.soinside.com 2019 - 2024. All rights reserved.