Recognizing A Button From A Dynamic View
Solution 1:
I assume that you are using the button tags in your click processing. To keep the tag data and add the needed wiring between buttons, you can create a data structure that would serve as a tag:
staticclassButtonTag {
String buttonType;
Button partner;
ButtonTag(Stringtype, Button button) {
buttonType = type;
partner = button;
}
}
Then you could reorganize your setup code:
for(inti=1; i <= 2 ; i++) {
Buttonbutton1=newButton(this);
button1.setId(i);
layout.addView(button1);
Buttonbutton2=newButton(this);
button2.setId(i);
button2.setEnabled(false);
button1.setTag(newButtonTag("age", button2));
button2.setTag(newButtonTag("country", button1));
layout.addView(button2);
}
The click processing will obviously need to be changed to cast getTag()
to a ButtonTag
instead of a String
.
If you don't need the "age" and "country" information to distinguish button types, just set each button as the tag for the other.
EDIT:
With the latter scheme, here's how you would use this in a click listener:
publicvoidonClick(View v) {
Objecttag= v.getTag();
if (tag instanceof Button) {
Buttonbtn= (Button) tag;
btn.setEnabled(true);
v.setEnabled(false);
}
}
If you needed the "age" and "country" part of the tag for other reasons, the code would be only a little different:
publicvoidonClick(View v) {
Objecttag= v.getTag();
if (tag instanceof ButtonTag) {
ButtonTagbTag= (ButtonTag) tag;
bTag.partner.setEnabled(true);
v.setEnabled(false);
}
}
Solution 2:
I got a solution to the problem after referring to this question here ( find button by ID or TAG ), it solves the problem I was facing as such !
publicclassDynmaicViewExperimentActivityextendsActivityimplementsOnClickListener{
List<Button> buttons;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
buttons = newArrayList<Button>();
setDynamicContentViewOfThisPage();
}
publicvoidonClick(View v) {
// TODO Auto-generated method stubintbuttonType=0;
if (v.getTag()=="age")
buttonType = 1;
elseif (v.getTag()=="country")
buttonType = 2;
switch (buttonType) {
case1:
for(Button b: buttons) {
if(b.getId() == v.getId() && b.getTag().equals("country")){
b.setEnabled(true);
v.setEnabled(false);
}
case2:
for(Button b: buttons) {
if(b.getId() == v.getId() && b.getTag().equals("age")){
b.setEnabled(true);
v.setEnabled(false);
}
}
}
}
privatevoidsetDynamicContentViewOfThisPage() {
// Defining the Scroll View and the LinearLayoutScrollViewsv=newScrollView(this);
LinearLayoutl=newLinearLayout(this);
l.setOrientation(LinearLayout.VERTICAL);
sv.addView(l);
for(inti=1; i <= 2 ; i++) {
Buttonbutton1=newButton(this);
button1.setId(i);
button1.setTag("age");
buttons.add(button1);
l.addView(button1);
Buttonbutton2=newButton(this);
button2.setId(i);
button2.setTag("country");
buttons.add(button2);
l.addView(button2);
button.setOnClickListener(this);
button2.setOnClickListener(this);
// Set the content View to thisthis.setContentView(sv);
}
}
}
Post a Comment for "Recognizing A Button From A Dynamic View"