so that my news app has a custom listview with the parsed thumbnail,headline and the news url,now i wish to pass this three items to another activity using intent which must show particular news items from the list with complete news details,please help me..
-
Possible duplicate of [How do I pass data between activities on Android?](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-on-android) – user1140237 Jan 06 '16 at 05:33
-
this may help http://stackoverflow.com/a/2091482/1140237 – user1140237 Jan 06 '16 at 05:34
5 Answers
Simply use Intent
to pass the data from ListView
to detail page:
Intent i = new Intent(FirstScreen.this, DetailScreen.class);
String title = "title";
String details = "details";
.....
i.putExtra("STRING_TITLE", title);
i.putExtra("STRING_DETAILS", details);
...
Pass the data you need here.
Receive the Intent in your DetailScreen.class
String titleString,....;
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
titleString= null;
} else {
titleString= extras.getString("STRING_TITLE");
....
}
} else {
titleString= (String) savedInstanceState.getSerializable("STRING_TITLE");
.....
}
Retrieve the text from your TextView like this:
String title = ((TextView) view.findViewById(your title textview id)).getText().toString();
Intent newsIntent = new Intent(getApplicationContext(), NewsDetails.class);
newsIntent.putExtra("title",title);
startActivity(newsIntent);

- 3,207
- 2
- 15
- 45
-
brother,how do i get the news title that is in the list view of the first activity to the second activity, i have already tried your method but it only shows the title of the last item in the list view...i need to get the title depending on the list which user clicks in – ashif-ismail Jan 06 '16 at 05:43
Try this one to pass Url:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Uri uri = Uri.parse(myRssFeed.getItem(position).getLink());
Intent w = new Intent(this, Contenturl.class);
w.putExtra(org.rss.mywindows.Contenturl.URL,uri.toString());
startActivity(w);
In other activity where you want try this:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contenturl);
String turl = getIntent().getStringExtra(URL);
Uri feedUri = Uri.parse(turl);
Headline: I think its string so its not hard to pass from intent.
If you want to pass object then you should do with Parcelable Search on it you get its better way then to pass all things in intent one by one values.

- 19,824
- 17
- 99
- 186

- 2,117
- 2
- 16
- 33
-
-
@a_lmukthar What happn please inform me.and if this answer help you then dont forget to give up vote – Vishal Thakkar Jan 06 '16 at 06:02
-
-
yep,json parsing works in list activity,what i want is to send the title of the selected item in the list view to other activity...right now i get only the title of the last item in the list view irrespective of the row clicked in list activity... – ashif-ismail Jan 06 '16 at 06:46
-
@a_lmukthar you first Parse Json and add values in Model class then set that in listview so you get item value from model class – Vishal Thakkar Jan 06 '16 at 06:59
-
that means,should i extend my NewsDetails Activity with my model class?...i got ur idea but how do i implement it ? – ashif-ismail Jan 06 '16 at 07:04
-
thanks for ua eagerness in helping!i'll upvote urs once if this works – ashif-ismail Jan 06 '16 at 07:13
Passing a custom object need to be Parcelable implemented, when passing between Activity. Intent class method below allow to pass parcelable object.
putExtra(String name, Parcelable value)
and below method allow to pass complete list of data
putParcelableArrayListExtra(String name, ArrayList<? extends Parcelable> value)
You can take a look in blog for parcelable implementation, at http://www.survivingwithandroid.com/2015/05/android-parcelable-tutorial-list-class.html
You can google similar solution yourself.

- 1,583
- 10
- 22
for those who may be having similar problem,this is how i send multiple items in the same custom listview as intents
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent newsIntent = new Intent(getApplicationContext(),NewsDetails.class);
titleText = (TextView) view.findViewById(R.id.newstitle);
TextView urlText = (TextView) view.findViewById(R.id.url);
title = titleText.getText().toString();
feedUrl = urlText.getText().toString();
newsIntent.putExtra("title",title);
newsIntent.putExtra("url",feedUrl);
startActivity(newsIntent);
}
});

- 1,037
- 17
- 34
I am still new in the programming world, but what I do in such cases is to store those custom listview objects as a "static" attribute in a class I make which is purely made for storing things we need in another activity.
Then start the activity you want to go to and use the custom ListView objects stored as static attribute of the class made for dedicated data transfer.
For example: you have to pass CustomListview Object to another Activity:
ActivityOne {
CustomListView clv = new CustomListView () ;
ObjectTransferClass.customListView = clv;
}
ObjectTransferClass {
public static MyCustomListView customListView ;
// other objects to transfer can be declaerd here
}
ActivityTwo {
MyCustomListView clvFromActivityOne = ObjectTransferClass.customListView;
// use clvFromActivityOne for further processes
// do other things
}
-
-
If you want to access to your custom listview in ActivityOne from ActivityTwo then unnecessary to do above way. Instead you can declare your custom listview a static in global class and you can access it in any where you want. – GiapLee Jan 06 '16 at 05:36
-
This is not correct and recommended to pass data between activity. – Anand Tiwari Jan 06 '16 at 05:39
-
i have already tried the approaches that were answered before,but what i get is the title of the last list item in the listview irrespective of the item click...ie even if i click the first item, i get the last item title.. this happens for all cases..how about getting the title using the position variable in OnItemClickListener??How would i do it? – ashif-ismail Jan 06 '16 at 05:46
-
You can handle the index on OnClickListener of listview where there must be a "index" variable being passed into onClick( View v , int index ) { – erluxman Jan 06 '16 at 05:48