获取android中HTC desire的联系人列表

获取android中HTC desire的联系人列表,android,Android,我想在列表视图中获取联系人列表。我可以用这个代码来演示 private ContactList getContacts() { ContactList contactList = new ContactList(); Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; String[] projection = new String[] { Co

我想在列表视图中获取联系人列表。我可以用这个代码来演示

private ContactList getContacts() {
        ContactList contactList = new ContactList();
        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        String[] projection = new String[] {
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER };

        Cursor people = getContentResolver().query(uri, projection, null, null,
                null);

        int indexName = people
                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        int indexNumber = people
                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

        people.moveToFirst();
        do {
            Contact c = new Contact();
            String name = people.getString(indexName);
            String number = people.getString(indexNumber);
            Log.d("number", "" + number);
            c.setDisplayName(name);
            c.set_phoneNumber(number);
            contactList.addContact(c);
        } while (people.moveToNext());
        people.close();
        return contactList;
    }
但当我在HTC desire c上尝试这段代码时,它无法工作。 任何人都能告诉我为什么? 实际问题是什么。以及如何解决这个问题

import java.lang.reflect.Array;
 import java.lang.reflect.Array;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.List;

 import android.R.bool;
 import android.app.Activity;
 import android.app.AlertDialog;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.database.Cursor;
 import android.net.Uri;
 import android.os.Bundle;
 import android.provider.Contacts.Phones;
 import android.provider.ContactsContract;
 import android.text.Html;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.AdapterView;
 import android.widget.ArrayAdapter;
 import android.widget.ListView;
 import android.widget.SimpleCursorAdapter;
 import android.widget.TextView;
 import android.widget.Toast;
 import android.widget.AdapterView.OnItemClickListener;

 import com.google.ads.AdRequest;
 import com.google.ads.AdView;

 public class button extends Activity implements OnItemClickListener {

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

@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(button.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(button.this).create();
        alert.setTitle("");

        alert.setMessage(list.size() + " Contact Found!!!");

        alert.setButton("OK", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        alert.show();

    } else {
        showToast("No Contact Found!!!");
    }
}

protected boolean isNullValue(String input)
{
    if(input==null)
    {
        return true;            
    }
    else
        return false;
}

private void showToast(String msg) {
    Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}

@Override
public void onItemClick(AdapterView<?> listview, View v, int position,
        long id) {
    ContactBean bean = (ContactBean) listview.getItemAtPosition(position);
    showCallDialog(bean.getName(), bean.getPhoneNo());
}

private void showCallDialog(String name, final String phoneNo) {
    AlertDialog alert = new AlertDialog.Builder(button.this).create();
    alert.setTitle("Call?");

    alert.setMessage("Are you sure want to call " + name + " ?");

    alert.setButton("No", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    alert.setButton2("Yes", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            String phoneNumber = "tel:" + phoneNo;
            Intent intent = new Intent(Intent.ACTION_CALL, Uri
                    .parse(phoneNumber));
            startActivity(intent);
        }
    });
    alert.show();
}

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

public class ContactBean {
    private String name;
    private String phoneNo;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhoneNo() {
        return phoneNo;
    }

    public void setPhoneNo(String phoneNo) {
        this.phoneNo = phoneNo;
    }

}
 }
导入java.util.ArrayList; 导入java.util.Collections; 导入java.util.Comparator; 导入java.util.List; 导入android.R.bool; 导入android.app.Activity; 导入android.app.AlertDialog; 导入android.content.Context; 导入android.content.DialogInterface; 导入android.content.Intent; 导入android.database.Cursor; 导入android.net.Uri; 导入android.os.Bundle; 导入android.provider.Contacts.Phones; 导入android.provider.contacts合同; 导入android.text.Html; 导入android.view.LayoutInflater; 导入android.view.view; 导入android.view.ViewGroup; 导入android.widget.AdapterView; 导入android.widget.ArrayAdapter; 导入android.widget.ListView; 导入android.widget.SimpleCursorAdapter; 导入android.widget.TextView; 导入android.widget.Toast; 导入android.widget.AdapterView.OnItemClickListener; 导入com.google.ads.AdRequest; 导入com.google.ads.AdView; “公共类”按钮扩展活动实现McClickListener{ 私有列表视图列表视图; 私有列表=新的ArrayList(); @凌驾 创建时受保护的void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); listView=(listView)findViewById(R.id.list); setOnItemClickListener(this); 游标phones=getContentResolver().query(ContactsContract.CommonDataTypes.Phone.CONTENT\u URI,null,null, 空,空); while(phones.moveToNext()){ 字符串名称=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataTypes.Phone.DISPLAY_name)); String phoneNumber=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataTypes.Phone.NUMBER)); ContactBean objContact=新ContactBean(); objContact.setName(名称); objContact.setPhoneNo(电话号码); list.add(objContact); } 电话。关闭(); ContanctAdapter objAdapter=新ContanctAdapter(按钮。此, R.layout.alluser_行,列表); setAdapter(objAdapter); if(null!=list&&list.size()!=0){ Collections.sort(list,newcomparator(){ @凌驾 公共int比较(ContactBean lhs、ContactBean rhs){ 返回lhs.getName().compareTo(rhs.getName()); } }); AlertDialog alert=新建AlertDialog.Builder(button.this.create(); 警报。设置标题(“”); alert.setMessage(list.size()+“找到联系人!!!”; alert.setButton(“确定”,新建DialogInterface.OnClickListener(){ @凌驾 public void onClick(DialogInterface dialog,int which){ dialog.dismise(); } }); alert.show(); }否则{ showToast(“未找到联系人!!!”; } } 受保护的布尔值isNullValue(字符串输入) { 如果(输入==null) { 返回true; } 其他的 返回false; } 私有void showtoos(字符串msg){ Toast.makeText(this,msg,Toast.LENGTH_SHORT).show(); } @凌驾 公共单击(AdapterView列表视图、视图v、内部位置、, 长id){ ContactBean=(ContactBean)listview.getItemAtPosition(position); showCallDialog(bean.getName(),bean.getPhoneNo()); } 私有void showCallDialog(字符串名称,最终字符串phoneNo){ AlertDialog alert=新建AlertDialog.Builder(button.this.create(); 警报。设置标题(“呼叫”); setMessage(“您确定要调用“+name+”?”); alert.setButton(“否”,新建DialogInterface.OnClickListener(){ @凌驾 public void onClick(DialogInterface dialog,int which){ dialog.dismise(); } }); alert.setButton2(“是”,新的DialogInterface.OnClickListener(){ @凌驾 public void onClick(DialogInterface dialog,int which){ 字符串phoneNumber=“电话:”+phoneNo; 意图=新意图(Intent.ACTION\u调用,Uri .parse(phoneNumber)); 星触觉(意向); } }); alert.show(); } 公共类ContanctAdapter扩展了ArrayAdapter{ 私人活动; 私人清单项目; 私人int row; 私有联系人bean objBean; 公共ContanctAdapter(活动法、整数行、列表项){ 超级(行动、行、项目); 这个活动=行动; this.row=行; 这个项目=项目; } @凌驾 公共视图getView(最终整型位置,视图转换视图, 视图组(父级){ 视图=转换视图; 视窗座; 如果(视图==null){ LayoutInflater充气器=(LayoutInflater)activity.getSystemService(Context.LAYOUT\u充气器\u服务); 视图=充气机。充气(行,空); holder=新的ViewHolder(); 视图.设置标签(支架); }否则{ holder=(ViewHolder)view.getTag(); } if((items==null)| |((position+1)>items.size()) 返回视图; objBean=items.get(位置); 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()); } 返回视图; } } 公共类视图持有者{ 公共文本视图tvname,tvPhoneNo; } 公众的