如何将数据从一个非 activity class 发送到另一个 android

how to send data from one non activity class to another android

在我的 android 应用程序中,我在另一个 Adatper 中使用 Adapter。假设 child 和 parent Adapter。在 parent Adapter 中,我在项目上使用了 onclick 方法并实例化了 child Adapter。现在 onClick 在 child 的视图上 Adapter 我想发送信号给 parent Adapter 和 运行 一个函数..
这是我的代码:

// This is in parent layout

        holder.liker.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                holder.reaction_layout.setVisibility(View.VISIBLE);
                if(holder.cursor.getCount()>0)
                {
                    holder.show_reaction=true;
                    holder.tinyDB.putString("post_id",String.valueOf(holder.username.getTag()));
                    holder.tinyDB.putString("act_id",String.valueOf(holder.post_comment.getTag()));
                    while(holder.cursor.moveToNext())
                    {

                        holder.imageModelArrayList.add(new ReactionModel(holder.cursor.getString(1),holder.cursor.getString(3),holder.cursor.getString(2)));
                        holder.recyclerView.setAdapter(holder.adapter);  //Here i instantiate the child adapter
                    }
                }

                return true;
            }
        });

现在这是child的点击功能 Adapter:

holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tinyDB.putString("image",imageModelArrayList.get(position).getImage_drawable());
                String react_id=String.valueOf(holder.title.getTag());
                String act_id=tinyDB.getString("act_id");
                String post_id=tinyDB.getString("post_id");
                NetworkController.postLikePojoClassCall(base64,Integer.parseInt(act_id),Integer.parseInt(react_id),Integer.parseInt(post_id)).enqueue(new retrofit2.Callback<PostLikePojoClass>() {
                    @Override
                    public void onResponse(Call<PostLikePojoClass> call, retrofit2.Response<PostLikePojoClass> response) {
                        if(response.isSuccessful()) {
                            System.out.println("response__  " + response.body().getSuccess());


                            }else{
                            System.out.println("response__  " + response.errorBody());
                            }
                        }


                    @Override
                    public void onFailure(Call<PostLikePojoClass> call, Throwable t) {
                        System.out.println("failure__  " + t.getMessage());

                    }
                });

            }
        });

在此点击中,我想向 parent Adapter 发送信号,该项目已被点击。

使用回调方法触发你的父适配器

创建接口

public interface MyInterface{
    void click();
}

让父适配器实现接口

class YourParentAdapter extends RecyclerView.Adapter<YourViewHolder> implements MyInterface

通过构造函数将父适配器传递给子适配器

public ChildAdapter(MyInterface myInterface){
    this.myInterface = interface;
}

现在您可以在子适配器中使用父适配器的功能:

myInterface.click();