0

I am coding to make a application to manage account and password, everything is good, only Button Edit has problem. I click it one time, it works normaly, when i click it in second time, it crash. Can you help me? This is logcat

private ListView listview_account;
private Button add_new;
private EditText newtype, oldtype;
private EditText newusername, oldusername;
private EditText newpassword, oldpassword;
private List<Account> list_accounts;
private String[] list_type;
private MyDatabaseHelper helper = new MyDatabaseHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show);
    listview_account = (ListView)findViewById(R.id.list_account);
    add_new = (Button)findViewById(R.id.ADD);

    LayoutInflater inflater = this.getLayoutInflater();
    final View dialogView_addnew = inflater.inflate(R.layout.dialog_addnew, null);
    final View dialogView_change = inflater.inflate(R.layout.dialog_change, null);

    newtype = (EditText)dialogView_addnew.findViewById(R.id.type);
    newusername = (EditText)dialogView_addnew.findViewById(R.id.username);
    newpassword = (EditText)dialogView_addnew.findViewById(R.id.password);

    oldtype = (EditText)dialogView_change.findViewById(R.id.type_old);
    oldusername = (EditText)dialogView_change.findViewById(R.id.username_old);
    oldpassword = (EditText)dialogView_change.findViewById(R.id.password_old);

    update_show();

    final AlertDialog.Builder builder_show = new AlertDialog.Builder(this);
    final AlertDialog.Builder builder_add = new AlertDialog.Builder(this);
    final AlertDialog.Builder builder_delete = new AlertDialog.Builder(this);
    final AlertDialog.Builder builder_change = new AlertDialog.Builder(this);

    builder_add.setView(dialogView_addnew);
    builder_change.setView(dialogView_change);

    builder_delete.setTitle("Delete Account");
    builder_delete.setIcon(android.R.drawable.ic_delete);
    builder_delete.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
        }
    });

    builder_change.setTitle("Edit");
    builder_change.setIcon(android.R.drawable.ic_input_get);
    builder_change.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });

    builder_add.setTitle("Add new account");
    builder_add.setIcon(android.R.drawable.ic_input_add);
    builder_add.setPositiveButton("Add", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    try {
                        add_new_account();
                    } catch (Exception ex) {
                        Toast.makeText(getApplicationContext(), ex.toString(), Toast.LENGTH_LONG).show();
                    }

                }
            }
    );
    builder_add.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    newtype.setText("");
                    newusername.setText("");
                    newpassword.setText("");
                    newtype.requestFocus();
                }
            }
    );
    final AlertDialog alert_add_new = builder_add.create();
    add_new.setOnClickListener(new Button.OnClickListener() {
                                   @Override
                                   public void onClick(View v) {
                                       alert_add_new.show();
                                   }
                               }
    );
    listview_account.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View v,final int pos, long id) {
            builder_show.setTitle(list_type[pos]);
            builder_show.setIcon(android.R.drawable.ic_dialog_info);
            builder_show.setMessage("Username: " + list_accounts.get(pos).getUSERNAME()
                    + "\nPassword: " + list_accounts.get(pos).getPASSWORD());
            builder_show.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            builder_delete.setMessage("Do you want to remove " + list_type[pos]+ "?\nUsername: " + list_accounts.get(pos).getUSERNAME() + "\nPassword: " + list_accounts.get(pos).getPASSWORD());

                            builder_delete.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    helper.DeleteAcc(list_type[pos]);
                                    Toast.makeText(getApplicationContext(), "Success.", Toast.LENGTH_SHORT).show();
                                    update_show();
                                }
                            });
                            final AlertDialog dialog_delete = builder_delete.create();
                            dialog_delete.show();

                        }
                    }
            );
            builder_show.setPositiveButton("Edit", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            oldtype.setText(list_type[pos]);
                            oldusername.setText(list_accounts.get(pos).getUSERNAME());
                            oldpassword.setText(list_accounts.get(pos).getPASSWORD());

                            builder_change.setPositiveButton("Save", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    helper.DeleteAcc(list_type[pos]);
                                    change();
                                }
                            });

                            AlertDialog alert_change = builder_change.create();
                            alert_change.show();
                        }
                    }
            );
            AlertDialog alert1_show = builder_show.create();
            alert1_show.show();
        }
    });
}
private void add_new_account()
{
    String new_type = newtype.getText().toString();
    String new_username = newusername.getText().toString();
    String new_password = newpassword.getText().toString();
    Account new_acc = new Account(new_type, new_username, new_password);
    helper.AddAccount(new_acc);
    update_show();
    newtype.setText("");
    newusername.setText("");
    newpassword.setText("");
    newtype.requestFocus();
    Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
}
private void change()
{
    String old_type = oldtype.getText().toString();
    String old_username = oldusername.getText().toString();
    String old_password = oldpassword.getText().toString();
    Account new_acc = new Account(old_type, old_username, old_password);
    helper.AddAccount(new_acc);
    update_show();
    Toast.makeText(getApplicationContext(), "Đã lưu.", Toast.LENGTH_SHORT).show();
}
private void update_show()
{
    list_accounts = helper.getListAccount();
    list_type = new String[list_accounts.size()];
    int i = 0;
    for (Account acc: list_accounts) {  list_type[i++] = acc.TYPE; }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>
            (this, android.R.layout.simple_list_item_1, list_type);
    listview_account.setAdapter(adapter);
}

1 Answers1

0

As for me, the problem is that you create your builder_show dialog inside your ItemClickListener.

listview_account.setOnItemClickListener(new AdapterView.OnItemClickListener()

Actually, you create it every time and on second time system shows you error, that "The specified child already has a parent. You must call removeView() on the child's parent first." Because dialog already exists.

If you wish to preserve everything as is, you should remove your dialog before every show() call. Check this for details.

But correct (for this issue) would be to move dialog creation code outside ItemClickListener.

Community
  • 1
  • 1
Goltsev Eugene
  • 3,325
  • 6
  • 25
  • 48