Take parameters from a Recyclerview

Asked

Viewed 180 times

0

I’m developing an APP where shows the user the activities to be done in a 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 taking the data from Animeactivity and passing the "name" to the Start screen ?

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • Someone can help me ?

  • 1

    Hello, try removing your click code from the "onCreateViewHolder" method for onBindViewHolder. Improvements you can make: Create an interface and remove the code from your Adapter, second: make the object of your list implement the installable or serializable interface will be better to send the data via Bundle as well as greatly decrease your code.

  • But for me to take the variables and insert them into another screen would be like ? Could you help me ?

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 onCreateViewHolder this way:

@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();
    } 
  • This gives an error on the part: Holder.itemView.setOnClickListener(view -> { // Java 8 Intent Intent = new Intent(mContext, Animeactivity.class); Intent.putExtra("extra_anime", anime); Activity.startActivity(Intent); // TA GIVING ERROR in "Activity"

  • Activity is the context used to call startActivity(). Use the context you passed to your Adapter.

  • When I write "Activity" he of error not recognizing the word

  • Use the context you passed to your Adapter. (mContext)

  • I understood, but for example I want to take the data of a Recyclerview there would pull by name as would do to the other screen to get the information of it ?

  • It’s the last part of the answer. Retrieve the data in the Internet that started Animeactivity. But only from an anime.

  • Ae for example to in the Start fileLelatorio.java will put getIntent equal to the Animeactivity#onCreate file:

  • I want that when the guy clicks on one, he directs to another screen picking up the information from the selected Animeactivity, someone can help me ?

  • If anyone gives me a light now in doubt from above Please

  • It looks like you need a lot of guidance. The code I posted does just that. When you click on a Recyclerview item (Holder.itemView), start an Activity by going to the Anime Browser (Serializable). You send together all the anime data at that position of the list, according to your Anime model class (first part of the question). In Activity that is started, retrieves the object().

  • I’m sorry to ask you so much, but that’s because as soon as I can understand and do for the future, I’m helping people too. My question is the next one I’ve managed to do that guided by you above. Now I need that when the person chooses an item and click on the button get info ex: name, in the Starreport class. parei na parte: xtContrato = (TextView) findViewById(R.id.txt_contrato); Intent intent = getIntent(); Anime anime = (Anime) intent.getSerializableExtra("extra_anime"); txtContrato.setText(anime.getName())

  • Only in this code above it does not pass any value in txt.

  • In the second screenshot, in AnimeActivity, you have placed in extra tv_name.toString(). You must place the Anime object itself. In the third, in IniciarVisita You retrieved the object by casting it for Animeactivity, so it’s probably just Anime. PS: Do not post screenshots with code, post the whole code in the question, we have no way of knowing if there is something wrong in another part of the code and if someone is willing to help you, you will have to write all the code.

  • In this case the anime object would be described as ?

  • The anime class is "described" as example at the beginning of the answer. No onBindViewHolder Anime anime = mData.get(position); -> intent.putExtra("extra_anime", anime). The next Activity (Animeactivity) receives the Anime object. If you want to pass it from this Activity to another, make another Intent and put it back in the putExtra. In this next Activity, use Anime animeReceived = (Anime) getIntent(). getSerializableExtra("extra_anime")

Show 10 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.