When click Button go to DetailView
-
24-06-2021 - |
Вопрос
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 :)
Решение
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);
}
});
Другие советы
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.