Search code examples
androidjsonandroid-intentandroid-listviewcustom-lists

Passing Custom ListView Items to Other Activities in Android


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


Solution

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