0

I am trying to show a user timeline in an android fragment. My HomeFragment.java onCreate() looks like this:

public class HomeFragment extends ListFragment {

@InjectView(android.R.id.list) ListView mTimeline;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mSectionNumber = getArguments().getInt(ARG_SECTION_NUMBER);
    }

    final UserTimeline userTimeline = new UserTimeline.Builder().screenName("fabric").build();
    final TweetTimelineListAdapter adapter = new TweetTimelineListAdapter(getActivity(), userTimeline);
    setListAdapter(adapter);

}

And my fragment_home.xml has this listview:

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

HomeFragment is a fragment of HomeActivity. This code just shows an empty list. How do I fix this problem? I would also like to know how setListAdapter(adapter); works. How does it know which list to set the adapter to since it can't be called on any particular list. I used this twitter fabric page for reference when writing this code.

spatel95
  • 447
  • 1
  • 5
  • 10

1 Answers1

0

Inflate your custom layout fragment_home.xml in the ListFragmentby overriding onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_home, container, false);
}

Your layout contains a ListView with the special @android:id/list id which identifies the ListView ListFragment's setListAdapter will use.

Also, TweetTimelineListAdapter is just a ListAdapter. If you'd like to find your own ListView and set the adapter yourself, use an ordinary Fragment (not a ListFragment).

mTimeline.setListAdapter(adapter)  // mTimeline is a ListView

The docs include a ListFragment example.

dgh
  • 8,969
  • 9
  • 38
  • 49