2016-01-25 29 views
0

我想在回收站视图O单击但把我的名字和图像数据IM有点糊涂了,我应该用paraceble或简单的捆绑机制如何我发送和recived它如何发送使用RecyclerView的onclick方法里面包

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; 
} 
+0

无论是paraceble/serializable还是简单的bundle机制在你的情况下都很好 –

+0

但是序列化在性能上比paraceble慢,我会用xml pull parser来获取数据,所以有点混淆何时使用paraceble和什么时候使用简单的捆绑 – rahul

回答

2

尝试编辑您的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); 

} 
+0

即时获取名称,但没有收到图像 – rahul

+0

请告诉我你的执行 –

+0

看到我在答案列 – rahul

0
@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; 
} 
0

要获得的图像和名称,你必须做这样的事情,

对于离:您点击事件发送数据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 Intent更多详情 和how to pass images through intent

+0

是你的问题解决? @rahul vyas –

+0

没有它的不工作我调试活动接收图像的区域,图像来到变量,但不显示它是问题 – rahul

+0

如果您使用私人诠释的照片;为照片而不是传递int意图。如果你得到那个值@rahulvyas然后设置该图像。 –

相关问题