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;
}