0

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..

ashif-ismail
  • 1,037
  • 17
  • 34

5 Answers5

0

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);
Jas
  • 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
0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Vishal Thakkar
  • 2,117
  • 2
  • 16
  • 33
0

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.

Anand Tiwari
  • 1,583
  • 10
  • 22
0

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);
        }
    }); 
ashif-ismail
  • 1,037
  • 17
  • 34
-1

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 

}
halfer
  • 19,824
  • 17
  • 99
  • 186
erluxman
  • 18,155
  • 20
  • 92
  • 126
  • could you be more specific please? – ashif-ismail Jan 06 '16 at 05:32
  • 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