在按下后撤消列表视图中的更改 [英] Undo the changes in listview onbackpressed

查看:78
本文介绍了在按下后撤消列表视图中的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含产品列表的列表视图,单击列表项后,打开列表详细信息.列表项包含诸如毛重,净重等值.

I have a list view which contains list of products and on the click of the list item, list details gets open. The list item contains values like gross weight, net weight etc.

在列表项详细信息页面的顶部,我有一个十字按钮而不是后退按钮,单击该按钮将打开一个对话框,显示您是否要保存更改",并使用是或否按钮.

On the top of the list item details page I have a cross button instead of back button which on click will open a dialog saying "Do you want to save the changes" and with the button yes or no.

按YES(是)时,我可以将更改保存在列表视图中,但是按NO(否)时,则不能撤消更改.

On pressing YES, I am able to save the changes in the list view but on pressing NO, am not able to undo the changes.

下面是显示对话框的代码:

Below is the code for showing the dialog:

@Override
    public boolean onBackPressed() {

        final AlertDialog alert = new AlertDialog.Builder(getContext()).create();
        alert.setMessage(getContext().getString(R.string.save_activity));
        alert.setCancelable(false);
        alert.setButton(getContext().getString(R.string.strYes), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                    saveAndClose();
            }
        });
        alert.setButton2(getContext().getString(R.string.strNo), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                alert.dismiss();
                ((Activity)getContext()).finish();
            }
        });
        alert.show();
        return true;

    }

同样,我尝试创建具有不同引用的数组列表并尝试设置值.

For the same, I have tried to create a array list with different reference and tried to set the values.

但它似乎不起作用.

请为此提供任何解决方案.

Please provide any solution for the same.

推荐答案

有一种简单的方法可以实现

There a simple way to achieve this

假设您有一个Product类

let's say you have a Product class

public class Product{
    private boolean currentState;
    private boolean previosState;
}

当用户单击十字按钮但不触摸 previousState 时,

保持 currentState .

Maintain the currentState when user clicks on the cross button but do not touch the previousState.

现在按是或否选择是,请按是,请考虑 currentState ,当按否时,请考虑 previosState .

Now on the yes or no selection when press Yes consider the currentState and when press No consider the previosState.

这篇关于在按下后撤消列表视图中的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆