suraj shukla
suraj shukla

Reputation: 268

Handle click listener in Table

I want add click listener in my tables Row in which I am creating rows dynamically. problem is when I click on any table Row I get the position of last row. I also used GetTag and SetTag but it dont work for me.

Here is the code which I have tried

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();
    }



}

}

can anyone please tell me where I am doing mistake. I am handling my click event on HandleClick. please review and help me

Thanks in advance.

Upvotes: 0

Views: 580

Answers (1)

choper
choper

Reputation: 1292

Of course it will return you the last row, because last one in property tbRow was initialized the last row so there is the instance of the last row. Change the HandleClick method in this way:

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();
}

Upvotes: 1

Related Questions