androidlistviewbaseadaptercustom-lists

Sorted Listview with latter as seprator android


I want to display list of names along with other information for that I have created custom listview using BaseAdapter Now I want to to display list in sorted order of names and add latter in separator as shown below

a--------------------
name: abcd
address: 22nd street
---------------------
name: acdb
address: Link Road
---------------------
name: axyz
address: Shankarnagar
b--------------------
name: bacd
address: Mharajbag
c-------------------
name: cabc
address: Wardha Road

Solution

  • Try this way,hope this will help you to solve your problem.
    
    
    activity_main.xml 
    
    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:divider="@android:color/white"
        android:dividerHeight="1dp"/>
    
    list_item.xml
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/txtHeader"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:textColor="@android:color/black"
            android:textSize="18sp"
            android:padding="5dp"/>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp">
            <TextView
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="Name :"
                android:textSize="16sp"/>
            <TextView
                android:id="@+id/txtName"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textSize="16sp"/>
            </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp">
            <TextView
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="Address :"
                android:textSize="16sp"/>
            <TextView
                android:id="@+id/txtAddress"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textSize="16sp"/>
        </LinearLayout>
    </LinearLayout>
    
    
    MainActivity.java
    
    public class MainActivity extends ListActivity{
    
        private ArrayList<Character> headerList;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            headerList = new ArrayList<Character>();
            ArrayList<HashMap<String,String>> data = new ArrayList<HashMap<String, String>>();
            HashMap<String,String> row1 = new HashMap<String, String>();
            row1.put("name","abcd");
            row1.put("address","1st street");
            data.add(row1);
    
            HashMap<String,String> row2 = new HashMap<String, String>();
            row2.put("name","bcde");
            row2.put("address","2st street");
            data.add(row2);
    
            HashMap<String,String> row3 = new HashMap<String, String>();
            row3.put("name","cdef");
            row3.put("address","3st street");
            data.add(row3);
    
            HashMap<String,String> row4 = new HashMap<String, String>();
            row4.put("name","acde");
            row4.put("address","4st street");
            data.add(row4);
    
            HashMap<String,String> row5 = new HashMap<String, String>();
            row5.put("name","adef");
            row5.put("address","5st street");
            data.add(row5);
    
            HashMap<String,String> row6 = new HashMap<String, String>();
            row6.put("name","bdef");
            row6.put("address","6st street");
            data.add(row6);
    
            HashMap<String,String> row7 = new HashMap<String, String>();
            row7.put("name","pqrs");
            row7.put("address","7st street");
            data.add(row7);
    
            HashMap<String,String> row8 = new HashMap<String, String>();
            row8.put("name","jklm");
            row8.put("address","8st street");
            data.add(row8);
    
            HashMap<String,String> row9 = new HashMap<String, String>();
            row9.put("name","fghi");
            row9.put("address","9st street");
            data.add(row9);
    
            HashMap<String,String> row10 = new HashMap<String, String>();
            row10.put("name","wxyz");
            row10.put("address","10st street");
            data.add(row10);
    
    
            Collections.sort(data,new MapComparator("name"));
    
            for (HashMap<String,String> row :data){
                if(!headerList.contains(row.get("name").toUpperCase().charAt(0))){
                    headerList.add(row.get("name").toUpperCase().charAt(0));
                    row.put("header",String.valueOf(row.get("name").charAt(0)));
                }
            }
    
            getListView().setAdapter(new CustomAdapter(this,data));
    
        }
    
        class MapComparator implements Comparator<Map<String, String>> {
            private final String key;
    
            public MapComparator(String key){
                this.key = key;
            }
    
            public int compare(Map<String, String> first,
                               Map<String, String> second){
                return first.get(key).compareTo(second.get(key));
            }
        }
    
        class CustomAdapter extends BaseAdapter{
    
            Context context;
            ArrayList<HashMap<String,String>> list;
    
            public CustomAdapter(Context context,ArrayList<HashMap<String,String>> list){
                this.context =context;
                this.list = list;
            }
    
            @Override
            public int getCount() {
                return list.size();
            }
    
            @Override
            public Object getItem(int position) {
                return list.get(position);
            }
    
            @Override
            public long getItemId(int position) {
                return position;
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
    
                ViewHolder holder;
                if(convertView==null){
                    holder = new ViewHolder();
                    convertView = LayoutInflater.from(context).inflate(R.layout.list_item,null,false);
                    holder.txtHeader = (TextView) convertView.findViewById(R.id.txtHeader);
                    holder.txtName = (TextView) convertView.findViewById(R.id.txtName);
                    holder.txtAddress = (TextView) convertView.findViewById(R.id.txtAddress);
    
                    convertView.setTag(holder);
                }else{
                    holder = (ViewHolder) convertView.getTag();
                }
                if(list.get(position).containsKey("header")){
                    holder.txtHeader.setText(list.get(position).get("header"));
                    holder.txtHeader.setVisibility(View.VISIBLE);
                }else{
                    holder.txtHeader.setVisibility(View.GONE);
                }
    
                holder.txtName.setText(list.get(position).get("name"));
                holder.txtAddress.setText(list.get(position).get("address"));
    
                return convertView;
            }
    
            class ViewHolder{
                TextView txtHeader;
                TextView txtName;
                TextView txtAddress;
            }
        }
    
    }