我有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
的值
遍布整个表格
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;
我想你正在寻找这个解决方案,
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)