1

I have two activities. Activity1.java and Activity2.java I have 5 buttons in the view of Activity1. Each button must open Activity2.java but must display different view for different buttons.

Activity1.java - activity1_view.xml

Activity2.java - activity2_view1.xml for Button 1
Activity2.java - activity2_view2.xml for Button 2
Activity2.java - activity2_view3.xml for Button 3
Activity2.java - activity2_view4.xml for Button 4
Activity2.java - activity2_view5.xml for Button 5

I have posted my code. It is not working

Can anyone tell where I am wrong?

Activity1.java

private View.OnClickListener sButton1Listener = new View.OnClickListener(){
    public void onClick(View v){
         Intent i = new Intent(Activity1.this, Activity2.class);
         i.putExtra("Button", "1");
         startActivity(i);
    }
};

Activity2. java

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    Bundle extras = getIntent().getExtras();
    if(extras !=null){
        String value = extras.getString("Button");
        if(value == "1"){
            setContentView(R.layout.activity2_view1);
        }
        else if(value == "2"){
            setContentView(R.layout.activity2_view2);
        }
        else if(value == "3"){
            setContentView(R.layout.activity2_view3);
        }
        else if(value == "4"){
            setContentView(R.layout.activity2_view5);
        }
        else if(value == "5"){
            setContentView(R.layout.activity2_view5);
        }
   }
}

Solved: As told by joni, i corrected if(value == "1") to if(value.equals("1") and the code is working. I just compared String objects just like integers. That was my problem.

  • You might want to add some code for us to look at to better help you. – kailoon Mar 29 '12 at 18:25
  • its hard to figure out the problem thi9s way. can you post some code here? thanks. – Anurag Ramdasan Mar 29 '12 at 18:25
  • please try this hope it works, first forget different different view xml and use only one xml, for that just put all the view in one linearlayout and as per your using view1,2.. just setVisibility for respective layout... – Mayur Bhola Mar 29 '12 at 18:28
  • 1
    you should use value.equals("1") instead of the == operator. – joni Mar 29 '12 at 18:55
  • @joni. Thank you. Its working now. I forgot that value is a String object :) – Praveen Sharath Mar 29 '12 at 18:59
  • you're welcome. For further understanding you might want to read this question: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java :D – joni Apr 03 '12 at 20:36

1 Answers1

0

Are you certain that the intent is getting from Activity 1 to Activity 2? Have you debugged Activity 2 to see what the incoming value is?

Though it's not necessary in this particular example, it's always a good idea to post the AndroidManifest.xml when you're asking about multiple components in an app.

Joe Malin
  • 8,621
  • 1
  • 23
  • 18
  • As told by joni, I corrected if(value == "4") to if(value.equals("4") and the code is working. I compared String objects just like integers. That was my problem. Now it is solved. Thanks. – Praveen Sharath Mar 29 '12 at 19:20