【问题标题】:how to send data using bundle inside RecyclerView onclick method如何在 RecyclerView onclick 方法中使用 bundle 发送数据
【发布时间】:2016-01-25 08:35:13
【问题描述】:

我想在 recycler view oclick 中发送我的姓名和图像,但我有点困惑我应该使用 paraceble 或简单的捆绑机制如何发送和接收它

public class FamousPeople

{
private static final String FAMOUS_NAME = "famous name";
private static final String FAMOUS_PHOTO="photo";
private String name;
private int photo;
private String details;

FamousPeople(String name, int photo) {

    this.name = name;
    this.photo = photo;
}
public  FamousPeople(){


}


public String getDetails() {
    return details;
}

public void setDetails(String details) {
    this.details = details;
}

public int getPhoto() {
    return photo;
}

public void setPhoto(int photo) {
    this.photo = photo;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}


   }

我必须在其中传递图像和文本数据的 BiographyViewHolder 类

 class BiographyViewHolder extends RecyclerView.ViewHolder
       implements View.OnClickListener {

   public TextView textView;
  public ImageView imageView;
List<FamousPeople> famousPeoples=GetData.getListdata();


public BiographyViewHolder(View itemView)
{
    super(itemView);
    itemView.setOnClickListener(this);
    textView= (TextView) itemView.findViewById(R.id.country_name);
    imageView= (ImageView) itemView.findViewById(R.id.country_photo);


}

@Override
public void onClick(View view) {
    Intent  intent=new Intent();
    Bundle bundle=new Bundle();

    intent.putExtras(bundle);
    view.getContext().startActivity(intent);

}

 }

GetData 类从这个类中获取数据

     public  class GetData {
    public static List<FamousPeople> getListdata() {
    List<FamousPeople> famousPeoples = new ArrayList<>();
    famousPeoples.add(new FamousPeople("rahul", R.drawable.amitabh));
    famousPeoples.add(new FamousPeople("sumit", R.drawable.cvraman));
    famousPeoples.add(new FamousPeople("neha", R.drawable.indira));
    famousPeoples.add(new FamousPeople("surbhi", R.drawable.kishorkumar));
    famousPeoples.add(new FamousPeople("rahul", R.drawable.modi));
    famousPeoples.add(new FamousPeople("sumit", R.drawable.mother));
    famousPeoples.add(new FamousPeople("neha", R.drawable.nehru)) 

    return famousPeoples;
}

【问题讨论】:

  • paraceble / 可序列化或简单的捆绑机制都适合您的情况
  • 但是序列化的性能比 paraceble 慢,我打算使用 xml pull parser 来获取数据,所以我有点困惑什么时候使用 paraceble 什么时候使用 simple bundle

标签: android


【解决方案1】:

尝试编辑您的onClick() 函数,如下所示。 您只需要从列表中获取由getAdapterPosition 指向的数据,然后将它们传递给bundle

    @Override
        public void onClick(View v) {
            Intent  intent=new Intent();
            Bundle bundle=new Bundle();
            bundle.putInt("IMAGE", famousPeoples.get(getAdapterPosition()).getPhoto());
            bundle.putString("NAME", famousPeoples.get(getAdapterPosition()).getName());
intent.putExtras(bundle);
    view.getContext().startActivity(intent);

}

【讨论】:

  • 我得到了名字但没有收到图像,
  • 见我在答案栏中添加
  • 尝试使用 setImageDrawable() 而不是 setImageResource()。还应该打印一些日志以获取传递的值和接收的值
【解决方案2】:
@Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.fragment_detail, container, false);
   textView= (TextView) view.findViewById(R.id.bioname);
    imageView= (ImageView) view.findViewById(R.id.imageView);
    Bundle getBundle = null;
    getBundle = getActivity().getIntent().getExtras();

    String name = getBundle.getString("NAME");
    int id = getBundle.getInt("IMAGE");
    textView.setText(name);
    imageView.setImageResource(id);
    return view;
}

【讨论】:

    【解决方案3】:

    要获取 image 和 name ,你必须这样做,

    例如:您的点击事件发送数据 onClick();

    ViewHolder VHheaderItem = (ViewHolder) view;
    
        VHheaderItem.image_detail.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Intent i = new Intent(CurrentClass.this, AnotherClass.class);
                                i.putExtra("Name", listItems1.get(position).getVarName(););
                                startActivity(i);
                            }
                        });
    

    收到它,

    String drName = getIntent().getStringExtra("Name");
    

    现在如果你想通过意图传递图像那么

    imageView.buildDrawingCache();
    Bitmap image= imageView.getDrawingCache();
    
    Bundle extras = new Bundle();
    extras.putParcelable("imagebitmap", image);
    intent.putExtras(extras);
    startActivity(intent);
    

    收到它,

    Bundle extras = getIntent().getExtras();
    Bitmap bmp = (Bitmap) extras.getParcelable("imagebitmap");
    
    image.setImageBitmap(bmp);
    

    更多详情请通过get ImageView's image and send it to an activity with Intenthow to pass images through intent

    【讨论】:

    • 不,它不工作我调试活动接收图像的区域,图像进入变量但不显示它,这是问题
    • 如果您使用的是私人照片;照片比意图传递int。如果你得到那个值@rahulvyas 然后设置那个图像。
    • 我的意思是当我开始调试会话时图片的 id 来了,但图片没有显示在 imageview 上
    • 问题已解决,因为我在 xml 图像视图中犯了错误
    • 是的,您已经按照我的回答中所写的 int 拍摄了图像,因此您无法获得。 @rahulvyas
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-16
    • 2012-11-20
    • 2018-09-11
    • 2012-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多