I am coming across an issue with dynamically creating buttons. I have my text I am adding to the buttons, however I also have a command argument I want to send with it. I want my dynamically created buttons to open up a new Activity and pass this argument via Intent. I am a .NET guy and this would be easily done with a CommandParameter off of the Button.
My question is, is this the following code best way to accomplish this task? If so, how can I pass command arguments to the click event. If not, what should be my approach?
int counter =0;
TableLayout layout = (TableLayout) findViewById(R.id.tableLayout);
while (counter< list.size())
{
MyObj obj = list.get(counter);
Button b = new Button(this);
b.setText(obj.getName());
// CommandParameter = obj.getId().toString();
b.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
Context ctx = getApplicationContext();
Intent intent = new Intent(ctx, TestScreen.class);
intent.putExtra("Id", "MyCommandParameter");
startActivity(intent) ;
}
});
layout.addView(b);
counter++;
}