Thursday, 17 January 2013

Sorting List of Data in Android using Comparator


Project Structure for Sorting List of Data :





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

    <Button
        android:id="@+id/desc_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="48dp"
        android:layout_toRightOf="@+id/asc_button"
        android:text="DESC" />

    <ListView
        android:id="@+id/name_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/desc_button" >
    </ListView>

    <Button
        android:id="@+id/asc_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/name_list"
        android:layout_marginLeft="54dp"
        android:text="ASC" />

</RelativeLayout>


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

    <TextView
        android:id="@+id/name_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

MainActivity.java

package com.example.sorting_listview_data_asc_desc_example;

public class MainActivity extends Activity implements OnClickListener {

    private Button mAscButton;
    private Button mDescButton;
    private ListView mNameListView;
   
    private List<String> stringList;
    private StringAdapter stringAdapter;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sort_list);
       
        mAscButton = (Button) findViewById(R.id.asc_button);
        mDescButton = (Button) findViewById(R.id.desc_button);
        mNameListView = (ListView) findViewById(R.id.name_list);
       
        stringList = new ArrayList<String>();
        stringList.add("Rajesh");
        stringList.add("Ramanan");
        stringList.add("Kannan");
        stringList.add("Mahesh");
        stringList.add("Moses");
        stringList.add("Kuberan");
       
        stringAdapter = new StringAdapter(MainActivity.this, R.layout.sort_list_item, stringList);
        mNameListView.setAdapter(stringAdapter);
       
        mAscButton.setOnClickListener(this);
        mDescButton.setOnClickListener(this);
       
       
    }
    // Comparator for Ascending Order
    public static Comparator<String> StringAscComparator = new Comparator<String>() {

        public int compare(String app1, String app2) {

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

    //Comparator for Descending Order
    public static Comparator<String> StringDescComparator = new Comparator<String>() {

        public int compare(String app1, String app2) {

            String stringName1 = app1;
            String stringName2 = app2;
           
            return stringName2.compareToIgnoreCase(stringName1);
        }
    };
   // 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(MainActivity.this,
                        R.layout.sort_list_item, null);

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

                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;
    }

    @Override
    public void onClick(View v) {

        switch(v.getId()) {
        case R.id.asc_button :
            Collections.sort(stringList, StringAscComparator);
            Toast.makeText(MainActivity.this, "Sorting in Ascending Order", Toast.LENGTH_LONG).show();
            break;
        case R.id.desc_button :
            Collections.sort(stringList, StringDescComparator);
            Toast.makeText(MainActivity.this, "Sorting in Descending Order", Toast.LENGTH_LONG).show();
            break;
        }
        stringAdapter.notifyDataSetChanged();
       
    }
}

Output :



Source Code :  Download this Example Here
















1 comment: