0
votes

In my activity I am dynamically creating controls based off of sqlite data. Each item will have a button with a click event that needs to send that rows ID to the new activity.

I have looked at the following page and can get this to work on a button that already exists in my layout. But it doesn't seem to work when the button is dynamically created. Is there something additional I need to do with the button for this to work? https://developer.xamarin.com/recipes/android/fundamentals/activity/pass_data_between_activity/

Here is the code that creates the button dynamically:

foreach (Tasks item in table)
{
    TableRow row = new TableRow(this);
    TextView txtTask = new TextView(this);
    txtTask.Text = item.Name;
    row.AddView(txtTask);
    tableLayout.AddView(row);
    row = new TableRow(this);
    Button btnEdit = new Button(this);
    btnEdit.Text = "Edit Record";
    btnEdit.SetWidth(300);
    btnEdit.Click += delegate 
    {
        Intent viewTask = new Intent(this, typeof(UpdateTaskActivity));
        viewTask.PutExtra("TaskId", item.Id);
        StartActivity(viewTask);
    };
    row.AddView(btnEdit);

    tableLayout.AddView(row);
}

In the OnCreate method of UpdateTaskActivity I have:

string test = Intent.GetStringExtra("TaskId") ?? "error";
if (test != "error")
{
    //Do Stuff
}

But when I put a breakpoint down, my string is always null. I did put a breakpoint to make sure the correct ID is being pulled.

Why does this work with a built in button but does not work with my dynamic one?

Just to avoid confusion, in my startup screen I have a test button, and my main activity has the following code for that button. This code works fine, because the button isn't dymaically created:

//Test button
Button btnTest = FindViewById<Button>(Resource.Id.btnTest);
btnTest.Click += delegate 
{
    var activity2 = new Intent(this, typeof(UpdateTaskActivity));
    activity2.PutExtra("TaskId", "1");
    StartActivity(activity2);
};
1
Is Id on Tasks a string Type? - pnavk
It's an int currently - jpaugh78
Well thats your issue. Your "working" example doesn't match your use case. Try changing activity2.PutExtra("TaskId", "1"); to activity2.PutExtra("TaskId", 1); - then you will see the same problem - pnavk
I'll test this out when I get to work and see what happens. In my mind it seems like the way I currently have it would result in one of those "cannot convert int to string" errors. When I receive the data I'm converting it to a string for my next operation, which is why I didn't catch the mismatch. - jpaugh78
Everything is working now. Thanks for the help. When I was receiving the value I needed to convert it to a string, which is why I didn't notice my type difference. I'm still shocked that I don't get any kind of error when trying to read an int as a string, because in C# this wouldn't have even compiled before warning me about the type mismatch. - jpaugh78

1 Answers

1
votes

The main issue is that you are adding an int to the extras:

// item.Id is an int type
viewTask.PutExtra("TaskId", item.Id);

And then you are trying to get it as a string:

var test = Intent.GetStringExtra("TaskId");

There are two ways to get the value, without having to add the value as a string. Either get an int value:

var test = Intent.GetIntExtra("TaskId", 0);
if (test != 0)
{
    // Do Stuff
}

Or, you can first check for the extra, if you don't want to rely on the default value:

if (Intent.HasExtra("TaskId"))
{
    var test = Intent.GetIntExtra("TaskId", 0);
    // Do Stuff
}