M
M
Mary Solar2018-05-30 18:46:36
Android
Mary Solar, 2018-05-30 18:46:36

Why is recyclerview copied when returning from another activity?

Hello! My application does the following: I run to the api on request, take json from there, parse it, and from the data I collect a recyclerview, which consists of cards. When you click on a card, a new activity is called, where information about this card is transferred (I send it via putExtra) in order to demonstrate more detailed information.
Then I go back to maybe select another card and see that my recyclerview is being copied. I understand that the duplication happens after the onRestart() method is called. Tell me what could be the problem? in an activity with a recyclerview. Everything else works fine I would be very grateful.
ps: newbie
adapter code:

public class BooksAdapter extends RecyclerView.Adapter<BooksAdapter.MyViewHolder> {
    private Context mContext;
    private List<Book> bookList;
    final static String TAG = BooksAdapter.class.getSimpleName();

    public class MyViewHolder extends RecyclerView.ViewHolder {
         TextView title, author, publisher, description;
         ImageView thumbnail;

        public MyViewHolder(View view) {
            super(view);
            title = (TextView) view.findViewById(R.id.title);
            author = (TextView) view.findViewById(R.id.count);
            thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
            publisher = (TextView) view.findViewById(R.id.publisher);
            description = (TextView) view.findViewById(R.id.description);
        }
    }

    public BooksAdapter(Context mContext, List<Book> bookList) {
        this.mContext = mContext;
        this.bookList = bookList;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, int viewType) {
        final View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.book_card, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        final Book returnedBook = bookList.get(position);

        holder.title.setText(returnedBook.getTitle());
        holder.author.setText(returnedBook.getAuthor());
        holder.publisher.setText(returnedBook.getPublisher());
        holder.description.setText(returnedBook.getDescription());

        holder.publisher.setVisibility(View.GONE);
        holder.description.setVisibility(View.GONE);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, BookInformationActivity.class);
                intent.putExtra("image_url", returnedBook.getImageUrl());
                intent.putExtra("name_url", returnedBook.getTitle());
                intent.putExtra("author_url", returnedBook.getAuthor());
                intent.putExtra("publisher_url", returnedBook.getPublisher());
                intent.putExtra("description_url", returnedBook.getDescription());
                mContext.startActivity(intent);

            }
        });

        Glide.with(mContext)
                .load(returnedBook.getImageUrl())
                .into(holder.thumbnail);

        Log.d(TAG, "onBindViewHolder " + position);
    }

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

Code from mainactivity:
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        bookList = new ArrayList<>();
        adapter = new BooksAdapter(this, bookList);
        RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 2);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setAdapter(adapter);
        recyclerView.setHasFixedSize(false);
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question