组适配器项

时间:2015-02-20 11:23:34

标签: android listview adapter android-arrayadapter

我需要为地址簿创建一个自定义适配器,在自定义布局中按名字字母对它们进行分组。我怎么能得到这样的结果? enter image description here

我创建了Hashmap<String firstLetter,Arraylist<Contacts>>,但我不知道如何将其传递给适配器以使列表的每个单元可点击。

我到目前为止找到的最佳解决方案是为我的HashMap的每个字母充气尽可能多的ListView,并将适配器设置为它

for (Map.Entry<Character, ArrayList<RlyJiffyContact>> entry : contactLinkedHashMap.entrySet()) {
        Character key = entry.getKey();
        ArrayList<RlyJiffyContact> value = entry.getValue();
        if (value != null && !value.isEmpty()) {
            LinearLayoutWithLabel llbel = new LinearLayoutWithLabel(getContext());
            llbel.setOrientation(LinearLayout.VERTICAL);
            llbel.setPadding(R.dimen.default_margin_left, R.dimen.default_margin_top, R.dimen.default_margin_right, R.dimen.default_margin_bottom);
            llbel.setText(key.toString().toUpperCase());
            ListView list = new ListView(getContext());
            list.setAdapter(new RlyJiffyContactsAdapter(getContext(), value));
            llbel.addView(list);
            linear.addView(llbel);
        }

和适配器

public class MYADAPTER extends ArrayAdapter<MYELEMENT> {

LayoutInflater vi;
private Context               context;
private List<RlyJiffyContact> contacts;
public MYADAPTER(Context context, List<MYELEMENT> contacts) {
    super(context, 0, contacts);
    vi = LayoutInflater.from(context);
    this.context = context;
    this.contacts = contacts;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolderItem holderItem;
    View v = convertView;
    if (convertView == null) {
        holderItem = new ViewHolderItem();

        convertView = vi.inflate(R.layout.reply_jiffy_contact_line, null);
        holderItem.contactNumber = (TextView) convertView.findViewById(R.id.contact_number);
        holderItem.displayName = (TextView) convertView.findViewById(R.id.contact_display_name);
        holderItem.preferedImage = (ImageView) convertView.findViewById(R.id.icon_prefered);
        convertView.setTag(holderItem);
    } else {
        holderItem = (ViewHolderItem) convertView.getTag();
    }
    RlyJiffyContact item = getItem(position);
    holderItem.contactNumber.setText(item.getPhoneNumber());
    holderItem.displayName.setText(item.getName() + " " + item.getSurname());
    if (item.isPrefered()) {
        holderItem.preferedImage.setVisibility(View.INVISIBLE);
    } else {
        holderItem.preferedImage.setVisibility(View.VISIBLE);
    }
    convertView.setOnClickListener(new ContactPhoneListener(item.getPhoneNumber()));
    return convertView;
}
static class ViewHolderItem {
    TextView  contactNumber;
    TextView  displayName;
    ImageView preferedImage;

}
private class ContactPhoneListener implements View.OnClickListener {
    private String number;
    public ContactPhoneListener(String number) {
        this.number = number;
    }
    @Override
    public void onClick(View v) {
        Intent i = new Intent(getContext(), FIRSTACTIVITY.class);
        i.putExtra(Constants.CONTACT_IDENTIFIER, number);
        i.setAction(Constants.SENDING_CONTACT);
        context.startActivity(i);
    }
}

但结果是我只能看到每个字母的第一个元素( 实施例:

我的联系人:

  • Anne a
  • Anne B
  • anthony c

  • Bob a

  • Bob b
  • Bob c

我在列表中看到的内容

  1. Anne a
  2. Bob a
  3. 谢谢!

2 个答案:

答案 0 :(得分:0)

要实现此类型的列表: 使用Char或String类型的ArrayList, 在getView()方法中,通过name.chatAt(0).toUpperCase()获取名称的第一个字母; in if条件如果数组包含名称的第一个字符,则只返回getview方法的普通视图,否则在数组中添加字符并创建一个简单的textview并在textview上设置字符并从getview方法返回textview

试试这个..

答案 1 :(得分:0)

听起来你需要一个Rolodex适配器,其中有两个变体,here。还有那里有大量示例代码的演示应用程序。顾名思义,它的设计可以处理数据自身排列的情况。在您的情况下,每个名称的第一个字母。它会自动为您分配适当分组下的名称列表。它适用于ExpandableListView,允许您指定子项和分组是否可单击。默认是可点击的。如果您不需要过滤功能,我建议您专门使用NFRolodexArrayAdapter。这是一个简单的例子:

活动/片段

//There's no need to pre-compute what the groupings are for each contact. Just pass in your List of contacts as is. 
PeopleAdapter adapter = new PeopleAdapter (this, mContacts);
mExpandableListView.setAdapter(adapter);

<强>适配器

class PeopleAdapter extends NFRolodexArrayAdapter<String, Contacts> {

    PeopleAdapter(Context activity, Collection<Contacts> items) {
        super(activity, items);
    }

    @Override
    public String createGroupFor(Contacts childItem) {
        //This is how the adapter determines what the groupins are and what contact belongs to it
        return childItem.getFirstLetterOfFirstName();
    }

    @Override
    public View getChildView(LayoutInflater inflater, int groupPosition, int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {
        //Inflate your view

        //Gets the Contact data for this view
        Contacts contact = getChild(groupPosition, childPosition);

        //Fill view with contact data
    }

    @Override
    public View getGroupView(LayoutInflater inflater, int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
        //Inflate your group view

        //Gets the Letter for this view
        String letter = getGroup(groupPosition);

        //Fill view with letter
    }
}