如何从另一个通知适配器? NotifyItemChanged无法正常运行

问题描述

我有以下流程:带注释的时间轴(RecyclerViewCommentAdapter)->每个注释都包含一个贴纸列表(RecyclerViewgalleryAdapter)->长按注释文本时,将弹出一个对话框,用户可以在其中选择要发送的贴纸评论。之后,我想更新评论,以显示更新的贴纸数量...

我试图一直发送recyclerview对象和注释位置,并在我的RecyclerViewgalleryAdapter上发送“ notifyItemChanged”,这是行不通的,有没有更好的方法呢?

这是我的回收站视图,其中显示了我的时间轴上显示评论

public class RecyclerViewCommentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private final Context context;
private ArrayList<Object> elements;
private AppCompatActivity activity;
private static final int COMMENT_ITEM_VIEW_TYPE = 0;
int mPostsPerPage = 10;

RecyclerViewCommentAdapter(@NonNull Context context,AppCompatActivity activity) {
    this.context = context;
    this.elements = new ArrayList<>();
    this.activity = activity;
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup,int viewType) {
    View menuItemLayoutView = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.item_comment,viewGroup,false);
    return new CommentViewHolder(menuItemLayoutView);
}


@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder1,int position) {
    holder1.setIsRecyclable(false);

    CommentViewHolder holder = (CommentViewHolder) holder1;
    final Comment comment = (Comment) elements.get(position);

    GridLayoutManager layoutManager = new GridLayoutManager(context,5);
    Util.mDatabaseRef.child("product").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            List<Product> finalElements = new ArrayList<>();
            for (DataSnapshot snap : dataSnapshot.getChildren()) {
                finalElements.add(snap.getValue(Product.class));
            }
            if (comment.getStickers() != null && comment.getStickers().size() > 0) {
                RecyclerViewStickerAdapter adapter = new RecyclerViewStickerAdapter(new ArrayList<>(comment.getStickers().values()),context,comment);
                DividerItemdecoration dividerItemdecoration = new DividerItemdecoration(context,layoutManager.getorientation());
                holder.stickers.addItemdecoration(dividerItemdecoration);
                holder.stickers.setLayoutManager(layoutManager);
                holder.stickers.setAdapter(adapter);
                holder.stickersLayout.setVisibility(View.VISIBLE);
                holder.stickers.setVisibility(View.VISIBLE);
            } else {
                holder.stickersLayout.setVisibility(View.GONE);
                holder.stickers.setVisibility(View.GONE);
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

    holder.text.setText(comment.getText());
    holder.text.setTrimExpandedText(" Ver menos");
    holder.text.setTrimCollapsedText(" Ver mais");
    holder.text.setTrimLines(4);
    holder.text.setColorClickableText(Color.BLUE);

    holder.text.setonLongClickListener(v -> {
        if (elements.get(position) instanceof Comment) {
            stickersDialog(position);
        }
        return true;
    });
}

private void stickersDialog(int position) {
    Comment comment = (Comment) elements.get(position);
    if (comment != null && !comment.getAuthorsUID().equals(Util.getUser().getUserUID())) {
        List<Product> products = new ArrayList<>();
        if (Util.getUser().getProducts() != null) {
            for (Product product : Util.getUser().getProducts().values()) {
                if (product != null && product.getQuantity() > 0) {
                    products.add(product);
                }
            }
        }
        DialogStickers cdd = new DialogStickers(activity,products,null,false,(Comment) elements.get(position),this,position);
        cdd.show();
    } else {
        Toast.makeText(context,"Você não pode enviar figurinhas para seu próprio comentário.",Toast.LENGTH_SHORT).show();
    }
}

@Override
public int getItemViewType(int position) {
    return COMMENT_ITEM_VIEW_TYPE;
}

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

private static class CommentViewHolder extends RecyclerView.ViewHolder {

    private ReadMoreTextView text;
    private RecyclerView stickers;
    private LinearLayout stickersLayout;

    CommentViewHolder(View rowView) {
        super(rowView);
        text = rowView.findViewById(R.id.text);
        stickers = rowView.findViewById(R.id.stickers);
        stickersLayout = rowView.findViewById(R.id.stickersLayout);
    }
}
}

对话框:

public class DialogStickers extends Dialog {

private Activity a;
public Dialog d;
private List<Product> productList;
private List<Argument> argumentList;
private boolean isForChat;
private Comment comment;
private RecyclerViewCommentAdapter recyclerViewCommentAdapter;
private int position;

DialogStickers(Activity a,List<Product> productList,List<Argument> argumentList,boolean isForChat,Comment comment,RecyclerViewCommentAdapter recyclerViewCommentAdapter,Integer position) {
    super(a);
    this.a = a;
    this.productList = productList;
    this.argumentList = argumentList;
    this.isForChat = isForChat;
    this.comment = comment;
    this.recyclerViewCommentAdapter = recyclerViewCommentAdapter;
    this.position = position;
    this.d = this;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestwindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.dialog_stickers);

    RecyclerView sticker = findViewById(R.id.mystickers);
    TextView empty = findViewById(R.id.empty);

    if (productList == null || productList.isEmpty()) {
        empty.setVisibility(View.VISIBLE);
    } else {
        empty.setVisibility(View.GONE);
    }

    GridLayoutManager layoutManager = new GridLayoutManager(getContext(),3);
    Util.mSubjectDatabaseRef.child(comment.getSubject()).child("servers").child(comment.getServerUID()).child("timeline").child("commentList").child(comment.getCommentUID()).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            Comment comment = snapshot.getValue(Comment.class);
            if (comment != null) {
                comment.setCommentUID(snapshot.getKey());
                RecyclerViewgalleryAdapter adapter = new RecyclerViewgalleryAdapter(Util.getUser().getProducts(),productList,getContext(),true,argumentList,d,comment,recyclerViewCommentAdapter,position,a);
                DividerItemdecoration dividerItemdecoration = new DividerItemdecoration(getContext(),layoutManager.getorientation());
                sticker.addItemdecoration(dividerItemdecoration);
                sticker.setLayoutManager(layoutManager);
                sticker.setAdapter(adapter);
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });
}
}

RecyclerViewgalleryAdapter:

public class RecyclerViewgalleryAdapter extends RecyclerView.Adapter<RecyclerViewgalleryAdapter.ViewHolder> {

private List<Product> allStickers;
private HashMap<String,Product> mystickers;
private StorageReference storageReference;
private Context context;
private List<Argument> argumentList;
private Dialog dialog;
private boolean isForDialog;
private boolean isForgallery;
private Comment comment;
private Activity activity;
private RecyclerViewCommentAdapter recyclerViewCommentAdapter;
private Integer commentPosition;

public RecyclerViewgalleryAdapter(HashMap<String,Product> mystickers,List<Product> allStickers,Context context,boolean isForgallery,boolean isForDialog,boolean isForComment,Dialog dialog,int commentPosition,Activity activity) {
    this.allStickers = allStickers;
    this.mystickers = mystickers;
    this.context = context;
    this.isForDialog = isForDialog;
    this.argumentList = argumentList;
    this.dialog = dialog;
    this.isForgallery = isForgallery;
    this.comment = comment;
    this.recyclerViewCommentAdapter = recyclerViewCommentAdapter;
    storageReference = FirebaseStorage.getInstance().getReference();
    this.commentPosition = commentPosition;
    this.activity = activity;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType) {
    View rowView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_sticker,parent,false);
    return new ViewHolder(rowView);
}

@Override
public void onBindViewHolder(@NonNull final RecyclerViewgalleryAdapter.ViewHolder holder,int position) {
        if (mystickers != null && mystickers.size() > 0) {
            List<Product> products = new ArrayList<>(mystickers.values());
            Product product = products.get(position);
            if (isForDialog && product.getQuantity() > 0) {
                storageReference.child("images/" + product.getItemSKU() + ".png").getDownloadUrl().addOnSuccessListener(uri -> Glide.with(context).load(uri).into(holder.image));
                holder.quantity.setText("(" + product.getQuantity() + ")");
            }
            holder.image.setonClickListener(v -> sendSticker(allStickers.get(position).getItemSKU(),products.get(position).getProductUID()));
    }
}

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

static class ViewHolder extends RecyclerView.ViewHolder {

    private ImageView image;
    private TextView title;
    private TextView quantity;

    ViewHolder(View rowView) {
        super(rowView);
        image = rowView.findViewById(R.id.image);
        title = rowView.findViewById(R.id.title);
        quantity = rowView.findViewById(R.id.quantity);
    }
}

private void sendSticker(String sku,String id) {

    if (isForDialog && comment == null) {
        Date data = new Date();

        Calendar cal = Calendar.getInstance();
        cal.setTime(data);
        Date current_date = cal.getTime();

        Sending sending = new Sending("text",current_date.getTime(),Util.getUser().getUserName(),Util.getUser().getUserUID(),Util.getSubject());
        Argument argument = new Argument(null,sku,Util.getGroup().getGroupUID(),sending);
        Util.mSubjectDatabaseRef.child(Util.getServer().getSubject()).child("servers").child(Util.getServer().getServerUID()).child("timeline")
                .child("commentList").child(Util.getGroup().getCommentUID())
                .child("group").child("argumentList").push().setValue(argument);
        Util.getUser().getProducts().get(id).setQuantity(Util.getUser().getProducts().get(id).getQuantity() - 1);
        Util.mUserDatabaseRef.child(Util.getUser().getUserUID()).child("products").child(id).child("quantity").setValue(Util.getUser().getProducts().get(id).getQuantity());
    }

    if (Util.getUser().getProducts().get(id) != null) {
        Product userProduct = Util.getUser().getProducts().get(id);
        if (isForDialog && comment != null) {
            Util.mSubjectDatabaseRef.child(Util.getServer().getSubject()).child("servers").child(Util.getServer().getServerUID()).child("timeline").child("commentList").child(comment.getCommentUID()).child("stickers").child(id).addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    Util.mSubjectDatabaseRef.child(Util.getServer().getSubject()).child("servers").child(Util.getServer().getServerUID()).child("timeline").child("commentList").child(comment.getCommentUID()).child("stickers").child(id).removeEventListener(this);
                    Product commentProduct = dataSnapshot.getValue(Product.class);
                    if (comment.getStickers() == null) {
                        comment.setStickers(new HashMap<>());
                    }
                    int quantity = Util.getUser().getProducts().get(userProduct.getProductUID()).getQuantity() - 1;
                    if (commentProduct != null) {
                        if (comment.getStickers().get(id) != null) {
                            comment.getStickers().get(id).setQuantity(comment.getStickers().get(id).getQuantity() + 1);
                        } else {
                            commentProduct.setQuantity(1);
                            comment.getStickers().put(id,commentProduct);
                        }
                        Util.mSubjectDatabaseRef.child(Util.getServer().getSubject()).child("servers").child(Util.getServer().getServerUID()).child("timeline").child("commentList").child(comment.getCommentUID()).child("stickers").child(id).setValue(comment.getStickers().get(id));
                    } else {
                        userProduct.setQuantity(1);
                        Util.mSubjectDatabaseRef.child(Util.getServer().getSubject()).child("servers").child(Util.getServer().getServerUID()).child("timeline").child("commentList").child(comment.getCommentUID()).child("stickers").child(id).setValue(userProduct);
                    }
                    Util.getUser().getProducts().get(userProduct.getProductUID()).setQuantity(quantity);
                    Util.mUserDatabaseRef.child(Util.getUser().getUserUID()).child("products").child(userProduct.getProductUID()).child("quantity").setValue(quantity);
                    if (recyclerViewCommentAdapter != null && commentPosition != null) {
                        recyclerViewCommentAdapter.notifyItemChanged(commentPosition);
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });
        }
    }
    if (dialog != null) {
        dialog.dismiss();
    }
}
}

我不知道这是否是最好的方法,我接受任何其他工作方式,我想要的是将此标签发送给评论,并立即对其进行更新,以便用户可以看到它的作用。

我确定它已发送到数据库,如果我离开并返回活动,它将显示更新的值。

TimeLineActivity-> RecyclerViewCommentAdapter(显示注释)-> DialogStickers(选择要发送的标签)-> RecyclerViewgalleryAdapter(显示每个可用标签

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...