arayray
arayray

Reputation: 241

Android AlertDialog SetText

Hi I have a alertdialog that is create when you click on an item in a listview, Im trying to get the name of the file,description, author etc.. from my activity and display it in my alertdialog but .setText will not work, can someone please help. Thank you, here is my code: http://pastebin.com/FzWSPp5e

Upvotes: 3

Views: 4160

Answers (1)

moonlightcheese
moonlightcheese

Reputation: 10590

this is not at all how you properly use dialogs in Android. you need to define your dialogs in an override of onCreateDialog, as described in the documentation:

http://developer.android.com/guide/topics/ui/dialogs.html

following this guide, you should be able to fix your problem. here's an example i just copied and pasted from a random app:

@Override
protected Dialog onCreateDialog(int id, Bundle b) {
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
    AlertDialog.Builder builder = null;
    switch(id) {
        case DIALOG_BLOCK_SIZE:
        {
            Dialog dialog = new Dialog(this);
            final View dialogLayout = inflater.inflate(R.layout.dialog_block_size, null);
            builder = new AlertDialog.Builder(this);
            builder.setView(dialogLayout);
            builder.setTitle("Set Block Size");

            final EditText blockIn = (EditText)dialogLayout.findViewById(R.id.block_size_in);
            blockIn.setText(new Integer(pref.getInt("block_size", 6)).toString());

            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        SharedPreferences.Editor editor = pref.edit();
                        editor.putInt("block_size", new Integer(blockIn.getText().toString()));
                        editor.commit();
                        ////////TODO///////////
                        //notify MinutemaidService that we have changed the block_size
                        dialog.dismiss();
                    }
                });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });
            dialog = builder.create();
            return dialog;
        }
        default:
        {
            return null;
        }
    }
    return dialog;
}

with the above code, you can call showDialog(DIALOG_BLOCK_SIZE) to show the dialog. also note that dialogs are created once and shown over and over again. to force rebuilding a dialog, call removeDialog(int) before calling showDialog(int). overriding onPrepareDialog() is the best method, but using removeDialog works and it's easier.

Upvotes: 1

Related Questions