Reputation: 19
here are my recyclerview and viewholder classes
public class BookAdapter extends RecyclerView.Adapter<BookAdapter.MyViewHolder> {
private ArrayList<Book> bookList;
private OnItemClickListener itemClickListener;
public BookAdapter( ArrayList<Book> bookList, OnItemClickListener itemClickListener) {
this.bookList = bookList;
this.itemClickListener = itemClickListener;
}
@Override
public BookAdapter.MyViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_list_item,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder( BookAdapter.MyViewHolder holder, int position) {
Book book = bookList.get(position);
holder.book_imageview.setImageResource(book.getBookImageId());
holder.title_textview.setText(book.getBookTitle());
holder.author_textview.setText(book.getBookAuthor());
}
@Override
public int getItemCount() {
return bookList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
ImageView book_imageview;
TextView author_textview, title_textview;
public MyViewHolder(View itemView) {
super(itemView);
book_imageview = itemView.findViewById(R.id.book_imageview);
author_textview = itemView.findViewById(R.id.author_textview);
title_textview = itemView.findViewById(R.id.title_textview);
// if i clcik on title then in toast title should only be displayed
// if i click on auther then in toast auther should only be displayed
switch (getAbsoluteAdapterPosition()){
case R.id.title_textview:
title_textview.setOnClickListener(v ->itemClickListener.onClick(bookList.get(getLayoutPosition())));
break;
case R.id.author_textview:
author_textview.setOnClickListener(v ->itemClickListener.onClick(bookList.get(getLayoutPosition())));
}
}
}
}
i want to ask that how can I add click listner on each component of itemView like title_textview, author_textview etc as I cant be able to create switch statment in implemented method of my interface
HERE IS MY MAIN ACTIVITY WHERE IS THE MAIN ISSUE I THINK. I WANT TO USE SWITCH STATEMENT FOR GETTING INDIVIDUAL RESULT BY CLICKING THE COMPONENTS OF ROW-ITEM SEPARATELY
public class MainActivity extends AppCompatActivity implements OnItemClickListener{
private ArrayList<Book> bookList;
private RecyclerView rv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bookList = new ArrayList<>();
Resources res = getResources();
String[] allBooks = res.getStringArray(R.array.books);
String[] allAuthors = res.getStringArray(R.array.authors);
pupoluateBookList(allBooks, allAuthors);
BookAdapter adapter = new BookAdapter(bookList, this);
rv = findViewById(R.id.rv);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
rv.setLayoutManager(linearLayoutManager);
rv.setAdapter(adapter);
}
private void showToast(String message){
Toast.makeText(this,message,Toast.LENGTH_LONG).show();
}
public void pupoluateBookList(String[] books, String[] authors) {
bookList.add(new Book(books[0], authors[0], R.drawable.davinci_code));
bookList.add(new Book(books[1], authors[1], R.drawable.girl_train));
bookList.add(new Book(books[2], authors[2], R.drawable.harry_potter));
bookList.add(new Book(books[3], authors[3], R.drawable.hunder_games));
bookList.add(new Book(books[4], authors[4], R.drawable.lord_rings));
bookList.add(new Book(books[5], authors[5], R.drawable.moby_dick));
bookList.add(new Book(books[6], authors[6], R.drawable.mocking_bird));
bookList.add(new Book(books[7], authors[7], R.drawable.the_godfather));
}
@Override
public void onClick( Book book) {
// here I want to use switch statment for clicking individual components and taking result respectively
showToast(book.getBookTitle());
showToast(book.getBookAuthor());
}
}
Upvotes: 1
Views: 83
Reputation: 3307
You have the wrong logic.
You need to change the click listener implementation like this:
Put these 2 lines before the switch case.
title_textview.setOnClickListener(this);
author_textview.setOnClickListener(this);
implement View.OnClickListener
in your ViewHolder class then override onClick()
as well as change your switch case like this.
@Override
public void onClick(View view)
{
switch (view.getId()) {
case R.id.title_textview:
String bookTitle = bookList.get(getAdapterPosition()).getBookTitle();
showToast(bookTitle);
break;
}
case R.id.author_textview:
String bookAuthor = bookList.get(getAdapterPosition()).getBookAuthor();
showToast(bookAuthor);
break;
}
}
And yes, You should use getAdapterPosition()
if you are not dealing with layout changes because it's preferable to use getAdapterPosition()
most of the time for only data-related stuff.
Upvotes: 1