Undo the changes in listview onbackpressed

两盒软妹~` 提交于 2019-12-20 07:28:05

问题


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.

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.

But it seems not working.

Please provide any solution for the same.


回答1:


There a simple way to achieve this

let's say you have a Product class

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

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

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



来源:https://stackoverflow.com/questions/39995201/undo-the-changes-in-listview-onbackpressed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!