在自定义列表视图的同一行中显示两个联系号码和联系人姓名

时间:2014-05-27 09:00:18

标签: android

任何人都可以告诉我如果联系人有2个或更多电话号码我应该如何将它们放在列表视图的单行中。现在,他们显示在具有相同名称的不同行中,如何在同一行自定义列表视图中显示电话号码和联系人姓名?代码粘贴在

下面
    private ListView listView;
private List<ContactBean> list = new ArrayList<ContactBean>();

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    listView = (ListView) findViewById(R.id.list);
    listView.setOnItemClickListener(this);

    Cursor phones = getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
    while (phones.moveToNext()) {

        String name = phones
                .getString(phones
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

        String phoneNumber = phones
                .getString(phones
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        ContactBean objContact = new ContactBean();
        objContact.setName(name);
        objContact.setPhoneNo(phoneNumber);
        list.add(objContact);

    }
    phones.close();

    ContanctAdapter objAdapter = new ContanctAdapter(
            ContactListActivity.this, R.layout.alluser_row, list);
    listView.setAdapter(objAdapter);

    if (null != list && list.size() != 0) {
        Collections.sort(list, new Comparator<ContactBean>() {

            @Override
            public int compare(ContactBean lhs, ContactBean rhs) {
                return lhs.getName().compareTo(rhs.getName());
            }
        });
        AlertDialog alert = new AlertDialog.Builder(
                ContactListActivity.this).create();
        alert.setTitle("");

这是联系adpater课程。

    public class ContanctAdapter extends ArrayAdapter<ContactBean> {

private Activity activity;
private List<ContactBean> items;
private int row;
private ContactBean objBean;

public ContanctAdapter(Activity act, int row, List<ContactBean> items) {
    super(act, row, items);

    this.activity = act;
    this.row = row;
    this.items = items;

}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View view = convertView;
    ViewHolder holder;
    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(row, null);

        holder = new ViewHolder();
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }

    if ((items == null) || ((position + 1) > items.size()))
        return view;

    objBean = items.get(position);

    holder.tvname = (TextView) view.findViewById(R.id.tvname);
    holder.tvPhoneNo = (TextView) view.findViewById(R.id.tvphone);

    if (holder.tvname != null && null != objBean.getName()
            && objBean.getName().trim().length() > 0) {
        holder.tvname.setText(Html.fromHtml(objBean.getName()));
    }
    if (holder.tvPhoneNo != null && null != objBean.getPhoneNo()
            && objBean.getPhoneNo().trim().length() > 0) {
        holder.tvPhoneNo.setText(Html.fromHtml(objBean.getPhoneNo()));
    }
    return view;
}

public class ViewHolder {
    public TextView tvname, tvPhoneNo;
}

}

我从代码中得到了这个结果但是我想表明,如果联系人有2个或更多的号码,他们必须在一个名字下的同一行显示。但它显示在一个单独的行中。请帮帮我!!!

the output![enter image description here

1 个答案:

答案 0 :(得分:1)

Here if you want to display example 50 Contact no in 25 row then first set items.size()/2 in getcount here also check odd even sequence if 51 contact no present then 26 row needed so
if(items.size()%2==0){
size=items.size()/2;
else{
size=(items.size()/2)+1;
}

and then in every row take two textview for contact name and two textview for contact no and set it

objBean = items.get(position);
objBean2 = items.get(position+1);

textview1.settext(objBean.getName());
textview2.settext(objBean.getPhoneNo());

textview3.settext(objBean2.getName());
textview4.settext(objBean2.getPhoneNo());

thats it..