views:

235

answers:

1

I'm trying to get my list view to open up an alert dialog to display information when a person clicks on a selection. I have been trying several different things on the Google Developers website that have to do with this but have yet to come up with a result. Here is my code.

package table.periodic;

import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.AdapterView.OnItemClickListener;

public class Anions extends Activity {      AlertDialog.Builder builder;    AlertDialog alertDialog;        String[] anions ={          "Acetate", "Bromide", "Carbonate", "Chlorate", "Chloride", "Chlorite", "Chromate",              "Cyanide", "Dichromate", "Fluoride", "Hexacyanoferrate(II)", "Hexacyanoferrate(III)",           "Hydride", "Hydrogen Carbonate", "Hydrogen Sulfate", "Hydroxide", "Hypochlorite",           "Iodide", "Nitrate", "Nitrite", "Oxide", "Perchlorate", "Permanganate", "Peroxide",             "Phosphate", "Sulfate", "Sulfide", "Sulfite"    };      public void onCreate(Bundle icicle){        super.onCreate(icicle);         setContentView(R.layout.anions);
                ListView list=(ListView)findViewById(R.id.anionlist);
        list.setTextFilterEnabled(true);

        ArrayAdapter<String> aa=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, anions);

        list.setAdapter(aa);

        list.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
                        String itemText = anions[arg2];

        new AlertDialog.Builder(Anions.this);
            Context mContext = getApplicationContext();
            Dialog dialog = new Dialog(mContext);
                dialog.setContentView(R.layout.custom_dialog);
                dialog.setTitle(itemText);

                ImageView image = (ImageView) dialog.findViewById(R.id.image);
                image.setImageResource(R.drawable.hydrogen);



        }

        });
            }

     }
A: 

Instead of getApplicationContext() use your activity as context Anions.this

Don't forget to call .create() and .show()

For example this works for me:

new AlertDialog.Builder(this).setTitle(R.string.contact_groups_add)
            .setView(addView).setPositiveButton(R.string.ok,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {

                        }
                    }).setNegativeButton(R.string.cancel,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {

                        }
                    }).show();
Pentium10
im not getting any dialog to come up. Im still having trouble figuring out how this works, because it seems like what i've got should work but isnt. Do you have any more ideas?
Joshua Sutherland
The above code works definetly. What is missing from your code is probably the show() method. Check in a simple home activity and you will see the dialog comes up.
Pentium10