-2

Possible Duplicate:
How to pass drawable between activities

I want to pass the image and text in this listview to another activity. Can someone explain to me how to pass image and text to another activity which displays both image and text

In the code there are two activities home activity and list_display in this list_display class created custom listview with image and text and I got the listview with image and text now I want to pass image and text to third activity which displays both image and text

Home Activity

    public void onClick(View v) {
    // TODO Auto-generated method stub
    if(v.getId()==R.id.iv_animals)
    {
        Intent intent=new Intent(Home_Activity.this,List_Display.class);
        intent.putExtra("type", "animal");
        startActivity(intent);
    }else if(v.getId()==R.id.iv_wild)
    {
        Intent intent=new Intent(Home_Activity.this,List_Display.class);
        intent.putExtra("type", "wild");
        startActivity(intent);
    }

package com.myown.kidszoo;

 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;

 import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.AdapterView;
 import android.widget.AdapterView.OnItemClickListener;
 import android.widget.BaseAdapter;
 import android.widget.GridView;
 import android.widget.ImageView;
 import android.widget.ListView;
 import android.widget.SimpleAdapter;
 import android.widget.TextView;

  public class List_Display extends Activity implements OnItemClickListener {
Object animalArray[][]={{"animal",R.drawable.a_american_alligator,"ALIGATOR"},"animal",R.drawable.bear,"BEAR"},
                        {"animal",R.drawable.camel,"CAMEL"},{"animal",R.drawable.dog,"DOG"},
                        {"wild",R.drawable.elephant,"ELEPHANT"},{"wild",R.drawable.fox,"FOX"},
                        {"wild",R.drawable.giraffe,"GIRAFFE"},{"wild",R.drawable.hippopotamus,"HIPPOPOTAMUS"},


String i;
int lstIndex;
int pos;
public Context mContext;
@Override
public void onCreate(Bundle instance) {
    super.onCreate(instance);
    setContentView(R.layout.list_sample);
    i=getIntent().getExtras().getString("type");
    List<HashMap<String,Object>> nameList=new ArrayList<HashMap<String,Object>>();
    HashMap<String,Object> hm=new HashMap<String, Object>();
    if(i.equals("animal"))
    {
    for(int i=0;i<animalArray.length;i++) {
            hm.put("name", animalArray[i][1]);
            hm.put("image", animalArray[i][2]);
            nameList.add(hm);
    }}
    if(i.equals("wild"))
    {
    for(int i=0;i<animalArray.length;i++) {
            hm.put("name", animalArray[i][1]);
            hm.put("image", animalArray[i][2]);
            nameList.add(hm);
    }}


    ListView lv1=(ListView)findViewById(R.id.ListView1);
    String from[]={"image","name"};
    int to[]={R.id.iv_home_singlelist,R.id.tv_name_single};
    SimpleAdapter adapter=new SimpleAdapter(getApplicationContext(), nameList, R.layout.single_list, from, to);
    lv1.setAdapter(adapter);
    lv1.setOnItemClickListener(this);

}
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
    // TODO Auto-generated method stub
    Intent intent=new Intent(List_Display.this,Fullimage_Display.class);
    //
    // what data i need to give here to go to next activity
    startActivity(intent);

};
}
Community
  • 1
  • 1
user1659482
  • 41
  • 1
  • 5
  • 1
    U need to show the selected list item (Image + text), in the next activity, right ?, If so, Use Bundle to put those datatype that you need to pass to the next activity and retrieve it in the next activity. – Nandagopal T Nov 28 '12 at 09:12
  • where exactly you are passing An image?! remember you can not pass image file directly , you need to decode it first – Mahdi Giveie Nov 28 '12 at 09:13
  • This will help you. http://stackoverflow.com/questions/8407336/how-to-pass-drawable-between-activities Cheers – Nandagopal T Nov 28 '12 at 09:17
  • I agree with @NandagopalT You can pass an image object and string to the bundle with key-value pair. Do it in onItemClick method, not in onCLick. – 0xC0DED00D Nov 28 '12 at 09:29
  • You can use putExtra, as other people also mentioned. And also you can use static reference of image and text and use them anywhere in the app (in all the activities) – Tahreem Nov 28 '12 at 09:20

4 Answers4

0

If you need to pass a image between two activities means, first you must covert the image to bitmap, then use this code to pass the bitmap,

 Intent intent = new Intent(Current.this, Next.class);
 intent.putExtra("bmp", bitmap); // for image
 intent.putExtra("text", text); //for text 
 startActivity(intent);

in activity two use this,

 Bitmap bitmap = getIntent().getParcelableExtra("bmp");
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
Aerrow
  • 12,086
  • 10
  • 56
  • 90
0

You cannot pass big image via intent's extra. But in your code, it seems all images are drawable resources. So you can just pass the resource id which is just "int" number, then reload image from resource in the next activity.

TieDad
  • 9,143
  • 5
  • 32
  • 58
0

Effectively you can transform your image into a bitmap, but i think it's not optimized and can take so much time. As much as possible, you should put the image's reference in your Extra (like its ressource ID).

Goo
  • 1,318
  • 1
  • 13
  • 31
0

Put the buffer of the bitmap into the putExtra, as below:

Bitmap.getPixels(int[] mybuffer,...)

Intent.putExtra("MyBuffer",mybuffer)

To get it from other activity, just do:

int[] mynewbuffer = Intent.getIntArrayExtra("MyBuffer")

Bitmap myBitmap = Bitmap.createBitmap(mybuffer,...)

e7fendy
  • 176
  • 1
  • 5