Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 更新recyclerview列表时出错?_Android_Android Recyclerview_Android Alertdialog - Fatal编程技术网

Android 更新recyclerview列表时出错?

Android 更新recyclerview列表时出错?,android,android-recyclerview,android-alertdialog,Android,Android Recyclerview,Android Alertdialog,我通过启动对话框的适配器调用接口函数。 我想根据用户与对话框的交互方式(即他是否按了“正”按钮或“负”按钮)更新“回收器”视图列表 我面临的问题是: 我尝试过的方法是,每当用户按下肯定或否定按钮时,更新全局变量,并基于此结果执行操作但我面临的问题是,即使在用户按下“确定/取消”按钮之前,函数也会返回全局值。 我从何处调用函数的采纳者: holder.actionTask.setOnTouchListener(new View.OnTouchListener() {

我通过启动对话框的适配器调用接口函数。 我想根据用户与对话框的交互方式(即他是否按了“正”按钮或“负”按钮)更新“回收器”视图列表

我面临的问题是:

我尝试过的方法是,每当用户按下肯定或否定按钮时,更新全局变量,并基于此结果执行操作但我面临的问题是,即使在用户按下“确定/取消”按钮之前,函数也会返回全局值。

我从何处调用函数的采纳者:

holder.actionTask.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {

                    Context mainActivityContext= Constants.getContext();

                    if (action_id.equals("pain"))
                    {

                        if (mainActivityContext!=null && mainActivityContext instanceof MainActivity)
                        {
                            interfaceAdapter=((HealthVitalsFunction) mainActivityContext);
                           interfaceAdapter.openPainRecordDialog(context,dbHelper);

                            Toast.makeText(context,"Pain "+Boolean.toString(Constant.taskdone), Toast.LENGTH_SHORT).show();
                        }

                     }
        }
}
@Override
    public boolean openPainRecordDialog(final Context context, final DbHelper dbHelper) {


        alertDialogBuilder
                .setCancelable(false)
                .setPositiveButton("ADD", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

            **Constants.taskDone=true;**
                })
                .setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
            Constants.taskDone=false;
                        dialog.cancel();
                    }
                });

        AlertDialog alertDialog = alertDialogBuilder.create();

        alertDialog.show();

       return Constants.taskDone;

    }
OpenRecordDialog函数:

holder.actionTask.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {

                    Context mainActivityContext= Constants.getContext();

                    if (action_id.equals("pain"))
                    {

                        if (mainActivityContext!=null && mainActivityContext instanceof MainActivity)
                        {
                            interfaceAdapter=((HealthVitalsFunction) mainActivityContext);
                           interfaceAdapter.openPainRecordDialog(context,dbHelper);

                            Toast.makeText(context,"Pain "+Boolean.toString(Constant.taskdone), Toast.LENGTH_SHORT).show();
                        }

                     }
        }
}
@Override
    public boolean openPainRecordDialog(final Context context, final DbHelper dbHelper) {


        alertDialogBuilder
                .setCancelable(false)
                .setPositiveButton("ADD", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

            **Constants.taskDone=true;**
                })
                .setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
            Constants.taskDone=false;
                        dialog.cancel();
                    }
                });

        AlertDialog alertDialog = alertDialogBuilder.create();

        alertDialog.show();

       return Constants.taskDone;

    }
全局变量:

public class Constants {
          public static boolean taskDone;
}

布尔值的默认值为false 所以,即使不赋值,它也返回false

您可以使用false来取消,只需更新列表即可

在适配器类内创建方法

public void update(ArrayList<Model> modelList){
    adapterModelList.clear(); 
    for (Product model: modelList) {
        adapterModelList.add(model); 
    }
    notifyDataSetChanged();
}

你能分享一些代码吗?我已经添加了代码@surajraow你在Apeter中使用了哪种全局变量?告诉我们code@sushantgosavi我已经更新了代码。我应该在哪里调用更新方法?在适配器中?意味着如何。my adapter和recyclerview是在调用适配器活动中定义的。如果您想从适配器内部调用它,而不是像update(modelList)那样调用它,如果您想从创建adpter实例的活动或片段调用它,而不是像((MyRecyclerAdapter)recyclerview.getAdapter())那样调用它。update(modelList);无论列表在哪里更新,我都会调用更新(modelList)。在适配器内部是否有其他替代方法来调用该函数。第二点,只有在滚动recyclerview时才调用此函数。我通过调用活动调用了此函数,但它不起作用。