0

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: } } } 
0

5 Answers 5

1

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); 
Sign up to request clarification or add additional context in comments.

1 Comment

For me, and I think for all others too, it is difficult to understand what exactly You are trying to do. Where are You creating Your new Collector? Where does the Collector set the content view?If You go through the loop and the Collector sets the content view to Your Forwarder Activity, then only the second case will be set. The first case is "overwritten" by the second.
0
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: } } } 

Comments

0

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

Comments

0

Check the button names. They are the same (b1). Change the second variable name and everything should work perfectly. Damn copy-paste :P

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.