0

ログインすると、名前などのリストが表示されるこのアプリケーションがあります。追加、更新、および削除ボタンもあります。編集ボタンを押すと、編集ページに移動し、そこで名前を編集できます。ここで、[更新] ボタンをクリックすると、ユーザーのユーザー名とパスワードを再度要求する AlertDialog が表示されます。ここに私が作ったコードがあります:

public void btn_add_update_click(View v){
    hideKeyboard();
    final String username = getIntent().getStringExtra("USERNAME");
    final String str_sitename = this.edt_sitename.getText().toString();
    String str_username = this.edt_username.getText().toString();
    String str_password = this.edt_password.getText().toString();
    LayoutInflater li = LayoutInflater.from(context);
    View prompt = li.inflate(R.layout.prompts, null);
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
    alertDialogBuilder.setView(prompt);
    final EditText user = (EditText) prompt.findViewById(R.id.loginEmail);
    final EditText pass = (EditText) prompt.findViewById(R.id.loginPassword);
    final TextView msg = (TextView) prompt.findViewById(R.id.login_error);
    final String password = pass.getText().toString();
    user.setText(getIntent().getStringExtra("USERNAME"));


    if (str_sitename.equals("")){
        //edt_sitename.requestFocus();
        show_mesg("Please insert sitename.");
    }else if (str_username.equals("")){
        //edt_username.requestFocus();
        show_mesg("Please insert username.");
    }else if (str_password.equals("")){
        //edt_password.requestFocus();
        show_mesg("Please insert password.");
    }else{
        if (selected_website!=null){
            selected_website.setUser(username);
            selected_website.setSitename(str_sitename);
            selected_website.setUsername(str_username);
            selected_website.setPassword(str_password);
                alertDialogBuilder.setCancelable(false)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {    
                        DBAdapter dbUser = new DBAdapter(PasswordActivity.this);
                        dbUser.open();
                        if(dbUser.Login(username, password))
                        {
                                datasource.updateWebsite(selected_website);
                                show_mesg(str_sitename + " updated.");
                                hideKeyboard();
                                selected_website = null;
                                show_list_layout(); 
                            }
                        else{
                            msg.setText("Username or Password is not correct.");
                            }
                        }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();

                    }
                });
                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show(); 
        }

        else{
            datasource.addWebsite(username, str_sitename,str_username,str_password);
            hideKeyboard();
            show_mesg(str_sitename + " added.");
            selected_website = null;
            show_add_layout();
        }
    }
}

しかし、実行するとうまくいきません。[OK] ボタンと [キャンセル] ボタンが表示されません。これに関するアドバイスはありますか?

4

1 に答える 1

1

まず、コントロールが更新状態になっていることを確認してください。その場合、アラート ダイアログの作成中にコードに問題はないようです。とにかく、コントロールが更新状態にあることが確実な場合は、これを試す価値があります。

 void showUpdateDialog(){
      AlertDialog.Builder updateDialog  = new AlertDialog.Builder(YourClassName.this);
      updateDialog.setTitle("Update Dialog");
      updateDialog.setCancelable(false);

    LayoutInflater layoutInflater  = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

  View inflatingView=layoutInflater.inflate(R.layout.updatelayout,null);

      EditText user = (EditText) inflatingView.findViewById(R.id.loginEmail);
      EditText pass = (EditText) inflatingView.findViewById(R.id.loginPassword);

      // More views here.......

 updateDialog.setPositiveButton("OK", new DialogInterface.OnClickListener(){

   @Override
   public void onClick(DialogInterface arg0, int arg1) {
    // TODO Auto-generated method stub

   }});

  updateDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){

   @Override
   public void onClick(DialogInterface arg0, int arg1) {
    // TODO Auto-generated method stub

   }});

        updateDialog.setView(inflatingView);
        updateDialog.show();
    }

showUpdateDialog()アクティビティで更新ボタンがクリックされたときに、このメソッドを呼び出します。それでも肯定ボタンと否定ボタンが見つからない場合は、独自のボタンを作成してください。そうすれば、システムのプラス ボタンとマイナス ボタンに頼る必要はありません。updatelayout.xml に 2 つのボタンを追加し、showUpdateDialog() メソッドで以下のようにコーディングして、それらのボタンで必要なことを行います。

Button yesButton = (Button)view.findViewById(R.id.positiveButton);
  yesButton.setOnClickListener(new Button.OnClickListener(){

    @Override
       public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), " Yes Button Is Pressed", Toast.LENGTH_LONG).show();
       }});
于 2013-01-31T04:25:37.690 に答える