Я пытаюсь реализовать Onclick на моем RecyclerView
.Я попробовал много способов сделать это, но это приводит к сбою моего приложения.Я также хотел бы иметь диалоговое окно при нажатии на любой из cardView
.
Я использую FireBase в качестве моего бэкэнда, и я совершенно новичок в этом.
Вот мойКласс адаптера:
public class BlogRecyclerAdapter extends RecyclerView.Adapter<BlogRecyclerAdapter.ViewHolder> {
public List<BlogPost>blogList;
public Context context;
private FirebaseFirestore firebaseFirestore;
private static final int SECOND_MILLIS = 1000;
private static final int MINUTE_MILLIS = 60 * SECOND_MILLIS;
private static final int HOUR_MILLIS = 60 * MINUTE_MILLIS;
private static final int DAY_MILLIS = 24 * HOUR_MILLIS;
String dateString;
public BlogRecyclerAdapter(List<BlogPost>blogList){
this.blogList=blogList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.blog_list_item,parent,false);
context=parent.getContext();
firebaseFirestore=FirebaseFirestore.getInstance();
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
String descData=blogList.get(position).getDesc();
holder.setDescText(descData);
String imageUrl=blogList.get(position).getImage_url();
holder.setBlogImage(imageUrl);
final String userId=blogList.get(position).getUser_id();
firebaseFirestore.collection("Users").document(userId).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if(task.isSuccessful()){
String username=task.getResult().getString("Name");
holder.setUserData(username);
}
else{
}
}
});
long miliseconds=blogList.get(position).getTimestamp().getTime();
long now = System.currentTimeMillis();
final long diff = now - miliseconds;
if (diff < MINUTE_MILLIS) {
dateString= "just now";
} else if (diff < 2 * MINUTE_MILLIS) {
dateString= "a minute ago";
} else if (diff < 50 * MINUTE_MILLIS) {
dateString= diff / MINUTE_MILLIS + " minutes ago";
} else if (diff < 90 * MINUTE_MILLIS) {
dateString= "an hour ago";
} else if (diff < 24 * HOUR_MILLIS) {
dateString= diff / HOUR_MILLIS + " hours ago";
} else if (diff < 48 * HOUR_MILLIS) {
dateString= "yesterday";
} else {
dateString= diff / DAY_MILLIS + " days ago";
}
// String dateString= DateFormat.format("MM/dd/yyyy",new Date(miliseconds)).toString();
holder.setTime(dateString);
}
@Override
public int getItemCount() {
return blogList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private View mView;
private TextView descView;
private ImageView blogImageView;
private TextView blogDate;
private TextView blogUserName;
public ViewHolder(@NonNull View itemView) {
super(itemView);
mView=itemView;
}
public void setUserData(String name){
blogUserName=mView.findViewById(R.id.blogUserName);
blogUserName.setText(name);
}
public void setDescText(String descText){
descView=mView.findViewById(R.id.blogDesc);
descView.setText(descText);
}
public void setBlogImage(String downloaduri){
blogImageView=mView.findViewById(R.id.blogImage);
Glide.with(context).load(downloaduri).apply(RequestOptions.bitmapTransform(new BlurTransformation(30,3))).into(blogImageView);
}
public void setTime(String date){
blogDate=mView.findViewById(R.id.blogDate);
blogDate.setText(date);
}
}
}
Я испробовал много способов реализации, но безуспешно.