Reputation: 513
i am trying to create a tablelayout,where data from service get binded to rows of table.Here i want these things to happen
1.)First row of tablelayout should be Default given(that is "Select item" in my case).
2.)Onclick of any particular row i should be able to retrieve that particular object value.
3.)to put red color divider between rows
below is my code used
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();
}
I am new to xamarin and c#.Any help appreciated.
Upvotes: 2
Views: 915
Reputation: 89129
public void HandleClick(object sender, EventArgs e)
{
var clickedTableRow = sender as TableRow;
int s = clickedTableRow.Id;
// s is the index of the row, so just retrieve the matching object
// from the data source
var selected = tableItems[s];
}
to create the "special" row at the start of your table, you could just create a dummy element at the start of your datasource
List<TableHeading> tableItems = client.Execute<List<TableHeading>>(request).Data;
// create a dummy TableHeading, insert it at the start of the list
tableItems.Insert(0, new TableHeading { .. set the appropriate properties here .. });
int countValue = tableItems.Count;
then let your for loop execute and build the table - you will need to remove the line tableItems[0] = ...
Upvotes: 5