如何使用AlertDialog从listView中删除项目?

时间:2015-01-18 20:37:08

标签: android arraylist android-alertdialog

大家好我正在尝试创建一个带有2个按钮的警告对话框。一个返回主屏幕,另一个从列表视图中删除一个项目并返回主屏幕。我尝试了一些方法,但似乎都没有。有人可以帮忙吗?这是我的代码。

@Override public void onItemClick(AdapterView<?> parent,
                                  View view, int position, long id) {

    AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle("Hello My Name is");
    alertDialog.setMessage("" + mNameList.get(position));
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
        }
    });
    alertDialog.setButton2("Remove", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {

            Toast.makeText(getApplicationContext(), "Removed from list", Toast.LENGTH_SHORT).show();
        }
    });

    alertDialog.show();
}

任何帮助将不胜感激。提前谢谢!

1 个答案:

答案 0 :(得分:0)

你可以这样做

alertDialog.setButton2("Remove", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        mNameList.remove(position);//or equalalent of remove method
        yourAdapter.notifyDataSetChange();
        Toast.makeText(getApplicationContext(), "Removed from list", Toast.LENGTH_SHORT).show();

    }
});