Get parameters from a RecyclerView

0

I'm developing an APP where it shows the user the activities to be done on RecyclerView . If he wants details of the activity he can click and see more as description, name and etc.

Now I have the following problem get the data from the AnimeActivity and pass the "name" to the screen IniciaReelatorio can someone give me a light?

    
asked by anonymous 27.09.2018 / 13:26

1 answer

0

Let me try to synthesize what Alessandro Barreto told you.

Make your Anime class implement Serializable

public class Anime implements Serializable {
    private String name;
    private double rating;
    // ...

    public Anime() {

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    // ...   
}

Leave the onCreateViewHolder like this:

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(mContext).inflate(R.layout.anime_row_item, parent, false);
    MyViewHolder holder = new MyViewHolder(v);
    return holder;
}

onBindViewHolder:

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    // Supondo que mData é uma lista de animes (List<Anime>)
    Anime anime = mData.get(position);

    holder.tv_name.setText(anime.getName());
    holder.tv_rating.setText(anime.getRating());
    holder.tv_studio.setText(anime.getStudio());
    holder.tv_category.setText(anime.getCategorie());

    // Load Image from the internet and set it into Imageview using Glide
    // Edit: o que é option aqui?        
    Glide.with(mContext).load(anime.getImage_url()).apply(option).into(holder.img_thumbnail);

    holder.itemView.setOnClickListener(v -> { // Java 8
        Intent intent = new Intent(mContext, AnimeActivity.class);
        intent.putExtra("extra_anime", anime);
        mContext.startActivity(intent);
    });
}

AnimeActivity # onCreate:

Intent intent = getIntent();
Anime anime = (Anime) intent.getSerializableExtra("extra_anime");

    if (anime != null) {
        Toast.makeText(this, anime.getName(), Toast.LENGTH_LONG).show();
    } else {
        // Toast.makeText(this, "Nenhum dado foi recebido", Toast.LENGTH_SHORT).show();
        // finish();
    } 
    
01.10.2018 / 16:37