Question about ListView Id


#1

After going through the whole book I have been working on an app that is heavy on listviews and I am extending ListFragment similar to what is happening in CrimeListFragment in this chapter.

I need to do some work on the fragment view in onCreateView within the ListFragment and am having some serious difficulty resolving the issue of where I need to put within my layout a ListView with the android:id attribute set to @android:id/list.

I keep getting faults and have found numerous references telling me to do this but I am just being a moron and not getting where and in which layout file it needs to go, and what I need to do additionally inflation-wise etc. The idea is to be able to findViewbyId to get the ListView in onCreateView.

Where do I need to put this?

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

I have noticed that in every example in the book that uses a ListFragment, there is no onCreateView method whatsoever in the extended ListFragment class (like CrimeListFragment) and this magic id is not around either.

Like this example my activity_fragment.xml file is:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragmentContainer" android:layout_width="match_parent" android:layout_height="match_parent" />

This is inflated just like this chapter in the SingleActivityFragment abstract class in the onCreate method.

My list_item_xxx.xml layout used for my custom item layout is a relative layout similar to list_item_crime.xml in this chapter.

Maybe I am confused as to the use of these layouts. I thought the list_item_crime layout is used when there is no view inflated in getView of the custom adapter, and that activity_fragment is inflated in singlefragmentactivity when the fragmentactivity (the fragment? or the activity?) is getting kicked off.

So where in which xml file should I put the ListView business so that I can access it in onCreateView? SHould I create an additional file with just the listview in it and only use that when inflating the ListView and accessing it in onCreateView?

Sorry for the convoluted question but this is driving me nuts. For now I simply removed onCreateView and am living without it in my listfragmentactivity but would like to use it.


#2

There’s no onCreateView() in the exercises that extend ListFragment because ListFragment is a subclass of Fragment and the subclass comes with built-in help for displaying lists. The inherited onCreateView() is the one that’s getting called. So I think that’s why you don’t see onCreateView() defined in the extended classes? (The book says we will override onCreateView() in a later chapter.)


#3

OK well I did ultimately figure it out and it works but maybe I’m still not understanding it. I overrode onCreateView in my ListFragment-derived class so that I could setup an onScrollListener() so I would know when to go grab the next part of my list data from the cloud on a background thread.

I didn’t think onCreate was the place to do this so I attnempted to do it in onCreateView. So I Overrode onCreateView and ran into issues doing the inflation so I could get access to the view so I could hang the listener on it. (I know there’s a LOT wrong with that statement).

I kept running into issues and faults since the activity_fragment layout that was in use for the fragment via SingleFragmentActivity did not have te requisite references to a ListView. So I broke out the hammer and jammed the ListView stuff I cited above into the activity_fragment layout, and voila I could get a reference to the ListView, did my ScrollListening and everything was great.

Then I was unhappy that I had polluted the perfectly simple activity_fragment layout, and moved the ListView bit into it’s own layout file, and inflated that in onCreateView - everything still worked just fine and thats where I am now.

After your comments though I may really be overdoing this. What would be the best way to setup the onScrollListener and in what method? Now I am thinking I should wait and do it in the onResume method of the ListFragment since that is the last oppy to set it up before the user can touch it. However once again I have the issue, of HOW DO I GET A REF TO THE LISTVIEW in onResume - can I just have a statement like this?

ListView lv = (ListView)findViewById(R.id.list_view); lv.setOnScrollListener(new OnScrollListener() {

Is R.is.list_view a system resource or what reource id can I use to get the ListView reference from within the ListFragment? Or is there an even simpler way?


#4

OMG its even easier. I will override either onResume or onActivityCreated and merely invoke the following, right?

Then hang my scrollListener on the view and I am in business. I just wasnt asking the right question or seeing with OOP eyes :wink: But I’m learning - even an old OS programmer of 30 years can get there :wink:

OMG so simple - oh well I am learning everywhich way to wrench on this thing…

Anyway thanks for the tolerance, patience, and great educational resource. (Hmm whats that clicking sound?)