Hello I am trying to pass data from my RecyclerView in Fragment, to my activity. But the app crashes. The code works when i try to open another activity so im guessing the error is in my Gallery.java activity. I tried putting .asBitMap where it says Glide in Gallery.java Activit, but it is marked as an error Here is my Gallery.java
package co.quindio.sena.navigationdrawerejemplo;
import android.nfc.Tag;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
public class Gallery extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
getIncomingIntent();
}
private void getIncomingIntent(){
if(getIntent().hasExtra("image_url")){
String imageUrl = getIntent().getStringExtra("image_url");
String imageName = getIntent().getStringExtra("image_name");
setImage(imageUrl,imageName);
}
}
private void setImage(String imageUrl, String imageName){
TextView name= (TextView) findViewById(R.id.idInfo);
ImageView image= (ImageView) findViewById(R.id.idImagen);
Glide.with(this)
.load(imageUrl)
.into(image);
}
}
Here is my adapter class
package co.quindio.sena.navigationdrawerejemplo.adapters;
import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import co.quindio.sena.navigationdrawerejemplo.Gallery;
import co.quindio.sena.navigationdrawerejemplo.R;
import co.quindio.sena.navigationdrawerejemplo.clases.PersonajeVo;
import de.hdodenhof.circleimageview.CircleImageView;
/**
* Created by CHENAO on 13/07/2017.
*/
public class PersonajesAdapter extends RecyclerView.Adapter<PersonajesAdapter.PersonajeViewHolder>{
ArrayList<PersonajeVo> listaPersonaje;
private Context mContext;
public PersonajesAdapter(Context context,ArrayList<PersonajeVo> listaPersonaje) {
this.listaPersonaje=listaPersonaje;
this.mContext=context;
ArrayList<String> mImages = new ArrayList<>();
ArrayList<String>mImageNames = new ArrayList<>();
}
@Override
public PersonajeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list,null,false);
return new PersonajeViewHolder(view);
}
@Override
public void onBindViewHolder(PersonajeViewHolder holder, final int position) {
holder.txtNombre.setText(listaPersonaje.get(position).getNombre());
holder.txtInformacion.setText(listaPersonaje.get(position).getInfo());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, Gallery.class);
intent.putExtra("image_url", (Parcelable) listaPersonaje.get(position));
intent.putExtra("image_name", (Parcelable) listaPersonaje.get(position));
mContext.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return listaPersonaje.size();
}
public class PersonajeViewHolder extends RecyclerView.ViewHolder {
TextView txtNombre,txtInformacion;
ImageView foto;
public PersonajeViewHolder(View itemView) {
super(itemView);
txtNombre= (TextView) itemView.findViewById(R.id.idNombre);
txtInformacion= (TextView) itemView.findViewById(R.id.idInfo);
foto= (ImageView) itemView.findViewById(R.id.idImagen);
}
}
}
the problem is:
you send a Parcelable:
intent.putExtra("image_url", (Parcelable) listaPersonaje.get(position));
intent.putExtra("image_name", (Parcelable) listaPersonaje.get(position));
but get it as String
String imageUrl = getIntent().getStringExtra("image_url");
String imageName = getIntent().getStringExtra("image_name");
so please using getIntent().getParcelableExtra("image_url")
and getIntent().getParcelableExtra("image_name")
or send a String
intent.putExtra("image_url", listaPersonaje.get(position).getImageUrl());
intent.putExtra("image_name",listaPersonaje.get(position).getImageName());
assume listaPersonaje
item have getImageName()
and getImageUrl()
method that get "image_url"
and "image_name"
value
UPDATE 1 If you want send a drawable, first, convert it to byte array
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.your_resource);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
and pass it via intent extras, send:
intent.putExtra("image_url", b);
and receiver:
byte[] b = getIntent().getByteArray("image_url");
finally, using it for image view
Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
UPDATE 2 In case we have a drawble, we need to convert it to bitmap
public static Bitmap drawableToBitmap (Drawable drawable) {
Bitmap bitmap = null;
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
if(bitmapDrawable.getBitmap() != null) {
return bitmapDrawable.getBitmap();
}
}
if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
} else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
UPDATE 3 , finally, using both of 2 way above, we have a way to resolve your issue when pass a drawable via intent
Drawable drawable = listaPersonaje.get(position).getImageUrl()
// convert drawable to byte array
Bitmap bitmap = drawableToBitmap(drawable);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
// send it
intent.putExtra("image_url", b);
<!----------------------------------------------->
// receiver
byte[] b = getIntent().getByteArray("image_url");
// and using glide to show it
Glide.with(this)
.load(b)
.asBitmap()
.into(imageview);
If you are passing only image_url and image_name then you have no need to pass as Parcelable.you just pass the only string in intent.
Intent intent = new Intent(mContext, Gallery.class);
intent.putExtra("image_url",listaPersonaje.get(position));
intent.putExtra("image_name", listaPersonaje.get(position));
mContext.startActivity(intent);
And fetch data in Gallery Activity
String imageUrl = getIntent().getStringExtra("image_url");
String imageName = getIntent().getStringExtra("image_name");
I hope its help you.
I will add some more information here in addition to the @gianhtran s answer.
In bindviewholder method, you will get the PersonajeVo object by listaPersonaje.get(position).
@Override
public void onBindViewHolder(PersonajeViewHolder holder, final int position) {
final PersonajeVo personajeVo = listaPersonaje.get(position);
holder.txtNombre.setText(personajeVo.getNombre());
holder.txtInformacion.setText(personajeVo.getInfo());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, Gallery.class);
intent.putExtra("image_url", personajeVo.getImageUrl());
intent.putExtra("image_name", personajeVo.getImageName());
mContext.startActivity(intent);
}
});
}
Secondly, if you want all of the details from PersonajeVo object, then you can put it this way,
Intent intent = new Intent();
intent.putExtra("personaje_obj",personajeVo); //make sure personjeVo model class is parcelable or cast it as (Parcelable)personajeVo
mContext.startActivity(intent);)
and getting it back in another activity as follows,
Intent mIntent = getIntent();
if(mIntent!=null && mIntent.hasExtra("personaje_obj"))
{
PersonajeVo personajeVo = mIntent.getParcelableExtra("personaje_obj");
}
Actually no need to intent inside onBindViewHolder. Do it in this way
public class PersonajeViewHolder extends RecyclerView.ViewHolder {
TextView txtNombre,txtInformacion;
ImageView foto;
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int get = AdatperPosition();
if(pos != RecyclerView.NO_POSITION){
Item clickDataItem = listaPersonaje.get(position);
Intent intent = new Intent(mContext, Gallery.class);
intent.putExtra("image_url", listaPersonaje.get(position));
intent.putExtra("image_name", listaPersonaje.get(position));
mContext.startActivity(intent);
}
});
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.