1

我有这个customListView,它显示了手机中的所有联系人,每行都有一个复选框以进行多项选择。问题是我无法显示电话号码。只有联系人姓名出现。代码是

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;



public class AddContacts extends Activity implements OnClickListener {

static final String[] FROM = { ContactsContract.CommonDataKinds.Phone.NUMBER,
        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME };
static final int[] TO = { R.id.contact_name, R.id.phone_number };
ListView list;
Cursor cursor;
SimpleCursorAdapter adapter;
Button AddNumber;    
static final String TAG = "In AddContacts";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview);
    AddNumber = (Button) findViewById(R.id.AddNumbers);
    number1 = (EditText) findViewById(R.id.NumberField1);
    number2 = (EditText) findViewById(R.id.NumberField2);
    number3 = (EditText) findViewById(R.id.NumberField3);
    AddNumber.setOnClickListener(this);
    list = (ListView)findViewById(R.id.lists);
    Log.d(TAG, "onCreate");


        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        String[] projection = new String[] { ContactsContract.Contacts._ID,
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME };
        String selection = ContactsContract.CommonDataKinds.Phone.IN_VISIBLE_GROUP
                + " = '1'";
        String[] selectionArgs = null;
        String sortOrder = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
                + " COLLATE LOCALIZED ASC";

        cursor = managedQuery(uri, projection, selection, selectionArgs,
                sortOrder);

        adapter = new SimpleCursorAdapter(this, R.layout.row, cursor, FROM, TO);
        list.setAdapter(adapter);
    }

@Override
public void onClick(View v) {
    Log.d(TAG, "onClicked method");
    }
}

现在程序在运行时因 IllegalArgument 异常而崩溃。当我使用它时,final String[] FROM = { ContactsContract.CommonDataKinds.Phone.NUMBER它引起了问题。日志猫是

    06-22 14:48:54.932: E/AndroidRuntime(11073): FATAL EXCEPTION: main
06-22 14:48:54.932: E/AndroidRuntime(11073): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.s/com.example.s.AddContacts}: java.lang.IllegalArgumentException: column 'data1' does not exist
06-22 14:48:54.932: E/AndroidRuntime(11073):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at android.os.Looper.loop(Looper.java:137)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at android.app.ActivityThread.main(ActivityThread.java:5041)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at java.lang.reflect.Method.invokeNative(Native Method)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at java.lang.reflect.Method.invoke(Method.java:511)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at dalvik.system.NativeStart.main(Native Method)
06-22 14:48:54.932: E/AndroidRuntime(11073): Caused by: java.lang.IllegalArgumentException: column 'data1' does not exist
06-22 14:48:54.932: E/AndroidRuntime(11073):    at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:78)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at android.widget.SimpleCursorAdapter.findColumns(SimpleCursorAdapter.java:333)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:81)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at com.pratik.safetytrack.AddContacts.onCreate(AddContacts.java:64)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at android.app.Activity.performCreate(Activity.java:5104)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-22 14:48:54.932: E/AndroidRuntime(11073):    ... 11 more

请告诉我我做错了什么

谢谢。

4

1 回答 1

0
   USE CURSOR FOR THIS..FOR E.G


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

    pno=new String[phones.getCount()];
   Log.d("rows",Integer.toString(phones.getCount()));

    Log.d("phone","here");
     while (phones.moveToNext()) { 
     String phoneNumber =    

   phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            String   
   name=phones.getString(phones.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            Log.i("Number + Name", phoneNumber + name);
   }        

    if(phones.getCount()>0)
      {
       int i=0;
       if(phones.moveToFirst())
        {
           do
            {
      String phoneNumber1 =   
      phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
      String  
      name1=phones.getString(phones.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
               pno[i]=name1 + "  , " + phoneNumber1;
            i++; 
            }while(phones.moveToNext());
        }

       phones.close();

      }
        ArrayAdapter<String> adapter=new ArrayAdapter<String> 
       (ContactFetch.this,android.R.layout.simple_list_item_1,pno);
       mContactList.setAdapter(adapter);

        }
于 2013-06-22T19:32:54.703 回答