【问题标题】:how to retrieve object on click of particular row in tableLayout in xamarin C#如何在 xamarin C# 中的 tableLayout 中单击特定行时检索对象
【发布时间】: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# 的新手。感谢任何帮助。

【问题讨论】:

    标签: c# android xamarin


    【解决方案1】:
       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];
    
        }
    

    要在表格的开头创建“特殊”行,您可以在数据源的开头创建一个虚拟元素

    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;
    

    然后让你的 for 循环执行并构建表 - 你需要删除行 tableItems[0] = ...

    【讨论】:

    • 如何在第一行设置默认对象值并重复查看底部边框。
    • 您可以在数据源中添加占位符值,或者在视图(和单击处理程序)中添加特殊条件,以区别对待第一行。
    • 我正在使用这个条件 "tableItems[0] = new TableHeading { HeadingID = 1, Heading = "select", SubHeading = "Hi"}; " inside for loop对于默认对象。这将出现但与第一个 json json 对象重叠。
    • 我们不是在这里解决了这个问题吗:stackoverflow.com/questions/36224037/…
    • 这里的tablerow没有eventclick的位置。我没有得到你能告诉我一些代码吗?请
    猜你喜欢
    • 2012-07-05
    • 1970-01-01
    • 2022-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多