Fill listview from fragment

Posted by Bohsen on Stack Overflow See other posts from Stack Overflow or by Bohsen
Published on 2012-10-04T09:36:13Z Indexed on 2012/10/04 9:37 UTC
Read the original article Hit count: 185

Filed under:
|

I have a layout file containing a listview that I would like to fill with the help of a Fragment. But it continues to give me errors. The layout file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

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

<TableLayout
    android:id="@+id/details"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:stretchColumns="1" >

    <Button
        android:id="@+id/create_patient_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/create_patient_button" />
</TableLayout>

</RelativeLayout>

My fragmentActivity:

public class BasicFragmentActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.create_patient_view);

    FragmentManager fm       = getSupportFragmentManager();
    Fragment        fragment = fm.findFragmentById(R.id.list);

    if (fragment == null) {


        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.list, new BasicFragment());
        ft.commit(); // Make sure you call commit or your Fragment will not be added. 
                     // This is very common mistake when working with Fragments!
    }
}

}

My ListFragment:

public class BasicFragment extends ListFragment {

private PatientAdapter pAdapter;

@Override
public void onActivityCreated(Bundle savedState) {
    super.onActivityCreated(savedState);

    pAdapter = new PatientAdapter(getActivity(), GFRApplication.dPatients);
    setListAdapter(pAdapter);
}
}

The error: java.lang.UnsupportedOperationException: addView(View) is not supported in AdapterView

© Stack Overflow or respective owner

Related posts about android

Related posts about listview