sort_list.xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<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>
create 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>
Then below is MainActivity.java
package com.examplemediaplayer.act;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
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("Android");
stringList.add("Iphone");
stringList.add("ipad");
stringList.add("Windows");
stringList.add("Blackberry");
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();
}
}
No comments:
Post a Comment
Comments