views:

36

answers:

2

Im opening a ListActivity with an xml called list with a ListView named list. I have an array adapter set up and working in another activity (not a list activity though, just a normal activity) and it all works fine. When I try to open this list activity though, I get an error saying that I need a ListView with the id android.R.id.list. I have that ListView though. What is my problem?

Code for my AlertDialog I am trying to get to work.

public void onListItemClick(ListView parent, View v, int position, long id)

    {
        Context mContext = getApplicationContext();
        Dialog dialog = new Dialog(mContext);

        dialog.setContentView(R.layout.custom_dialog);
        dialog.setTitle(getText(position));

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




        }

and this was another code i had trying to get it to work

        public void onListItemClick(ListView parent, View v, int position,
                long id) {
            new AlertDialog.Builder(Anions.this);
                alertDialog.setContentView(R.layout.custom_dialog);
                alertDialog.setTitle(anions[position]);
            ImageView image = (ImageView) alertDialog.findViewById(R.id.image);
            image.setImageResource(R.drawable.hydrogen);
            alertDialog.setButton("Done", new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog, int which){
                    return;
                }
            }

            );
            alertDialog.show();


        }
+1  A: 

Your list view has to have the id declared as such android:id="@android:id/list" for Android to automagically bind to it.

<ListView android:id="@android:id/list"
           android:layout_width="fill_parent" 
           android:layout_height="fill_parent"/>

Take note that you are not defining a new id, but using one which Android knows about. This allows you to have many different layout elements in the layout file, but android can find the one which will contain the list elements.

This is a quote from the documentation for ListActivity.

ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "@android:id/list" (or list if it's in code)

The same also goes for having a text view in the same file (with id 'empty') which Android will show in case your ListView is empty. e.g.

<TextView android:id="@android:id/empty"
               android:layout_width="fill_parent" 
               android:layout_height="wrap_content"               
               android:text="No data"/>
codinguser
Ok. I edited my question some to have the information there.
Joshua Sutherland
Your XML is wrong. You are defining a new id attribute called 'list' inside your own R class. You are to use the one from android.R class. Look at the XML I posted again and see how I defined the id. Use android:id="@android:id/list" and NOT android:id="@+id/list"
codinguser
ok. I got my listview to show finaly and that is working fine. now my problem is that when I click on a selection I want it to open an AlertDialog, but i keep erroring out. I'm using a custom dialog and have tried to set it up a few different ways. Here is the latest try above.
Joshua Sutherland
A quick check at the documentation shows that AlertDialogs are good for short text, lists, or checkboxes. If you want a Dialog with your custom layout, you will be better served using the Dialog class directly. The code also looks wrong (if it is complete as is). In the first snippet, you define a variable `dialog` but later u reference 'alertDialog'. Also, does the image displayed ever vary?I could go ahead, but one can only say so much in a comment. Here is a link to showing dialogs where you can find more info. http://developer.android.com/guide/topics/ui/dialogs.html#ShowingADialog
codinguser
A: 

Ok. So I finally found what I needed after going through who knows how many sites. Anyways here is the code that finally worked for anyone else out there who runs into a problem like this.

public void onListItemClick(ListView parent, View v, int position, long id) {

if ("Acetate".equals(anions[position]))

{ Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.custom_dialog); dialog.setTitle("Acetate");
dialog.setCancelable(true);

ImageView img = (ImageView) dialog.findViewById(R.id.ImageView01); img.setImageResource(R.drawable.element_el);

dialog.show();

}

my final code will probably vary from this but this works. For more information about this go to http://www.androidear.com/developers/how-to-display-a-custom-dialog-in-your-android-application/comment-page-1/#comment-1570

Joshua Sutherland