I have seen all kinds of tutorials about how to turn an Activity into a ListActivity to populate a ListView, but nothing for ListFragments. I have two tabs with a Fragment under each. Here is what my fragment currently looks like…. can someone give me some idea on how I can do this?
public class MainFragment extends ListFragment {
String[] items = { "12 Monkeys", "(500) Days of Summer", "Chariots of Fire" };
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.fragment_main, container, false);
}
}
and here is the XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
I’m trying to get items to show in the ListView.
You don’t need to inflate a
viewcontaining alistviewinside of yourListFragment. Simply create anadapter, give it some data and inonCreate()of yourfragmentcallsetListAdapter(). Then somewhere else (presumably in yourActivity) you can then add thisfragmentwith afragmentmanager.