在Android中的硬件按下的后退按钮上关闭自定义警报对话框

时间:2013-08-07 11:12:51

标签: android android-alertdialog back-button

我在硬件的后退按钮上遇到问题。在我的主Activity中,我有一个List视图(比如1)。当我单击此列表视图(1)的项目时,会出现一个警报对话框,在此警报对话框中,有一个列表视图(例如2)。当我按下硬件的后退按钮时,正在重复该列表视图(2)的数据。我还在此警报对话框中放置取消图像以解除,当我按下取消图像时,数据不会重复。我尝试了onResume(),onPause(),onDestroy(),onRestart()的不同方法来清除List视图(2)的数组,但没有任何作用。这是我的代码......

case LIST_DIALOG :
LayoutInflater inflater2 = LayoutInflater.from(this);
View dialogview1 = inflater2.inflate(R.layout.listdialog, null);
AlertDialog.Builder dialogbuilder2 = new AlertDialog.Builder(this);
dialogbuilder2.setView(dialogview1);
dialogDetails = dialogbuilder2.create();


case LIST_DIALOG:

    AlertDialog alertDialog1 = (AlertDialog) dialog;

    // Cancel Alert Dialog
    ImageView ivCancel = (ImageView) alertDialog1.findViewById(R.id.imgCancel);
    ivCancel.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            dismissDialog(LIST_DIALOG); 
            arr2.clear();
        }
    });

    // Friend List
    showFriendList();
    break;

//使用此函数调用插入列表视图数据     private void showFriendList(){             // TODO自动生成的方法存根             Request.executeMyFriendsRequestAsync(friendSession,new GraphUserListCallback(){

            @Override
            public void onCompleted(List<GraphUser> users, Response response) {
                // TODO Auto-generated method stub

            //  arr2 = new ArrayList<String>();
                for(GraphUser user : users)
                {
                    arr2.add(user.getName());
                }

                adapter2 = new ArrayAdapter<String>(getBaseContext(), R.layout.single_row, R.id.txt,arr2);
                lvDialog.setAdapter(adapter2);
                lvDialog.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
                adapter2.notifyDataSetChanged();

                itemCount = lvDialog.getCount();
                Toast.makeText(getBaseContext(), "" + itemCount, 1000).show();

            }
        });


        }

//我尝试了这些方法,但没有任何作用......

@Override
      public void onResume()
      {
          super.onResume();
          ShowSavedFiles();
          arr2.clear();

      }

    @Override
    public void onPause()
    {
        super.onPause();
        arr1.clear();
        arr2.clear();
    }


    @Override
    public void onBackPressed() {
    //super.onBackPressed();
    // finish your Activity

    arr2.clear();

    return;
    }

  @Override
   public boolean onKeyDown(int keyCode, KeyEvent event) {
   if (keyCode == KeyEvent.KEYCODE_BACK) {
        arr2.clear();
        dismissDialog(LIST_DIALOG); 

    }
    return false;
}

1 个答案:

答案 0 :(得分:0)

我不确定但是可能会将以下代码添加到您的onKeyDown方法中可能有所帮助:

return super.onKeyDown(keyCode, event);