учебник по индексатору разделов Android?

Как создать индексатор категориальных разделов для списка с массивом строк? Я видел примеры индексатора алфавита, но как он реализован для категорий, например. Раздел 1, Раздел 2, Раздел 3...?


person bunbun    schedule 30.07.2011    source источник
comment
Я делаю что-то подобное ЗДЕСЬ stackoverflow .com/questions/10224233/   -  person toobsco42    schedule 25.04.2012
comment
проверьте это   -  person Girish Bhutiya    schedule 07.08.2013


Ответы (2)


Настройте это как адаптер в соответствии с вашими потребностями и настройте представление списка, вот и все, взятое из здесь

public class ContactsAdapter extends BaseAdapter implements SectionIndexer {

Context context;
String[] strings;
String[] sections ;
HashMap<String, Integer> alphaIndexer;


public ContactsAdapter(Context context, String[] strings) {
    this.context = context;
    this.strings = strings;
    alphaIndexer = new HashMap<String, Integer>();
    int size = strings.length;

    for (int x = 0; x < size; x++) {
        String s = strings[x];
        String ch = s.substring(0, 1);
        ch = ch.toUpperCase();
        if (!alphaIndexer.containsKey(ch))
            alphaIndexer.put(ch, x);
    }

    Set<String> sectionLetters = alphaIndexer.keySet();
    ArrayList<String> sectionList = new ArrayList<>(sectionLetters);
    Collections.sort(sectionList);
    sections = new String[sectionList.size()];
    sectionList.toArray(sections);

}

@Override
public int getCount() {
    return strings.length;
}

@Override
public Object getItem(int position) {
    return strings[position];
}

@Override
public long getItemId(int position) {
    return position;
}

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


    ViewHolder holder;
    if (convertView == null) {
        convertView = LayoutInflater.from(context).inflate(R.layout.main, parent, false);
        holder = new ViewHolder();
        holder.text = (TextView) convertView.findViewById(R.id.tv_contact);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.text.setText(strings[position]);

    return convertView;
}

@Override
public Object[] getSections() {

    return sections;
}

@Override
public int getPositionForSection(int sectionIndex) {
    return alphaIndexer.get(sections[sectionIndex]);
}

@Override
public int getSectionForPosition(int position) {
    return 0;
}

static class ViewHolder {
    TextView text;
}
}

В вашем списке

 ContactsAdapter contactsAdapter = new ContactsAdapter(Registration.this, YOUR_Array;

    listview.setAdapter(contactsAdapter);

    listview.setFastScrollEnabled(true);
person Ameen Maheen    schedule 13.08.2015