0

I am trying to create a ListView inside a ListFragment. My list is customize, so it has been created with an adapter. But the aplication crass and give me this error your content must have a listview whose id attribute is 'android.r.id.list This is the ListFragment class:

public static class DummySectionFragment extends ListFragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_section_dummy, container, false);
        Vector<String> names=new Vector<String>();
        names.add("Alex");
        names.add("Julia");
        setListAdapter(new MyAdapter(getActivity(), names));

        return rootView;
    }        
}

This is MyAdapter class:

    public class MiAdaptador extends BaseAdapter {
    private final Activity activ;
    private final Vector<String> list;

    public MiAdaptador(Activity activ, Vector<String> list) {
          super();
          this.actividad = activ;
          this.list = list;
    }

    public View getView(int position, View convertView, 
                                     ViewGroup parent) {
          LayoutInflater inflater = activ.getLayoutInflater();
          View view = inflater.inflate(R.layout.adapter_view, null, true);
          TextView textView =(TextView)view.findViewById(R.id.titulo);
          textView.setText(lista.elementAt(position));          

          return view;
    }
}

My fragment_section_dummy layout:

    <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="Name list"
       android:gravity="center"
       android:layout_margin="10px"
       android:textSize="10pt"/>
<FrameLayout
       android:layout_width="match_parent"
       android:layout_height="0dip"
       android:layout_weight="1">
       <ListView
              android:id="@android:id/list" 
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:drawSelectorOnTop="false" />
       <TextView
              android:id="@android:id/empty"
              android:layout_width="match_parent"
              android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>

And my adapter_view layout:

 <RelativeLayout  
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="?android:attr/listPreferredItemHeight">

       <TextView android:id="@+id/title"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_alignParentTop="true"
             android:textAppearance="?android:attr/textAppearanceLarge"
             android:singleLine="true"
             android:text="title" 
             android:gravity="center"/>
       <TextView android:id="@+id/subtitle"
              android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:text="Other text"
             android:layout_below="@id/title"
             android:layout_alignParentBottom="true"
             android:gravity="center"/>
</RelativeLayout>

How could I solve this problem?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sergiohernandez
  • 103
  • 1
  • 12
  • BTW, I recommend you to use `ArrayList` instead of `Vector`, which is obsolete: http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated – Oleksandr Karaberov Oct 31 '14 at 22:42

2 Answers2

0

I'm starting to think that you should attach the view your inflating to the root view.

Change the inflation to:

View rootView = inflater.inflate(R.layout.fragment_section_dummy, container, true);

Another option could be to only use the onCreateView() method to inflate the main layout. And use the onStart() method to set the list adapter. This way makes sure Android knows about your custom layout.

Posting a full stacktrace could help solving your problem.

Rolf ツ
  • 8,611
  • 6
  • 47
  • 72
0

There was others class who extends to ListFrament (like DummySectionFragment) but these didn`t inflate a layout which have a ListView. So I had to change the parent of there class (Fragment instead of ListFragment) and it has solved. Thanks for your answer

Sergiohernandez
  • 103
  • 1
  • 12