在表中处理单击侦听器

时间:2014-03-21 08:57:43

标签: android xamarin

我希望在我的表中添加单击侦听器,我在其中动态创建行。问题是,当我点击任何表格时,我得到最后一行的位置。我也使用了GetTag和SetTag,但它对我不起作用。

这是我试过的代码

private void DisplayRecords()
    {
        if (flgTable == "1") {

            TableRow tbRow1 = new TableRow (this);
            tbRow1.Clickable = true;

            for (int i = 0; i < 14; i++) {
                TextView t1 = new TextView (this);
                t1.Text = itemsTableColumn[i];
                tbRow1.AddView (t1);

            }
            tv.AddView (tbRow1);
            flgTable = "0";
        }

        string ret = findTableFields();
        string  []tblField = ret.Split('@');

        try {
            string dPath = System.IO.Path.Combine (System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal), "adodemo3.db3");
            Connection = new SqliteConnection ("Data Source =" + dPath);
            Connection.Open ();
            using (var contents = Connection.CreateCommand ()) {
                contents.CommandText = "SELECT "+ tblField[1] +" from "+ tblField[0] +" where [Sync]=0";
                var r = contents.ExecuteReader ();
                // showing header part.

                while (r.Read ()) {

                    itemsTableField = new List<string> ();
                    string currentRecordID = "";

                    string x1 = tblField[1];
                    string  []d = x1.Split(',');
                    foreach (string item in d) {
                        string it = item.Replace("[","");
                        it = it.Replace("]","");
                        if(it=="1")
                        {
                            itemsTableField.Add("");
                        }
                        else if(it == "301")
                        {
                            string temp = r[it].ToString();
                            itemsTableField.Add(temp);
                        }
                        else
                        {
                            string temp = r[it].ToString();
                            itemsTableField.Add(temp);
                        }
                    }


                    tbRow = new TableRow (this);
                    tbRow.Clickable = true;
                    tbRow.Id = j;

                    j++;
                    string fields = tblField[1];
                    string  []items = fields.Split(',');



                    for (int i = 0; i < 14; i++) {
                        TextView t1 = new TextView (this);
                        t1.Text = itemsTableField[i];
                        tbRow.AddView (t1);
                    }

                    tbRow.Click += HandleClick;
                    tv.AddView (tbRow);

                    try{
                        int rowId = tbRow.Id;
                        tbRow.SetTag(rowId,tbRow);
                    }catch(Exception e){}


                }



                Connection.Close ();

            }
        } catch (Exception ex) {
            ex.Message.ToString ();
            Connection.Close ();
        }

    }

    public void HandleClick (object sender, EventArgs e)
    {int s = tbRow.Id;

        var tag = tbRow.GetTag(s);


        Toast.MakeText(this,tag+" hi " + s,ToastLength.Long).Show();
    }



}

}

任何人都可以告诉我我在哪里做错了。我在HandleClick处理我的点击事件。请查看并帮助我

提前致谢。

1 个答案:

答案 0 :(得分:1)

当然它会返回最后一行,因为属性tbRow中的最后一行被初始化为最后一行,因此存在最后一行的实例。以这种方式更改HandleClick方法:

public void HandleClick (object sender, EventArgs e)
{
    var clickedTableRow = sender as TableRow;
    int s = clickedTableRow.Id;

    var tag = clickedTableRow.GetTag(s);


    Toast.MakeText(this,tag+" hi " + s,ToastLength.Long).Show();
}