2014-01-14 81 views
1

我有一个由人的联系人组成的字符串数组,我从webservice获取这些数据。我已经使用自定义数组适配器来显示列表。我在顶部添加了一个EditText进行过滤,它工作正常。现在我想在UI的右侧添加字母,以便用户也可以使用字母进行过滤。我怎样才能做到这一点?创建一个与默认联系人设计android相同的用户界面?

这是我的阵列适配器。

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

} 

回答

相关问题