Activity that is sending the putExtra()
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
final Intent intent = new Intent();
// Set up different intents based on the item clicked:
switch (position)
{
case ACTIVITY_0:
intent.putExtra("Value1", "Display this text!");
intent.setClass(this, com.a.someclass.class);
}
startActivityForResult(intent, position);
}
Activity receiving the putExtra()
@Override
protected void onCreate(Bundle bundle) {
// TODO Auto-generated method stub
super.onCreate(bundle);
setContentView(R.layout.information);
Bundle extras = getIntent().getExtras();
if (extras == null) {
return;
}
String value1 = extras.getString("Value1");
if (value1 != null) {
informationTitle = (TextView)findViewById(R.id.informationTitle);
informationText = (WebView)findViewById(R.id.informationText);
informationTitle.setText(value1);
}
Original Message:
i have been searching everywhere for a good tutorial on this, i have posted my code online so people can look at it but have not found the help i needed.
I am new to this and what i am basically trying to do is just have a list of items which are all linked to one class that has a dynamic TextView that will be used for the title and a WebView for content, And so basically when a item is clicked on the list it will open up the new activity/intent and it will also pass arguments to change the TextView and WebView accordingly.
I know how to open a new activity by making a new class for each item on the list but i am pretty sure there is an easier way where i can reuse one class and just keep changing the TextView and WebView. The reason i say this is because i have 15 items on my list, but that will expand overtime so i dont want to be making 50-60 different classes to open up each new item.
If some could point me to the right tutorial or give me some insight on here i will really really appreciate it!
Thank you
To accomplish that, you would, instead of using a different
Intentfor each list item, call the sameActivitywith the sameIntent, but pass extras along with it.Let’s say, for instance, that you want to pass a different
Stringdepending on which list item is clicked. You would want toThe
Activitythat you start with thisIntentwill then be able to grab these extras in itsonCreate()usingand access the extras you put in the
Intentusing the methods outlined here. This way you can use a singleActivityfor all list items but have theActivitydisplay different information based on the extra values.