1

I have a String array which consists of contacts of people and I get this data from webservice. I have used custom array adapter to show the list. I have added a EditText at top for filtering, it is working fine. Now I want alphabets at right side of the UI so that user can also filter using the alphabets. How can I achieve this?

This is my array adapter.

public class EntryAdapter extends ArrayAdapter<Item> implements Filterable,SectionIndexer {
    HashMap<String, Integer> alphaIndexer;
    String[] sections;
    private Context context;
    private ArrayList<Item> items;
    private ArrayList<Item> fitems;
    private LayoutInflater vi;
    private contact contact;
     private ItemsFilter mFilter;
    public EntryAdapter(Context context,ArrayList<Item> items) {
        super(context,0, items);
        this.context = context;
        this.contact=(contact) context;
        this.items = items;
        vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);alphaIndexer = new HashMap<String, Integer>();
        int size = items.size();

        for (int x = 0; x < size; x++) {
            Item s = items.get(x);
            String q=s.toString();
            // get the first letter of the store
            String ch = q.substring(0, 1);
            // convert to uppercase otherwise lowercase a -z will be sorted
            // after upper A-Z
            ch = ch.toUpperCase();
            // put only if the key does not exist
            if (!alphaIndexer.containsKey(ch))
                alphaIndexer.put(ch, x);
        }

        Set<String> sectionLetters = alphaIndexer.keySet();
        // create a list from the set to sort
        ArrayList<String> sectionList = new ArrayList<String>(
                sectionLetters);
        Collections.sort(sectionList);
        sections = new String[sectionList.size()];
        sections = sectionList.toArray(sections);
    }

@Override
public int getCount() {
    // TODO Auto-generated method stub

    return items.size();
}

@Override
public Item getItem(int position) {
    // TODO Auto-generated method stub
    return super.getItem(position);
}

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        final Item i = items.get(position);

        if (i != null) {
            if(i.isSection()){
                SectionItem si = (SectionItem)i;
                v = vi.inflate(R.layout.list_item_section, null);

                v.setOnClickListener(null);
                v.setOnLongClickListener(null);
                v.setLongClickable(false);

                final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
                sectionView.setText(si.getTitle());

            }else{
                EntryItem ei = (EntryItem)i;
                v = vi.inflate(R.layout.entrylist, null);
                final TextView title = (TextView)v.findViewById(R.id.list_item_entry_title);
                final TextView subtitle = (TextView)v.findViewById(R.id.list_item_entry_summary);


                if (title != null) 
                    title.setText(ei.title);
                if(subtitle != null)
                    subtitle.setText(ei.subtitle);
            }
        }
        return v;
    }

     public Filter getFilter() {
            if (mFilter == null) {
                mFilter = new ItemsFilter();
            }
            return mFilter;

        }

     private class ItemsFilter extends Filter{

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();

            if (constraint == null || constraint.length() == 0){
                results.values = items;
                results.count = items.size();

            }
            else{

                 ArrayList<Item> itemsList = new ArrayList<Item>();

                 for (Item i : items){

                     if (i.toString().toUpperCase().startsWith(constraint.toString().toUpperCase()))
                            itemsList.add(i);
                 }
                 results.values = itemsList;
                    results.count = itemsList.size();
            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {
             if (results.count == 0){

                 notifyDataSetInvalidated();
             }
             else{

                 ArrayList<Item> lst = (ArrayList<Item>)results.values;
                 ArrayList<Item> itemsList = new ArrayList<Item>(lst);
                 //this.items=mItems;
                items =itemsList;              
                notifyDataSetChanged();
             }

        }


     }

    @Override
    public int getPositionForSection(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int getSectionForPosition(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Object[] getSections() {
        // TODO Auto-generated method stub
        return null;
    }

}
user2699728
  • 377
  • 2
  • 8
  • 25

1 Answers1

1

One thing you can do is to enable the enable Fast Scroll of your ListView. Here is the documentation for that public void setFastScrollEnabled (boolean enabled). You can also follow the link to get that kind of requirements. Android ListView with an alphabet scroller. Look at this link Create easy alphabetical scrolling in ListView?

Community
  • 1
  • 1