Reputation: 473
Below is what i have tried. "Forward" class extends Activity. It contains two buttons. In this class , we create two objects of "Collector" class using for loop. In the first iteration of the loop we create first button. In the second, we create one more button. we set "OnClickListener" for both the buttons. but only the second button responds for the click. First button doesnt respond to a click. I'm trying to use same variable name (b1) - I want to stick to OO Principle by not creating separate object for achieving my goal. - Also I'm expecting properties of 2 Objects as separate entity.Please help me. regards.
Forward.java (is an Activity, contains two buttons):
below is a for loop in which i create 2 objects of collector class.
for (int i = 0; i < 2; i++) {
new Collector(this, i);
}
Collector.java :
public class Collector {
Forwarder f;
int n;
Button b1;
public Collector(Forwarder caller, int i) {
f = caller;
n = i; // 0 or 1
f.setContentView(R.layout.forwarder);
switch(n)
{
case 0:
b1 = (Button) f.findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// get a new Contact
Intent i = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
f.startActivityForResult(i, 1);
// onActivityResult has to be implemented in f because
// f extends Activity class
}
});
break;
case 1:
b1 = (Button) f.findViewById(R.id.button2);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// get a new Contact
Intent i = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
f.startActivityForResult(i, 1);
}
});
break;
default:
}
}
}
Upvotes: 0
Views: 108
Reputation: 150
I don't have the answer on why such behavior but it will definitively help you if you add few things as a part of clarification or rephrase your question else people consider it as some novice mistake. - Clearly mention that you know that you are trying to use same variable name (b1) - You want to stick to OO Principle by not creating separate object for achieving your goal. - Also you can mention that you are expecting properties of 2 Objects as separate entity.
Hope it helps. All the best.
Upvotes: 0
Reputation: 661
Check the button names. They are the same (b1). Change the second variable name and everything should work perfectly. Damn copy-paste :P
Upvotes: 0
Reputation: 55
You have mistaken in your switch case you named both of buttons b1 when the second object starts to be create b1 is no longer Button1 now it is Button2 so you set onclick listener only for Button2 use a custom class for your buttons or try to declear new objects like b2 b3 good luck soheil
Upvotes: 0
Reputation: 9870
You gave Your button a new id.
first, You wrote:
b1 = (Button) f.findViewById(R.id.button1);
at the second switch You wrote:
b1 = (Button) f.findViewById(R.id.button2);
Upvotes: 1
Reputation: 7560
public class Collector {
Forwarder f;
int n;
Button b1;
public Collector(Forwarder caller, int i) {
f = caller;
n = i; // 0 or 1
f.setContentView(R.layout.forwarder);
switch(n)
{
case 0:
b1 = (Button) f.findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// get a new Contact
Intent i = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
f.startActivityForResult(i, 1);
// onActivityResult has to be implemented in f because
// f extends Activity class
}
});
break;
case 1:
b1 = (Button) f.findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// get a new Contact
Intent i = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
f.startActivityForResult(i, 1);
}
});
break;
default:
}
}
}
Upvotes: 0