【问题标题】:How to pass image and text in list view to another activity [duplicate]如何将列表视图中的图像和文本传递给另一个活动[重复]
【发布时间】:2012-11-28 09:06:25
【问题描述】:

可能重复:
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);

};
}

【问题讨论】:

  • 你需要在下一个活动中显示选定的列表项(图像+文本),对吗?,如果是这样,使用 Bundle 将那些你需要传递给下一个活动的数据类型并检索在下一个活动中。
  • 你到底在哪里传递图像?!记住不能直接传图片文件,需要先解码
  • 这将对您有所帮助。 stackoverflow.com/questions/8407336/…干杯
  • 您可以使用 putExtra,正如其他人也提到的那样。您还可以使用图像和文本的静态引用并在应用程序的任何位置(在所有活动中)使用它们
  • 我同意@NandagopalT 您可以使用键值对将图像对象和字符串传递给捆绑包。在 onItemClick 方法中执行,而不是在 onCLick 中。

标签: android android-intent android-widget


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      如果你需要在两个activity之间传递图片,首先你必须将图片转换为bitmap,然后使用这段代码来传递bitmap,

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

      【讨论】:

        【解决方案4】:

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

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

        Intent.putExtra("MyBuffer",mybuffer)

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

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

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

        【讨论】:

          猜你喜欢
          • 2012-01-13
          • 2023-03-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多