I practiced with below source code. But I want to create separate activities for separate books.I can add more books to this app.But I can't create separate activities for each and every books.It means anyone click any book should open it's activity(let's think open pdf version view of the book).Please help me to create it.I want full code.because I'm a bigginer...Thank you...You can edit it via Github.....
Asked
Active
Viewed 69 times
2 Answers
1
You can create a BookDetailActivity
like the one that you have created in your source code.
Then you have to implement Serializable
in your POJO class. ie. Book.java
Just write public class Book implements Serializable
and then you will be able to pass a single Book
object from your RecyclerViewAdapter
to BookDetailActivity
via an Intent
.
You can do so by using following code in your holder.cardView.setOnClickListener
's onClick
:
Intent intent = new Intent(mContext, BookDetailActivity.class);
intent.putExtra("Book", mData.get(position)); //mData is a list of books and mData.get(position) will give a Book object.
mContext.startActivity(intent);
And in you BookDetailActivity
use the following code to get the object from the intent.
// To retrieve object in second Activity
if (getIntent().hasExtra("Book")){
Book book= (Book) getIntent().getSerializableExtra("Book");
Log.d(TAG, " Got serialized object "+ book.getTitle());
/*
Then you can get what you want from the Book object and set to textviews or other stuffs.
*/
}
I have assumed that your holder.cardView.setOnClickListener
is working.

ravi
- 899
- 8
- 31
0
Change the RecyclerViewAdapter.java on click method of cardView.
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mData.get(position).getTitle().equalsIgnoreCase("The Vegitarian")){
// move to another activity
}
else if(mData.get(position).getTitle().equalsIgnoreCase("The Wild Robot")){
// move to another activity
}
// continue ..
}
});

Harish Rawal
- 226
- 2
- 15
-
It is right but this is not a proper way , here you have know the details of book but you cant always get the details that you want to show – pravin maske May 21 '18 at 11:19
-
This way, one will have to create a thousand `else if`s if there are thousand books. And not to mention thousand Activities. Don't you think? – ravi May 21 '18 at 11:50
-
I know this , what are you trying to say but the question asked "I want to create separate activities for separate books". so he wants different activity. – Harish Rawal May 21 '18 at 11:56
-
Yeah,I want different activities.It means different pdf for different books via assets folder... – Mr.Mark May 21 '18 at 15:45
-
Just compare getIntent() in next activity not create multiple activity. – Harish Rawal May 21 '18 at 15:51
-
There is no need to create multiple activity just compare your getIntent value and open your assets according to them. – Harish Rawal May 21 '18 at 15:53