I am new into Android, I have a project to do and I am stuck with this. I am trying to make different items from my card view doing different things. At the moment I can click any item I want, but all of them will do the same thing. How can I make each Item do a each own thing. Here is my adapter :
package com.mecachrome.ffbf;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.squareup.picasso.Picasso;
import java.util.List;
public class reviewsAdapter extends RecyclerView.Adapter<reviewsAdapter.ViewHolder> {
private Context context;
private List<reviews> review;
private reviewsAdapter.ItemClickListener mItemListener;
public reviewsAdapter(Context context, List<reviews> review,reviewsAdapter.ItemClickListener itemClickListener) {
this.context = context;
this.review = review;
this.mItemListener = itemClickListener;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.reviews_card_layout,parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
reviews currentReview = review.get(position);
holder.rev_username.setText(currentReview.getUsername());
holder.rev_text.setText(currentReview.getText());
holder.rev_user_rating.setRating(currentReview.getUserrating());
Picasso.get().load(currentReview.getProfilepicUrl()).fit().centerCrop().into(holder.rev_user_pic);
holder.rev_like_nr.setText(String.valueOf(currentReview.getLikes()));
holder.rev_dislike_nr.setText(String.valueOf(currentReview.getDislikes()));
holder.rev_like_btn.setOnClickListener(view ->{
mItemListener.onItemClick(review.get(position));
});
}
@Override
public int getItemCount() {
return review.size();
}
public interface ItemClickListener{
void onItemClick(reviews place);
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView rev_username;
public TextView rev_text;
public RatingBar rev_user_rating;
public ImageView rev_user_pic;
public ImageView rev_like_btn;
public ImageView rev_dislike_btn;
public ImageView rev_delete_btn;
public TextView rev_like_nr;
public TextView rev_dislike_nr;
public ViewHolder(@NonNull View itemView) {
super(itemView);
rev_username = itemView.findViewById(R.id.rcv_username);
rev_text = itemView.findViewById(R.id.rcv_user_review);
rev_user_rating = itemView.findViewById(R.id.rcv_user_rating);
rev_user_pic = itemView.findViewById(R.id.rcv_user_profile_img);
rev_like_btn = itemView.findViewById(R.id.iv_like);
rev_dislike_btn = itemView.findViewById(R.id.iv_dislikes);
rev_delete_btn = itemView.findViewById(R.id.rcv_delete_btn);
rev_like_nr = itemView.findViewById(R.id.rcv_likes);
rev_dislike_nr = itemView.findViewById(R.id.rcv_dislikes);
}
@Override
public void onClick(View v) {
}
}
}
And here is the activity:
package com.mecachrome.ffbf;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
public class Restaurant_Reviews extends AppCompatActivity {
Button add_review;
TextView restaurant_name;
ImageView rev_like_btn;
RecyclerView rv_reviews;
reviewsAdapter myadapter;
private places place;
private ArrayList<reviews> reviewsArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_restaurant_reviews);
place = getIntent().getParcelableExtra("restaurant");
DatabaseReference databaseref = FirebaseDatabase.getInstance().getReference("restaurants").child(place.getId()).child("reviews");
restaurant_name = findViewById(R.id.tv_add_restaurant_name);
restaurant_name.setText(place.getName() " Reviews");
rev_like_btn = findViewById(R.id.iv_like);
reviewsArrayList = new ArrayList<>();
databaseref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot dss: snapshot.getChildren()){
reviews rev = dss.getValue(reviews.class);
reviewsArrayList.add(rev);
}
myadapter = new reviewsAdapter(Restaurant_Reviews.this, reviewsArrayList, place -> {
Toast.makeText(Restaurant_Reviews.this, "Clicked", Toast.LENGTH_SHORT).show();
});
rv_reviews.setAdapter(myadapter);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(Restaurant_Reviews.this, error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
add_review = findViewById(R.id.btn_add_review);
add_review.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Restaurant_Reviews.this, add_review.class);
intent.putExtra("restaurant",place);
startActivity(intent);
}
});
rv_reviews = findViewById(R.id.rv_restaurant_reviews);
rv_reviews.setHasFixedSize(true);
rv_reviews.setLayoutManager(new LinearLayoutManager(this));
}
}
CodePudding user response:
public interface ItemClick{
void onItemClick(View view,int position,Reviews place);
}
onBindViewHolder(){
holder.rev_like_btn.setOnClickListener(view ->{
mItemListener.onItemClick(view,position,review.get(position));
});
}
public setItemClick(ItemClick itemClick){
this.itemClick = itemClick
}
adapter.setItemClick(new ItemClick{
@Override
void onItemClick(view,position,place){//TODO()}
})
edit
onBindViewHolder(){
holder.rev_like_btn.setOnClickListener(view ->{
mItemListener.onItemClick(view,position,review.get(position));
});
holder.rev_delete_btn.setOnClickListener(view ->{
mItemListener.onItemClick(view,position,review.get(position));
});
holder.rev_like_nr.setOnClickListener(view ->{
mItemListener.onItemClick(view,position,review.get(position));
});
holder.rev_dislike_nr.setOnClickListener(view ->{
mItemListener.onItemClick(view,position,review.get(position));
});
}
adapter.setItemClick(new ItemClick{
@Override
void onItemClick(view,position,place){
ViewHolder holder =
recyclerView.findViewHolderByAdapterPosition(position);
}
})
CodePudding user response:
Just add holder.itemView setOnClickListener(view ->{ //TODO })
on onBindViewHolder
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
reviews currentReview = review.get(position);
holder.rev_username.setText(currentReview.getUsername());
holder.rev_text.setText(currentReview.getText());
holder.rev_user_rating.setRating(currentReview.getUserrating());
Picasso.get().load(currentReview.getProfilepicUrl()).fit().centerCrop().into(holder.rev_user_pic);
holder.rev_like_nr.setText(String.valueOf(currentReview.getLikes()));
holder.rev_dislike_nr.setText(String.valueOf(currentReview.getDislikes()));
holder.itemView.setOnClickListener(view ->{
mItemListener.onItemClick(review.get(position));
});
}