0

I searched for a while and found no solution. I tried dialog.cancel() and dialog.dismiss and other ones, but nothing worked.

I have a Listview and you can change the name of this listview item by clicking the OptionsMenu, then you choise an item in the Listview and now an alertDialog opens and you can write a new name in it. Then you can say "save" or "cancel". Till no everything workes fine, but if you press again an item another alertDialog opens and you can change name again. That should not happen, you should edit the name just once.

Code:

public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

            case R.id.changeHersteller:
                //TODO bleibt "offen" wenn man nochmals auf ein item klickt
                Toast.makeText(getApplicationContext(), R.string.takeHersteller, Toast.LENGTH_LONG).show();
                final ListView listView = (ListView) findViewById(R.id.listViewHersteller);
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                        final AlertDialog.Builder builder = new AlertDialog.Builder(Hersteller.this);
                        final EditText editText = new EditText(Hersteller.this);
                        builder.setView(editText);
                        builder.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                String oldName = listView.getItemAtPosition(position).toString();
                                String newName = editText.getText().toString();
                                //Log.i(TAG, oldName);
                                //Log.i(TAG, newName);
                                if(!newName.isEmpty()){
                                    updateFolder(mainFolder, oldName, newName);
                                    setContent(mainFolder, listView);
                                }
                                dialog.dismiss(); //don't work
                            }
                        })
                                .setNegativeButton(R.string.abort, new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        dialog.dismiss(); //don't work
                                        return;
                                    }
                                });
                        AlertDialog alertDialog = builder.create();
                        alertDialog.setTitle(R.string.changeHersteller);
                        alertDialog.show();
                    }
                });
                return true;
        }
        return true;
    }
Niklas
  • 45
  • 7
  • Maybe try to return `false`, after you dismiss dialog? – David Kasabji Jan 08 '17 at 15:01
  • Make sure that you are dismissing the **RIGHT** dialog. See you have any other dialog s around that you initialized with the same name! – Charuක Jan 08 '17 at 15:26
  • no, both do not work – Niklas Jan 08 '17 at 18:48
  • Maybe try replaced "dialog.dismiss();" with "listView.setOnItemClickListener(null);". – i_A_mok Jan 09 '17 at 04:29
  • Didn't work like I wanna have it. Couse the normal code should open then a new intent if you cick on the item. With your code 'listView.setOnItemClickListener(null)' it opens not the new intent. I replaced now the 'dialog.dismiss();' with 'recreate();' It's not my favourite couse there is a little _lightning_ but it works :) – Niklas Jan 11 '17 at 20:10

0 Answers0