【发布时间】:2016-04-06 12:10:17
【问题描述】:
我正在尝试创建一个表格布局,其中来自服务的数据被绑定到表格的行。我希望这些事情发生
1.) tablelayout 的第一行应该是默认给定的(在我的例子中是“选择项目”)。
2.)单击任何特定行我应该能够检索该特定对象值。
3.)在行之间放置红色分隔符
下面是我使用的代码
TableLayout tl = FindViewById<TableLayout>(Resource.Id.maintable);
var client = new RestClient("http://sitemakong.net/");
var request = new RestRequest("Service/HeadingSearch", Method.POST);
request.RequestFormat = DataFormat.Json;
List<TableHeading> tableItems = client.Execute<List<TableHeading>>(request).Data;
int countValue = tableItems.Count;
TableHeading Tablevalues = new TableHeading();
for (int i = 0; i < countValue; i++)
{
tableItems[0] = new TableHeading { HeadingID = 1, Heading = "_select_", SubHeading = "Hi"};
Tablevalues = tableItems[i];
var Heading = Tablevalues.Heading;
id = Heading;
//Create a new row to be added.
tr = new TableRow(this);
tr.Id = i;
int rowId = tr.Id;
tr.SetTag(Resource.Id.rowId, tr);
tv = new TextView(this);
createView(tr, tv, id.ToString());
tl.AddView(tr);
}
}
//Method
private void createView(TableRow tr, TextView t, String viewdata)
{
t.SetText(viewdata, TextView.BufferType.Editable);
//You have to use Android.Graphics.Color not System.ConsoleColor;
t.SetTextColor(Color.Blue);
t.SetBackgroundColor(Color.Cyan);
t.SetPadding(5, 0, 0, 0);
tr.SetPadding(0, 1, 0, 1);
tr.SetBackgroundColor(Color.Black);
tr.Clickable = true;
tr.AddView(t); // add TextView to row.
}
public void HandleClick(object sender, EventArgs e)
{
var clickedTableRow = sender as TableRow;
// string strval = clickedTableRow.gett;
int s = clickedTableRow.Id;
var tag = clickedTableRow.GetTag(s);
//GET TEXT HERE
// Toast.MakeText(this, tag + " hi string " + strval, ToastLength.Long).Show();
Toast.MakeText(this, tag + " hi " + s, ToastLength.Long).Show();
}
我是 xamarin 和 c# 的新手。感谢任何帮助。
【问题讨论】: