In this article we will create an application which displays a list of countries using ListFragment. ListFragment is a subclass of Fragment class which can display a set of items in a listview and facilitates to exist as a fragment. We are building this application for Android 4.0 in which fragments are supported by default.
1. Create an Android Project namely “ListFragmentDemo”
2. Select Android Build Target
3. Enter application details
4. src/in/wptrafficanalyzer/listfragmentdemo/CountryList.java
package in.wptrafficanalyzer.listfragmentdemo; import android.app.ListFragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; public class CountryList extends ListFragment { String[] countries = new String[] { "India", "Pakistan", "Sri Lanka", "China", "Bangladesh", "Nepal", "Afghanistan", "North Korea", "South Korea", "Japan" }; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { /** Creating an array adapter to store the list of countries **/ ArrayAdapter<String> adapter = new ArrayAdapter<String>(inflater.getContext(), android.R.layout.simple_list_item_1,countries); /** Setting the list adapter for the ListFragment */ setListAdapter(adapter); return super.onCreateView(inflater, container, savedInstanceState); } }
5. res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <fragment android:id="@+id/country_fragment" android:name="in.wptrafficanalyzer.listfragmentdemo.CountryList" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
6. Application in Execution
7. Download

8. Reference
http://developer.android.com/guide/index.html

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in
You can hire me on hourly basis or on project basis for Android applications development.
For hiring me, please mail your requirements to info@wptrafficanalyzer.in.
My other blogs
store4js.blogspot.com
thnx its usefull to me
I am getting this error.
02-09 13:23:40.153: E/AndroidRuntime(745): FATAL EXCEPTION: main
02-09 13:23:40.153: E/AndroidRuntime(745): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{in.wptrafficanalyzer.listfragmentdemo/in.wptrafficanalyzer.listfragmentdemo.CountryList}: java.lang.ClassCastException: in.wptrafficanalyzer.listfragmentdemo.CountryList cannot be cast to android.app.Activity
Can you guide me where I am going wrong.
Hey same error for my. Someone know how to fix it pls??
Hi Bott,
Did you try with the downloaded source code?
Also please try with the apk file contained in the source code.
i try and work fine, i was trayin to execute the clas extends Listfragment, thanks!
Nice. but how to use it with icon. so different icon for different array list.
thanks,
Please see the article titled Android ListFragment with Images and Text using Android Support Library
How to clear ListFragment?? http://i.imgur.com/h5oEKHT.jpg
How do I make the list items on the list fragment clickable?
Apparently you should only set the adaptor in the onActivityCreated callback.
Hi,
How to delete the element when clicked which should get auto updated in the listfragment..kinda auto refresh on listening to deletion made.heard like its possible with the method notifyDataSetChanged(),but i don’t know how?