简体   繁体   中英

how can pass image from recyclerview adapter to another activity

want pass image from RecyclerView adapter to another activity . this is my adapter :

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



                }
            });
        }
    }
}

this is my another activity :

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

    }
}

so i pass text in view.setOnClickListener(new View.OnClickListener() { section

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

and recive that in another activity with this section

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

i try so many for do same to passing image but not work . seems this link duplicate my problem but have some different in my case and i am confused . if any one can please help

Don't pass images directly in intents. Try to save image locally (or use some library for loading images from web if it's URL image, they implements cache) and pass only URI to this image to activity. Then read image from passed URI.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM