tags:

views:

898

answers:

2

I am trying to make the item list dynamic, so i can add to it on runtime, but i have no idea. CharSeqence isnt dynamic, and no clue how to use the adapter option, how could i change my code to be dynamic?

private void alertDialogLoadFile() {

    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("Choose:");  
    CharSequence[] items = { "moshe", "yosi", "ee" };
    alert.setSingleChoiceItems(m_items , -1, new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int item){
            /* User clicked on a radio button do some stuff */
        }
    });

    alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int id) {
    }
    });

    alert.setNegativeButton("No", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    });


    AlertDialog ad = alert.create();
    ad.show();

}
A: 

If you create the dialog in onCreateDialog(), you can implement onPrepareDialog() to change the choices before it's displayed to the user. For example:

protected void onPrepareDialog(int id, Dialog dialog) {    
    if (id == YOUR_DIALOG_ID) {

        // Create new adapter
        ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>();
        adapter.add("new items ...");
        ...

        // Use the new adapter
        AlertDialog alert = (AlertDialog) dialog;
        alert.getListView().setAdapter(adapter);
    }
}

You could also get the same effect by getting the adapter from the dialog (and casting it to the correct type) and adding or removing the items as you see fit. I'd probably lean towards simply creating a new adapter, because you won't have to worry about casting the value from getListAdapter() to the wrong type. However, reusing the adapter is probably a bit more memory efficient.

Erich Douglass
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(); you cant use it this way, there is no defualt constructor.
rayman
A: 

I tried to do your first suggestaion(although if you could show me how to do the second one) but still no success,this is how i tried it:

@Override

@Override
protected Dialog onCreateDialog(int id)
{
    switch (id)
    {
    case DIALOG_SINGLE_CHOICE:
        return new AlertDialog.Builder(TwittListActivity.this)
        .setTitle("Choose route map:")



        .setPositiveButton("Yes",
                new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog, int id)
                    {
                        TwittListActivity.this.finish();
                    }
                })
        .setNegativeButton("No",
                new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog, int id)
                    {
                        dialog.cancel();
                    }
                })

        .create();

protected void onPrepareDialog(int id, Dialog dialog) {     
    if (id == DIALOG_SINGLE_CHOICE) 
    { 

        // Create new adapter 
        ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(MyActivity.this, 0); 
        adapter.add("a"); 
        adapter.add("b"); 


        // Use the new adapter 
        AlertDialog alert = (AlertDialog) dialog; 
        alert.getListView().setAdapter(adapter); 
    } 
} 
rayman