Android Development: Passing A Parameter Into An Onclicklistener()
Basically I am creating buttons within a for loop, I need each button to return a different value when pressed. I had thought that creating my own onClickListener() and passing in
Solution 1:
Toast.makeText
takes string resource id as a second argument. Your counter value is not a valid resource id that's way you are getting an error. You need to pass a String
instead of int
and it will work.
Toast.makeText(v.getContext(), String.valueOf(counter), Toast.LENGTH_LONG).show();
Solution 2:
Instead of creating multiple listeners you can use setTag()
moreInfo = newButton(this);
moreInfo.setText("More Info");
moreInfo.setTag(newInteger(counter));
moreInfo.setOnClickListener(newDynamicOnClickListener();
then, in your listener
public void onClick(View v) {
Log.v("DynamicOnClickListener","1");
Toast.makeText(v.getContext(), ((Integer)v.getTag()).toString(), Toast.LENGTH_LONG).show();
}
Post a Comment for "Android Development: Passing A Parameter Into An Onclicklistener()"