A ListFragment Application in Android

July 2, 2012
By

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”

New Android Project

Figure 1: New Android Project


2. Select Android Build Target

Select Build Target

Figure 2 : Select Build Target


3. Enter application details

Application Details

Figure 3: 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

ListFragment Demo in Execution

Figure 4 : ListFragment Demo in Execution


7. Download


8. Reference

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


How to hire me?

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


Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :



Tags: , , ,

11 Responses to A ListFragment Application in Android

  1. aradhna on September 19, 2012 at 4:19 pm

    thnx its usefull to me

  2. Tech Rahul on February 9, 2013 at 1:29 pm

    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.

    • bott on August 2, 2013 at 7:57 pm

      Hey same error for my. Someone know how to fix it pls??

      • george on August 2, 2013 at 10:56 pm

        Hi Bott,

        Did you try with the downloaded source code?

        Also please try with the apk file contained in the source code.

        • Bott on August 3, 2013 at 4:56 am

          i try and work fine, i was trayin to execute the clas extends Listfragment, thanks!

  3. okfriansyah on April 3, 2013 at 2:50 pm

    Nice. but how to use it with icon. so different icon for different array list.

    thanks,

  4. Paulo McNally on May 12, 2013 at 12:33 pm

    How to clear ListFragment?? http://i.imgur.com/h5oEKHT.jpg

  5. Eli on December 28, 2013 at 7:04 pm

    How do I make the list items on the list fragment clickable?

  6. Richard on August 27, 2014 at 11:32 am

    Apparently you should only set the adaptor in the onActivityCreated callback.

  7. Chris on October 5, 2014 at 7:39 pm

    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?

Leave a Reply to Richard Cancel reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner