Thursday, 17 January 2013

Simple Sectioned List Example in Android


Download this Sectionizer from the following url :

https://github.com/ragunathjawahar/simple-section-adapter

Add the following files  "Sectionizer.java" and "SimpleSectionAdapter" to your application project as shown in the following figure :

In Your Activity file as follows :

package com.rajeshvijayakumar.sectionedlist;

import com.rajeshvijayakumar.adapters.Sectionizer;
import com.rajeshvijayakumar.adapters.SimpleSectionAdapter;

public class SectionedListDemoActivity extends Activity implements OnItemClickListener {

    //Init Listview
    private ListView listView;

    //Declare List Data
    private List<String> stringList;


    // Declare CustomAdapter
     private StringAdapter stringAdapter;


   //Declare SimpleSectionAdapter
 private SimpleSectionAdapter<String> sectionAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sd_layout_list);
     
       // Initialize ListView
        listView=(ListView)findViewById(R.id.list_view);

       // Initialize array list and add data to the list
        stringList=new ArrayList<String>();
        stringList.add("Rajesh");
        stringList.add("Ramanan");
        stringList.add("Kannan");
        stringList.add("Mahesh");
        stringList.add("Moses");
        stringList.add("Kuberan");
       
        // Initialize Custom Adapter that you defined
        stringAdapter=new StringAdapter(this,R.id.item_textView, Sorting(stringList));

        //Initialize SimpleSectionAdapter and add your custom adapter and sectionizer class that you defined
        sectionAdapter=new SimpleSectionAdapter<String>(this, stringAdapter,
           R.layout.sd_layout_sectioned, R.id.title, new ListSectionizer());

       // set that sectionAdapter to listview
        listView.setAdapter(sectionAdapter);
        listView.setOnItemClickListener(this);
    }
   
   /// Defining your own Custom Adapter
    private class StringAdapter extends ArrayAdapter<String> {
        // Attributes
        private List<String> strModel;

        public StringAdapter(Context context, int textViewResourceId,
                List<String> strModel) {
            super(context, textViewResourceId, strModel);
            this.strModel = strModel;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;
            Holder holder = null;

            if (view == null) {
                view = View.inflate(SectionedListDemoActivity.this,
                        R.layout.sd_layout_list_item, null);

                holder = new Holder();
                holder.StringNameTextView = (TextView) view
                        .findViewById(R.id.item_textView);

                view.setTag(holder);
            } else {
                holder = (Holder) view.getTag();
            }
            String nameText=strModel.get(position);
            holder.StringNameTextView.setText(nameText);
            return view;
        }
    }
    static class Holder
    {
        private TextView StringNameTextView;
    }

    // Defining your own Sectionizer class  which can added to your sectionAdapter
    class ListSectionizer implements Sectionizer<String> {

        @Override
        public String getSectionTitleForItem(String itemName) {
            return itemName.toUpperCase().substring(0, 1);
        }
    }
   
   //Sort your list using comparator for sectioned list
    public static Comparator<String> StringComparator = new Comparator<String>() {

        public int compare(String app1, String app2) {

            String stringName1 = app1;
            String stringName2 = app2;
           
            return stringName1.compareToIgnoreCase(stringName2);
        }
    };

    private List<String> Sorting(List<String> Names) {
        Collections.sort(Names, StringComparator);
        return Names;
    }
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
        // TODO Auto-generated method stub
        int index = sectionAdapter.getIndexForPosition(position);
        sectionAdapter.notifyDataSetChanged();
    }
   
   
}

sd_layout_list.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/list_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
   
</LinearLayout>


sd_layout_list_item.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" >

    <TextView
        android:id="@+id/item_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>


sd_layout_sectioned.xml

<?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"
    android:background="@android:color/holo_blue_dark" >

    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:textSize="15dp"
        android:textStyle="bold" />

</RelativeLayout>


Output :





Source Code : Download this Example Here
















2 comments: