如何将图像从recyclerview适配器传递到另一个活动

时间:2016-12-12 10:08:33

标签: android android-intent android-recyclerview recycler-adapter

希望将RecyclerView适配器的图像传递给另一个活动。 这是我的适配器:

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
    private Context context;
    private ArrayList<Deatails> android;


    public DataAdapter(Context context,ArrayList<Deatails> android) {
        this.context = context;
        this.android = android;
    }

    @Override
    public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_row, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, final int i) {

        viewHolder.tv_name.setText(android.get(i).getName());
        viewHolder.tv_version.setText(android.get(i).getVer());
        viewHolder.tv_api_level.setText(android.get(i).getApi());

      Picasso.with(context).load("http://memaraneha.ir/Erfan/images/"+android.get(i).getPic()).resize(500,500)
                .into(viewHolder.tv_image);

    }

    @Override
    public int getItemCount() {
        return android.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        private TextView tv_name,tv_version,tv_api_level;
        private ImageView tv_image;


        public ViewHolder(final View view) {
            super(view);

            tv_name = (TextView)view.findViewById(R.id.tv_name);
            tv_version = (TextView)view.findViewById(R.id.tv_version);
            tv_api_level = (TextView)view.findViewById(R.id.tv_api_level);
            tv_image= (ImageView) view.findViewById(R.id.img);

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Bundle b = new Bundle();
                    Intent intent=new Intent(view.getContext(),Main2Activity.class);
                    String passingdata = tv_name.getText().toString();
                    b.putString("Key", passingdata);
                    intent.putExtras(b);
                    view.getContext().startActivity(intent);



                }
            });
        }
    }
}

这是我的另一项活动:

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        Bundle b = getIntent().getExtras();
        String receivingdata = b.getString("Key");
        TextView textView=(TextView)findViewById(R.id.tv_title);
        textView.setText(receivingdata);

    }
}

所以我传递了view.setOnClickListener(new View.OnClickListener() {部分

中的文字
view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Bundle b = new Bundle();
                    Intent intent=new Intent(view.getContext(),Main2Activity.class);
                    String passingdata = tv_name.getText().toString();
                    b.putString("Key", passingdata);
                    intent.putExtras(b);
                    view.getContext().startActivity(intent);
                }
            });

并在本节的另一项活动中回顾

 Bundle b = getIntent().getExtras();
        String receivingdata = b.getString("Key");
        TextView textView=(TextView)findViewById(R.id.tv_title);
        textView.setText(receivingdata);

我尝试了很多,因为传递图像而不是工作。似乎this link重复我的问题,但在我的情况下有一些不同,我很困惑。如果有人可以请求帮助

1 个答案:

答案 0 :(得分:4)

不要直接以意图传递图像。尝试在本地保存图像(或使用某些库从Web加载图像,如果它的URL图像,它们实现缓存)并仅将URI传递给此图像以进行活动。然后从传递的URI中读取图像。