1

i am using json method to get text and image in listview. I am getting both text and image in listview. But i am getting text only in next activity. I dont no how to get image in next activity. Anyone can help me...?

MyCode:

MainActivity

listview.setOnItemClickListener(new OnItemClickListener() {

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

                // getting values from selected ListItem
                String title = ((TextView) view.findViewById(R.id.title)).getText().toString();
                String desc = ((TextView) view.findViewById(R.id.desc)).getText().toString();
                String image = ((ImageView) view.findViewById(R.id.image1)).getImageMatrix().toString();

                // Starting new intent
                Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
                in.putExtra("TAG_TITLE", title);
                in.putExtra("TAG_DESC", desc);
                in.putExtra("TAG_IMAGE", image);

                startActivity(in);
            }
        });

    } 

SingleMenuItemActivity

public class SingleMenuItemActivity extends Activity {

Bitmap bmimage = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.single_list);

    // getting intent data
    Intent in = getIntent();

    // Get JSON values from previous intent
    String title = in.getStringExtra("TAG_TITLE");
    String desc = in.getStringExtra("TAG_DESC");
    String image = in.getStringExtra("TAG_IMAGE");

    try {
        URL url = new URL(image);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();
        bmimage = BitmapFactory.decodeStream(is);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


     TextView lbltitle = (TextView) findViewById(R.id.title);
     TextView lbldesc = (TextView) findViewById(R.id.desc);
     ImageView lblimage = (ImageView) findViewById(R.id.image1);

    // Displaying all values on the screen

    lbltitle.setText(title);
    lbldesc.setText(desc);
    lblimage.setImageBitmap(bmimage); 


}

}

2 Answers2

3

The best solution is not send bitmap by parcelable because image can be too big - and application in some devices will crash.

I recommended just save bitmap in sdcard - send by extras link/url where bitmap is save and in next activity just load bitmap from sdcard.

Saving bitmap

Load bitmap

Community
  • 1
  • 1
Dariusz Mazur
  • 587
  • 5
  • 21
0

Pass the image bitmap instead of a String

ImageView imageView = (ImageView) view.findViewById(R.id.image1);
Bitmap image = ((BitmapDrawable)imageView.getDrawable()).getBitmap();

// ...

// Starting new intent
Bundle extras = new Bundle();
extras.putParcelable("TAG_IMAGE", image);
in.putExtras(extras);
startActivity(in);

And in the second activity, read it out of the bundle

Bundle extras = getIntent().getExtras();
Bitmap bmp = (Bitmap) extras.getParcelable("TAG_IMAGE");
// ...
lblimage.setImageBitmap(bmp );
Nachi
  • 4,218
  • 2
  • 37
  • 58
  • hi @Nachi, i want only "title" in listview at first activity and then we click in any one item(title) in listview means it will show all details of that item in second activity. i tried but its not working. – Arutkumaran Sep 11 '13 at 12:54
  • It's not really clear what you are trying to do. Better create a new question for this. – Nachi Sep 11 '13 at 13:25