Nawaz Nawaz - 1 month ago 13
Android Question

intent put extra method is crashing the app

So here is what I want to do ,
get the image link from Mydata, display it in image view and then send the image link via intent put extra method to webview activity and display the image in webview from that image link.

The images are loading in Image Views but whenever I try to send the image_link via intent putEtra method the app is crashing on onClick method.

I think something is wrong with

intent.putExtra("image",my_data.get(position).getImage_link());

may be I am not getting the image link refernece

MY DATA code,

package com.techhgeeks.techhgeeks;


public class MyData {

private int id;
private String description, image_link;

public MyData(int id, String description, String image_link) {
this.id = id;
this.description = description;
this.image_link = image_link;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getImage_link() {
return image_link;
}

public void setImage_link(String image_link) {
this.image_link = image_link;
}
}


Adapter code,

package com.techhgeeks.techhgeeks;
import android.content.Intent;


import android.support.v7.widget.RecyclerView;
import android.content.Context;
import android.widget.ImageView;
import android.widget.TextView;

import android.view.LayoutInflater;
import java.util.List;
import com.bumptech.glide.Glide;
import android.view.View;
import android.view.ViewGroup;





public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private Context context;
private List<MyData> my_data;

public MyAdapter(Context context, List<MyData> my_data) {
this.context = context;
this.my_data = my_data;
}



@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_child,parent,false);

final ViewHolder holder = new ViewHolder(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = holder.getAdapterPosition();
Intent intent = new Intent(context,article.class);
intent.putExtra("image",my_data.get(position).getImage_link());
context.startActivity(intent);

}
});

return new ViewHolder(itemView);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

holder.description.setText(my_data.get(position).getDescription());
Glide.with(context).load(my_data.get(position).getImage_link()).into(holder.imageView);



}

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




public class ViewHolder extends RecyclerView.ViewHolder{

public TextView description;
public ImageView imageView;

public ViewHolder(View itemView) {
super(itemView);
description = (TextView) itemView.findViewById(R.id.textView);
imageView = (ImageView) itemView.findViewById(R.id.image);
}


}


}

Answer

You should be setting your onClick listener in the onBindViewHolder method.

Try this:

package com.techhgeeks.techhgeeks;
import android.content.Intent;


import android.support.v7.widget.RecyclerView;
import android.content.Context;
import android.widget.ImageView;
import android.widget.TextView;

import android.view.LayoutInflater;
import java.util.List;
import com.bumptech.glide.Glide;
import android.view.View;
import android.view.ViewGroup;





public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>   {
    private Context context;
    private List<MyData> my_data;

    public MyAdapter(Context context, List<MyData> my_data) {
        this.context = context;
        this.my_data = my_data;
    }



    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_child,parent,false);

        final ViewHolder holder = new ViewHolder(itemView);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        holder.description.setText(my_data.get(position).getDescription());
        Glide.with(context).load(my_data.get(position).getImage_link()).into(holder.imageView);
        holder.view.setOnClickListener(new MyOnItemClickListener(position));
    }

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




    public  class ViewHolder extends  RecyclerView.ViewHolder{

        public TextView description;
        public ImageView imageView;
        public View view;

        public ViewHolder(View itemView) {
            super(itemView);
            view = itemView;
            description = (TextView) itemView.findViewById(R.id.textView);
            imageView = (ImageView) itemView.findViewById(R.id.image);
        }


    }

     private class MyOnItemClickListener implements View.OnClickListener {
        private int position;
        public MyOnItemClickListener(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context,article.class);
            intent.putExtra("image",my_data.get(position).getImage_link());
                context.startActivity(intent);
        }
    }
}