开发者

ListActivity error

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();


        }


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"/>


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜