在复选框中显示联系人和电话号码

发布于 2025-01-08 09:23:57 字数 3337 浏览 0 评论 0原文

我想以这种方式在复选框中显示联系人姓名及其电话号码:“ContactName : Phone number” 但我在 logcat "java.lang.IllegalArgumentException: Invalid column data1" 中发现此错误

这是我使用的代码 ----->

package wael.ilahi.pfe;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.zip.Inflater;

import android.app.Activity;
import android.app.ListActivity;
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.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
 ...


public class SelectFriends extends Activity implements  OnClickListener {
    public static final String TAG = "ContactManager";
    private Button bSave;
    private ListView lv;
    private boolean mShowInvisible;
    CheckBox ch;
    String s;
    int k=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        Log.v(TAG, "Activity State: onCreate()");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);
        bSave = (Button) findViewById(R.id.addContactButton);
        lv = (ListView) findViewById(R.id.contactList); 
        populateContactList();

        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,final                 
                int position, long id) {
            ch = (CheckBox) view.findViewById(R.id.checkBox);
            ch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(),
                         "Click ListItem Number " + position, Toast.LENGTH_LONG).show();
                    }
                });     
            }
        });

        bSave.setOnClickListener(this);
    }

    private void populateContactList() {
        // Build adapter with contact entries
        Cursor cursor = getContacts();
        String[] fields = new String[] {
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME +":"+ 
            ContactsContract.CommonDataKinds.Phone.NUMBER};

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,  
        R.layout.contact_entry, cursor,fields, new int[] {R.id.checkBox});
        lv.setAdapter(adapter);
    }

    private Cursor getContacts(){
        // Run query
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        String[] projection = new String[] {

             //ContactsContract.Contacts.DISPLAY_NAME, 
             ContactsContract.CommonDataKinds.Phone.NUMBER };
        String selection = null;
        String[] selectionArgs = null;
        String sortOrder =null;

        return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
    }

    @Override
    public void onClick(View arg0) {
         // TODO Auto-generated method stub
    }
} 

I want to display the contact name and it's phone number in a check box in this manner: "ContactName : Phone number"
But I find this error in the logcat "java.lang.IllegalArgumentException: Invalid column data1"

Here is the code I work with ----->

package wael.ilahi.pfe;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.zip.Inflater;

import android.app.Activity;
import android.app.ListActivity;
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.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
 ...


public class SelectFriends extends Activity implements  OnClickListener {
    public static final String TAG = "ContactManager";
    private Button bSave;
    private ListView lv;
    private boolean mShowInvisible;
    CheckBox ch;
    String s;
    int k=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        Log.v(TAG, "Activity State: onCreate()");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);
        bSave = (Button) findViewById(R.id.addContactButton);
        lv = (ListView) findViewById(R.id.contactList); 
        populateContactList();

        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,final                 
                int position, long id) {
            ch = (CheckBox) view.findViewById(R.id.checkBox);
            ch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(),
                         "Click ListItem Number " + position, Toast.LENGTH_LONG).show();
                    }
                });     
            }
        });

        bSave.setOnClickListener(this);
    }

    private void populateContactList() {
        // Build adapter with contact entries
        Cursor cursor = getContacts();
        String[] fields = new String[] {
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME +":"+ 
            ContactsContract.CommonDataKinds.Phone.NUMBER};

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,  
        R.layout.contact_entry, cursor,fields, new int[] {R.id.checkBox});
        lv.setAdapter(adapter);
    }

    private Cursor getContacts(){
        // Run query
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        String[] projection = new String[] {

             //ContactsContract.Contacts.DISPLAY_NAME, 
             ContactsContract.CommonDataKinds.Phone.NUMBER };
        String selection = null;
        String[] selectionArgs = null;
        String sortOrder =null;

        return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
    }

    @Override
    public void onClick(View arg0) {
         // TODO Auto-generated method stub
    }
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东北女汉子 2025-01-15 09:23:57

看起来您正在尝试查询“联系人”表并从中返回电话号码。

不幸的是,这不是联系人提供程序的工作方式。总之:
联系人提供程序具有三个表:联系人、RawContacts 和数据。每个联系人行
指向一个或多个 RawContacts 行。每个 RawContact 行都指向一个或多个数据行。

人员作为原始联系人存储到联系人提供程序中。提供者自己创建
联系行。自动选择一个原始联系人姓名作为联系人行的 DISPLAY_NAME_PRIMARY。

数据表包含原始联系人的详细信息。这些行是通用的。它们中的每一个都具有相同的列名称,您可以在 ContactsContract.Data 中看到这一点。要选择特定类型的详细信息(例如电话号码),您必须搜索 MIME 类型为 ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE 的数据行。然后,您可以将该行中的电话号码引用为
ContactsContract.CommonDataKinds.Phone.PHONE_NUMBER;该常数的值为
“数据1”。也就是说,CommonDataKinds 类中的常量是列名称的别名
DATA1-DATA15 在 ContactsContract.Data 中定义。别名使您更容易理解数据行的语义。

有一个简单的方法可以实现您的目标:使用 ContactsContract.Contacts.Entity 构造。这将返回一个包含联系人的所有数据和 RawContacts 行的游标。如果将选择参数设置为 ContactsContract.Data.MIME_TYPE = ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,则您将仅检索电话号码。

注意:原始联系人的显示名称存储在数据表中,作为
ContactsContract.CommonDataKinds.StructuredName 行。

It looks like your trying to query the Contacts table and return a phone number from it.

Unfortunately, that's not how the Contacts Provider works. In summary:
The Contacts Provider has three tables: Contacts, RawContacts, and Data. Each Contact row
points to one or more RawContacts rows. Each RawContact row points to one or more Data rows.

People are stored into the Contacts Provider as raw contacts. The provider itself creates
Contact rows. One raw contact name is chosen automatically as the DISPLAY_NAME_PRIMARY of the Contact row.

The Data table contains the details for a raw contact. The rows are generic. Every one of them has the same column names, which you can see in ContactsContract.Data. To choose a particular type of detail, such as a phone number, you have to search for the Data row that has a MIME type of ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE. You can then refer to the phone number in that row as
ContactsContract.CommonDataKinds.Phone.PHONE_NUMBER; the value of this constant is
"data1". That is, constants in the CommonDataKinds classes are aliases to the column names
DATA1-DATA15 defined in ContactsContract.Data. The aliases make it easier to understand the semantics of a Data row.

There's a simple way to accomplish your goal: use the ContactsContract.Contacts.Entity construct. This returns a Cursor containing all the Data and RawContacts rows for a Contact. If you set your selection arguments to ContactsContract.Data.MIME_TYPE = ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE, you'll only retrieve phone numbers.

Notes: A raw contact's display name is stored in the Data table, as a
ContactsContract.CommonDataKinds.StructuredName row.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文