Accessing my ListView


Last Updated:

  1. andywhoa

    andywhoa Well-Known Member This Topic's Starter

    Joined:
    May 15, 2010
    Messages:
    105
    Likes Received:
    13
    I created an Activity that extends ListActivity
    Code (Text):
    1. public class ActivityInbox extends ListActivity implements OnClickListener, StateModelListener {
    2.  
    My layout for this Activity is as follows
    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3.     android:orientation="vertical"
    4.     android:layout_width="fill_parent"
    5.     android:layout_height="fill_parent"
    6.     >
    7.     <ListView
    8.         android:id="@+id/android:list"
    9.         android:layout_width="fill_parent"
    10.         android:layout_height="fill_parent"
    11.     />
    12.     <TextView
    13.         android:id="@+id/android:empty"
    14.         android:layout_width="fill_parent"
    15.         android:layout_height="fill_parent"
    16.         android:text="@string/inbox_no_items"
    17.     />
    18. </LinearLayout>
    I followed a tutorial to do this. As you can see, my ListView's id in the xml is @+id/android:list which seems to be a generic id.

    When I am in my Activity's OnCreate handler, I cannot access this ListView object.

    Code (Text):
    1. ListView lw = (ListView) findViewById(R.id.???????);
    How can I do this? If I edit the layout xml and give it my own custom id, the application crashes when the following line is called in the Activity OnCreate method:
    Code (Text):
    1. setContentView(R.layout.layout_inbox);
    Is there something like this.????????? that can pull the view? I want to create a context menu for my list items, but I need this in a ListView object
     

    Advertisement
  2. cp1

    cp1 Well-Known Member

    Joined:
    Apr 30, 2010
    Messages:
    375
    Likes Received:
    35
    Because you extended list activity you don't need to find the list view -- it's automatic. getListView() will get it if you need to work with it.

    Use setListAdapter(cursorAdapter) to set the adapter.
     

Share This Page

Loading...