1

enter image description here

This is the ListView and what I want is that when I click on any of the list item it opens in another Activity.

So far all TextView texts are passing from MainActivity to the ResultActivity but I'm not able to send the image.

MainActivity

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            TextView description = (TextView) view.findViewById(R.id.book_description);
            TextView title_ = (TextView) view.findViewById(R.id.book_title);
            TextView isbn_ = (TextView) view.findViewById(R.id.book_isbn);

            String _title = title_.getText().toString();
            String _isbn = isbn_.getText().toString();
            String _description = description.getText().toString();

            Intent myIntent = new Intent(MainActivity.this, ResultActivity.class);
            myIntent.putExtra("title", _title);
            myIntent.putExtra("isbn", _isbn);
            myIntent.putExtra("description", _description);

            startActivity(myIntent);
        }
});

ResultActivity

public class ResultActivity extends ActionBarActivity {
    TextView title, isbn, description;
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);

        title=(TextView) findViewById(R.id.r_title);
        isbn=(TextView) findViewById(R.id.r_isbn);
        description=(TextView) findViewById(R.id.r_description);

        title.setText(getIntent().getStringExtra("title"));
        isbn.setText(getIntent().getStringExtra("isbn"));
        description.setText(getIntent().getStringExtra("description"));
        description.setMovementMethod(new ScrollingMovementMethod());
    }

}
Mithun
  • 2,075
  • 3
  • 19
  • 26
bizimunda
  • 819
  • 2
  • 9
  • 26

1 Answers1

1

How are you setting the image? If you are loading it from network then you can put an image URL as a String to your intent. Store the image to cache and then get it from there in your ResultActivity using the URL.

If you are setting an image from Drawable then you can pass any convenient indication such as position, id, etc. Whatever works better for you.

In any case you need to restore the image using your key (url, id, position, etc.). Don't try to send the whole image to different activity, it's too much data to hold in Intent object.

Gennadii Saprykin
  • 4,505
  • 8
  • 31
  • 41
  • Thank you very much, Yes I'm loading it from network. But I don't know how to cache it and I don't know how to put image URL as a string the intent. Do you need my code? – bizimunda Mar 09 '16 at 07:48
  • To put an image url to intent is simple, you already do this for title, isbn and description. It will look like this: `myIntent.putExtra("url", url);` where url variable is actually the url you were using to load an image from server. To cache the image use this: http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html. Use either memory or disk cache depending on your needs. – Gennadii Saprykin Mar 09 '16 at 15:04
  • I would also consider reading this: http://stackoverflow.com/questions/541966/lazy-load-of-images-in-listview There are a lot of different solutions for lazy-loading images in `ListView` which handles everything for you (lazy image loading, caching, multithreading, OOM issues, etc.) Many of those solutions are very stable and are used by many popular apps. If you use one of them your image loading will be more robust then writing your own solution from scratch. – Gennadii Saprykin Mar 09 '16 at 15:06
  • thank you very much for your help anyways. I'll have a look on it. – bizimunda Mar 10 '16 at 19:36