【问题标题】:Dialog message buttons dont do what is inside the code?对话框消息按钮不执行代码中的内容?
【发布时间】:2016-10-19 16:40:27
【问题描述】:

我有一种方法可以从数据库中删除某些内容。我想得到用户的确认。为了做到这一点,我实现了一个布尔函数来通过对话框获得确认。

好吧,我的问题是,无论我选择是或否,我总是得到相同的错误结果。

(我使用了 final boolean[],否则会出错)

这是方法:

public boolean alertMessage() { //Confirmation to delete routine
    final boolean[] confirmation = {false};
    DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
                case DialogInterface.BUTTON_POSITIVE:
                    // Yes button clicked
                    confirmation[0] =true;
                    break;
                case DialogInterface.BUTTON_NEGATIVE:
                    // No button clicked
                    // do nothing
                    confirmation[0]=false;
                    break;
            }
        }
    };

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(R.string.sure_delete)
            .setPositiveButton(R.string.yes, dialogClickListener)
            .setNegativeButton(R.string.no, dialogClickListener).show();
    return confirmation[0];
}

这就是我在删除代码中检查它的方式:

//CONFIRMATION
if(!alertMessage()){
 return;
}

更新: 用一个答案建议试试这个,但还是一样:

public boolean alertMessage() { //Confirmation to delete routine
    final boolean[] confirmation = {false};
    new AlertDialog.Builder(this)
            .setTitle(R.string.delete)
            .setMessage(R.string.sure_delete)
            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    confirmation[0]=true;
                }
            })
            .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    confirmation[0]=false;
                }
            })
            .setIcon(android.R.drawable.ic_dialog_alert)
            .show();
    return confirmation[0];
}

我把 final boolean [] 因为只有一个布尔值我得到一个错误:

"Variable is accessed from within an inner class, needs to be declared final."

一旦我宣布它是最终的:

"Can not assign a value to final variable"

所以我必须把它转换成一个数组。 我希望它是一个布尔方法,不要在“是”内实现删除,因为我想在其他方法中重用它。

【问题讨论】:

  • 愚蠢的问题但是:你为什么使用布尔数组?
  • 试试这个。我建议您使用以下链接中的方法并在正面按钮实现中执行删除。 stackoverflow.com/questions/2115758/…
  • @FrédéricLetellier:我怀疑这是因为如果只使用final boolean 那么 onClick 将无法修改值,而数组中的值可以。
  • @vidulaJ 同意,我这样编码
  • 您的方法不会等待您的对话框被关闭。所以在 onclick 监听器中做你的事情。

标签: android button dialog confirmation


【解决方案1】:

这是因为当您显示对话框时您的代码不会停止运行,因此您的方法将始终返回 false,因为它是分配给它的默认值。询问删除确认的最佳方式是在对话框的肯定按钮内调用删除方法。

假设您有一个列表视图,并且您想在单击某个项目时将其删除。以下是你应该怎么做。

public class MyActivity extends AppCompatActivity {


    public ListViewCompat listView;
    private List<Object> myObjects;

    public void onCreate(Bundle savedinstance) {
        super.onCreate(savedinstance);
        setContentView(R.layout.activity_my_list);

        listView = (ListViewCompat) findViewById(R.id.my_list_view);
        //Set a listview adapter

        listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
                showConfirmationDialog(position);         
            }
        });
    }

    private void showConfirmationDialog(int itemPosition) {
        AlerDialog.Builder builder = new AlerDialog.Builder(this);

        builder.setTitle("Confirmation");
        builder.setMessage("Delete item?")
        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            deleteItem(itemPosition);
        });
        builder.setNegativeButton("No", null);

        builder.create().show();
    }

    private void deleteItem(int itemPosition) {
        //Delete item
        myObjects.remove(itemPosition);
    }
}

【讨论】:

  • 如果我想重用不起作用的对话框,这与 Frederic 回答中的相同。
  • 我认为在这种情况下这不是问题,因为对话框将在方法内部创建,并且无法在另一个方法中调用它
【解决方案2】:

尝试使用接口:

public class MainActivity extends AppCompatActivity implements DialogInterface.OnClickListener {

    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e(TAG, "onCreate");
        setContentView(R.layout.activity_main);

        new AlertDialog.Builder(this)
                .setTitle("Hello")
                .setMessage("World")
                .setPositiveButton(android.R.string.yes, this)
                .setNegativeButton(android.R.string.no, this)
        .show();

    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch(which) {
            case DialogInterface.BUTTON_POSITIVE:
                Log.d(TAG, "Ok");
                break;
            case DialogInterface.BUTTON_NEGATIVE:
                Log.d(TAG, "Cancel");
                break;
        }

    }
}

【讨论】:

    【解决方案3】:

    如你所愿,对同一列表的不同元素进行不同操作的相同对话框:

    public final static TAG_UPDATE = "update";
    public final static TAG_DELETE = "delete";
    
    public void alertMessage(String id, String actionTag) { //the same call for all elements of a list
    final String mIdElement = idElement;
    final String mActionTag = actionTag;
    
    new AlertDialog.Builder(this)
            .setTitle(R.string.confirmation)
            .setMessage(R.string.sure_confirmation)
            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    makeSomething(mIdElement, mActionTag);
                }
            })
            .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    //no-op
                }
            })
            .setIcon(android.R.drawable.ic_dialog_alert)
            .show();
    }
    
    public void makeSomething(String idElement, String actionTag){
        switch(actionTag) {
            case TAG_UPDATE :
                // your update code to update the id element
                break;
            case TAG_DELETE :
                // your delete code to delete the id element
                break;
        } 
    }
    

    【讨论】:

    • 这可行,但如果我想将确认对话框用于其他目的,我将无法做到。
    • 如果你想删除列表中的一个元素,所有元素都是同一个对话框。使用参数调用方法(例如:元素的 id)并将这些参数传递给 delete 方法。如果你想对不同的元素使用相同的对话框,也许你的方法很糟糕..
    • 我的意思是,如果我想将相同的对话框用于“更新”、“删除”或“添加”等不同的操作,例如私有布尔 showConfirmationDialog(String message)。这种方式我做不到。
    猜你喜欢
    • 1970-01-01
    • 2013-12-26
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多