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

如何解决如何从另一个通知适配器? 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 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-