开发者

Adding contacts to ListView

I am developing an Android application. I am fetching all contacts one by one but i'm having trouble adding all contacts to 开发者_JAVA百科the Listview

package com.jigar.Contact;

import android.R.integer;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;


import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ContactStatus extends ListActivity 
{
    /** Called when the activity is first created. */
    private  String contactid;
    private String name;
    private String hasphone;
    private String phonenumber;
    private String[] dat=new String[10];
    private int count=0;
    private ListView lv;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ContentResolver cr = getContentResolver();
        //lv=(ListView)findViewById(R.id.listv);
        Cursor cur=cr.query(ContactsContract.Contacts.CONTENT_URI, null,null, null,null);

         while(cur.moveToNext())
         {
             contactid=cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
             name=cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
             dat[count]=name;
             hasphone=cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
             Toast.makeText(this, contactid, 1000).show();
             Toast.makeText(this, name, 1000).show();
            // dat[count]=name;
             count++;
             if(hasphone.equals("1"))
             {
                 hasphone="true";
             }

             if(Boolean.parseBoolean(hasphone))
             {

                 Cursor cphon=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+contactid, null, null);
                 while(cphon.moveToNext())
                 {
                     phonenumber=cphon.getString(cphon.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                     Toast.makeText(this, phonenumber, 1000).show();
                 }

             }

             //lv.setAdapter(new ArrayAdapter<String>(ContactStatus.this,android.R.layout.simple_list_item_1, dat)); 
         }
       //  ArrayAdapter arr=new ArrayAdapter(this,android.R.layout.simple_list_item_1, dat);
         //setListAdapter(arr);
    }
}


Without a better description of the problem, it's difficult to help.

However, it appears that you're loading all the contact data in the onCreate rather than a background thread, so the app is probably killed as unresponsive.

If this is the issue (and even if it isn't), you should move the code to load contacts into a background thread; AsyncTask is the simplest API for this.

If you have a different issue, please provide:

  • A description of what is or isn't happening that you expect.
  • LogCat output
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜