如何将列表视图中的图像和文本传递给另一个活动

时间:2012-11-28 09:06:25

标签: android android-intent android-widget

  

可能重复:
  How to pass drawable between activities

我想将此列表视图中的图像和文本传递给另一个活动。有人可以向我解释如何将图像和文本传递给另一个显示图像和文本的活动

在代码中有两个活动home活​​动和list_display在这个list_display类中创建了带有图像和文本的自定义列表视图,我得到了包含图像和文本的列表视图,现在我想将图像和文本传递给第三个显示图像和文本的活动文本

家庭活动

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

};
}

4 个答案:

答案 0 :(得分:0)

如果需要在两个活动之间传递图像,首先必须将图像转换为位图,然后使用此代码传递位图,

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

使用此,

 Bitmap bitmap = getIntent().getParcelableExtra("bmp");

答案 1 :(得分:0)

你不能通过意图的额外传递大图像。但在您的代码中,似乎所有图像都是可绘制的资源。所以你可以传递资源id,它只是“int”号,然后在下一个活动中从资源重新加载图像。

答案 2 :(得分:0)

有效地,您可以将图像转换为位图,但我认为它没有经过优化,可能需要花费很多时间。 您应该尽可能地将图像的引用放在Extra中(就像它的资源ID一样)。

答案 3 :(得分:0)

将位图的缓冲区放入putExtra,如下所示:

  

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

     

Intent.putExtra( “MyBuffer”,mybuffer)

要从其他活动中获取,请执行以下操作:

  

int [] mynewbuffer = Intent.getIntArrayExtra(“MyBuffer”)

     

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