0

I browsed through every other same/similar question:

Android: Your content must have a ListView whose id attribute is android.R.id.list

Your content must have a ListView whose id attribute is 'android.R.id.list'

None of it helped.

The difference is that my Activity doesn't extend ListActivitiy (which seemed to be the problem in the above cases).

My activity extends FragmentActivity (and I can't really change this to Activity because I have dependencies).

public class MainActivity extends FragmentActivity 

... ListView

<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:divider="#b5b5b5"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector" >
</ListView>

... Fragment view.

<fragment
    android:id="@+id/contentFragment"
    android:name="com.landa.fragment.ContentFragment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/include1" />

I also tried changing the 'id' format of the ListView as suggested in the above questions - Android doesn't agree with that notation.

Content fragment extends ListFragment:

public class ContentFragment extends ListFragment {

How can I fix this?

Community
  • 1
  • 1
Tool
  • 12,126
  • 15
  • 70
  • 120
  • And where is the `ListView`? – A--C Feb 08 '13 at 03:15
  • One second, just adding the code. – Tool Feb 08 '13 at 03:16
  • Is the fragment a `ListFragment`? Where exactly is the ListView? Part of which layout in which UI component? – A--C Feb 08 '13 at 03:21
  • Yes. "public class ContentFragment extends ListFragment " it's a ListFragment. ListView is inside content_view.xml. It's rendered in the fragment with "return inflater.inflate(R.layout.content_view, container, false);". I'll add the full code now. – Tool Feb 08 '13 at 03:22
  • By the way, my code works when I do not extend the fragment with ListFragment. But then my list renders as one big object and I can't click it's elements. – Tool Feb 08 '13 at 03:26
  • 1
    Are you're sure you did ` – A--C Feb 08 '13 at 03:27
  • Yes, that produces an error on the following line: "ListView list = (ListView) ac.findViewById(R.id.list);" where "ac" is an Activity (extended FragmentActivity) – Tool Feb 08 '13 at 03:29
  • You have to work with the fragment's view. and when you're finding the Id, use `android.R.id.list` (or in the fragment `getListView()` However, why not let the fragment handle the list itself? Then have optional callbacks to the Activity. – A--C Feb 08 '13 at 03:29
  • This fixed the problem. Answer the question and I'll accept. But there's one last problem I have - I can't scroll through elements of my list (vertical list) - any idea why? – Tool Feb 08 '13 at 03:34
  • Answer posted! Also, since your new issue is different than what this question asks, you should make a new question. – A--C Feb 10 '13 at 17:39

1 Answers1

5

First problem:

android:id="@+id/list"

needed to be

android:id="@android:id/list"

Then when you want to access the ListView, you can do it in this way if you're working in your Activity and you have a reference to the Fragment:

ListView l = fragment.getListView();

or

ListView l = (ListView) fragment.getView().findViewById (android.R.id.list);

If you're in the fragment, calling getListView() will suffice.

Also don't forget that you can have Fragment -> Activity communication by always casting getActivity() into YourActivity to call your logic methods or by creating an interface to handle this.

A--C
  • 36,351
  • 10
  • 106
  • 92