Android ListFragment example

There are at least two different ways to display a ListView object in an Activity. In this Android tip, I am going to show how to display the ListView in an Activity using ListFragment. ListFragment hosts a ListView object that binds to a data source. The data source can be an array or a Cursor holding query results. Typically, the data source is wrapped in an object that implements the ListAdapter interface such as ArrayAdapter, BaseAdapter, CursorAdapter, SimpleCursorAdapter, etc. To bind the data source to the ListFragment, you need to call the setListAdapter() passing the adapter object.
Now to have example application on using ListFragment to display a ListView object in an Activity, create a new Android Project. Then in the rest/layout folder, you create two layout files: fragmentlayout.xml and rowlayout.xml. The fragmentlayout.xml file is the layout file of the ListFragment. It contains a ListView widget.





<?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/android:list"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1"
          android:drawSelectorOnTop="false"/>

</LinearLayout>


The rowlayout.xml file defines the layout of the row or item of the ListView. In this example, it has only one TextView.

<?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" >
<TextView
     android:id="@+id/txtitem"
     android:layout_width="wrap_content"
    android:layout_height="wrap_content"  
   android:padding="10dp"
      />  

</LinearLayout>



Next, in the src folder, you create a class called LstFragment that extends the ListFragment class. In onCreateView() method, you create a simple data source (an array of string), create an object of ArrayAdapter to wrap the data source, and set the adapter to the ListFragment using the setListAdapter(). To handle the item click-vent, you need to override the onListItemClick() method. In the example, the text of a selected item is displayed in Toast message when the item is selected.

package com.example.andexamples;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class LstFragment extends ListFragment{

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragmentlayout, container, false);
     // Create an array of string to be data source of the ListFragment
     String[] datasource={"English","French","Khmer","Japanese","Russian","Chinese"};
     // Create ArrayAdapter object to wrap the data source
     ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),R.layout.rowlayout,R.id.txtitem,datasource);
     // Bind adapter to the ListFragment
     setListAdapter(adapter);
     // Retain the ListFragment instance across Activity re-creation
     setRetainInstance(true);
     return rootView;
   }

// Handle Item click event
public void onListItemClick(ListView l, View view, int position, long id){
  ViewGroup viewg=(ViewGroup)view;
  TextView tv=(TextView)viewg.findViewById(R.id.txtitem);
  Toast.makeText(getActivity(), tv.getText().toString(),Toast.LENGTH_LONG).show();

  }

}

Now to display the ListView object in the MainActivity, in the onCreate() method, you write the code as shown below.
package com.example.andexamples;

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.view.MenuItem;

public class MainActivity extends FragmentActivity {

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LstFragment lstfragment=(LstFragment)getSupportFragmentManager().findFragmentByTag("lstfragment");
    if(lstfragment==null){
       lstfragment=new LstFragment();
       FragmentTransaction transact=getSupportFragmentManager().beginTransaction();
       transact.add(android.R.id.content,lstfragment,"lstfragment");
      transact.commit();

     }
  }

}


Android-ListFragment-example

Posted by: Dara | post date: 10-20-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.