Twing90
Twing90

Reputation: 417

Alert Dialog don't close

I have a problem. in my activity I have a button and when I click it I call a method. this method show me an alert dialog with a listview and i want save the item click to a variable and then i want to close the method but it doesn't close!!! why?? I post the code of the method. My logcat doesn't give me any error. Can anyone help me?? please

private void getValuta() {

AlertDialog.Builder miaAlert = new AlertDialog.Builder(this);

final ListView lV = new ListView(this);

Cursor c = null;

miaAlert.setTitle("Choose Valuta");

c = vdb.fetchValuteListView("0");

startManagingCursor(c);


SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
        android.R.layout.simple_list_item_1, 
        c, 
        new String[] {
                ValuteDb.ValuteMetaData.VALUTE_NAME_KEY}, 
        new int[] { android.R.id.text1});

stopManagingCursor(c);

lV.setAdapter(adapter);

miaAlert.setView(lV);

miaAlert.setCancelable(false);

lV.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v,
            int position, long id) {

        String riga = String.valueOf(id);
        String confr = vdb.getRecord(riga,2);
        System.out.println("position= " + position + "/id= " + id+"/nome= "+confr);
        new_valuta = vdb.getRecord(riga,2);
        listdb.update("9", "Valuta", new_valuta, "2");
        c_list.requery();

        return;

    }
});

AlertDialog alert = miaAlert.create();

alert.show();}

Upvotes: 1

Views: 1766

Answers (4)

user1087919
user1087919

Reputation:

Use alert.dismiss() to dismiss dialog

Upvotes: 0

Aleks G
Aleks G

Reputation: 57336

What you're missing is the call to actually close the dialog:

AlertDialog.Builder miaAlert = new AlertDialog.Builder(this);

final ListView lV = new ListView(this);
miaAlert.setTitle("Choose Valuta");
miaAlert.setView(lV);
miaAlert.setCancelable(false);

final AlertDialog alert = miaAlert.create();

lV.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v,
            int position, long id) {

        String riga = String.valueOf(id);
        String confr = vdb.getRecord(riga,2);
        System.out.println("position= " + position + "/id= " + id+"/nome= "+confr);
        new_valuta = vdb.getRecord(riga,2);
        listdb.update("9", "Valuta", new_valuta, "2");
        c_list.requery();

        alert.dismiss();

    }
});

alert.show();

This is, of course, in addition to your Cursor management code.

Upvotes: 2

Ant4res
Ant4res

Reputation: 1225

I think you have to call alert.dismiss() within the onItemClick(...) method.

Upvotes: 1

Jave
Jave

Reputation: 31856

You don't do anything to close your dialog. If you set buttons the default behaviour is to close the dialog when clicked, but in this case you are using a custom view with a custom OnItemClickListener.

You can explicitly close the dialog by calling alert.dismiss(); in the onItemClick()-method.

Upvotes: 0

Related Questions