-1

Can someone help me?: I have two diffrent buttons and when I press Button 1 I want to open the DetailView with Information1. When I press Button 2 I want to open the DetailView with Information2 Here is the Tutorial I choosen for the ListView and the DetailView: http://www.raywenderlich.com/5527/getting-started-with-android-development

With the ListView it works perfect, but how to do this with two buttons?

Thanks for help :)

pesc
  • 25
  • 1
  • 7
  • I'm not gonna read the tutorial, but just to clarify what you are asking, you want to press a button, and start what, an `Activity`? – Andy Aug 04 '12 at 18:14
  • Thank you, when I press on button1 I want to start an Activity. But when I press button2 I want to open the same Activity but with diffrent text(like in the tutorial with the listView) – pesc Aug 04 '12 at 18:20

2 Answers2

0

Simply intent to that activity on button click

Button1.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

 Intent i = new Intent(MainActivity.this,DetailActivity.class);
 i.putExtra("Detail1","Detail 1");
 startActivity(i);  

   }
});


   Button2.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {

     Intent i = new Intent(MainActivity.this,DetailActivity.class);
     i.putExtra("Detail2","Detail 2");
     startActivity(i);  

       }
    });
Furqi
  • 2,403
  • 1
  • 26
  • 32
  • No not really, because I just have 1 Activity and want to fill in the right text from the DataSource – pesc Aug 04 '12 at 18:28
  • now do like this put ur detail1 data in put extra and retrieve it on detail page. – Furqi Aug 04 '12 at 20:04
  • Hmm, I think this doesn´t work too. I think it would help if you have the full code, right? – pesc Aug 04 '12 at 20:29
0

OK well in that case do this:

Button1.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {

    Intent i = new Intent(YourActivity.this,DetailActivity.class);
    i.putExtra("text","Some String for this one");

    startActivity(i);  

    }
});


Button2.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {

    Intent i = new Intent(YourActivity.this,DetailActivity.class);
    i.putExtra("text","Some other String for this one");
    startActivity(i);  

    }
});

As you can see, the method putStringExtra() form Intent allows you to send in some information. So in your DetailActivity class you can get it like so:

Intent intent = getIntent();
String text = intent.getStringExtra("text");
//and now you have the text you sent in when you created the Activity

So you can make your DetailActivity in a way where it displays whatever is sent with the intent's extras.

Andy
  • 10,553
  • 21
  • 75
  • 125
  • Hmm, I think this is not that what I mean: Did you read the tutorial? When yes I mean that: When I press on the 1. Item in the ListView it´s open the DetailView with Data for 1. When I press the 2. Item in the ListView it opens the DetailView for 2. Now, how can I do this with buttons. If you dont know what I mean, I can take some pictures to show you ;) – pesc Aug 04 '12 at 19:14