Android 联系人列表(适配器)

发布于 2024-12-10 06:28:50 字数 4260 浏览 1 评论 0原文

(菜鸟问题) 您好,

我正在使用此代码来制作所有联系人的列表并显示电话号码。

目前我只想显示名称,但我遇到了困难并且出现强制关闭错误。

ActivityManager: 开始: Intent { act=android.intent.action.MAIN 猫=[android.intent.category.LAUNCHER] cmp=com.beta.cphonebook/.CPhonebook }

我使用的代码是这样的:

PhoneList.java:phonelist.xml

package com.beta.cphonebook;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.app.ListActivity;
import android.os.Bundle;

import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import android.content.ContentUris;
import android.net.Uri;
import android.database.Cursor;



public class PhoneList extends ListActivity {

    private SimpleCursorAdapter myAdapter;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ContentResolver cr = getContentResolver();

        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);

        ArrayList<String> nlist = new ArrayList<String>();
        if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts._ID));
        String name = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        nlist.add(name);
        if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
            //Query phone here.  Covered next
            }
            }
    }

        String[] nlistString = new String[nlist.size()];
        nlist.toArray(nlistString);
        Arrays.sort(nlistString);

      int[] mapto = new int[] {R.id.contact_name}; 
//------------------------------------------------------------------      
//This is where I get the error. While trying to use the list adapter      
      ListAdapter mAdapter = new SimpleCursorAdapter(this,R.layout.phonelist,cur,test,mapto);
this.setListAdapter(mAdapter);      
//------------------------------------------------------------------ 

    }

}

:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageView
        android:src="@drawable/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Name: "
            />
            <TextView android:id="@+id/contact_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
            />
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal"      
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Number: "
            />
            <TextView android:id="@+id/contact_number"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
            />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

我知道它应该是非常简单的东西,但在过去的3个小时里,我就是找不到它!!

(noob question)
Hello,

I am using this code to make a list of all contacts and display the phone number.

Currently I just want to display the name, but I am facing difficulties and I get a force close error.

ActivityManager: Starting: Intent { act=android.intent.action.MAIN
cat=[android.intent.category.LAUNCHER]
cmp=com.beta.cphonebook/.CPhonebook }

The code I am using is this:

PhoneList.java:

package com.beta.cphonebook;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.app.ListActivity;
import android.os.Bundle;

import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import android.content.ContentUris;
import android.net.Uri;
import android.database.Cursor;



public class PhoneList extends ListActivity {

    private SimpleCursorAdapter myAdapter;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ContentResolver cr = getContentResolver();

        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);

        ArrayList<String> nlist = new ArrayList<String>();
        if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts._ID));
        String name = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        nlist.add(name);
        if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
            //Query phone here.  Covered next
            }
            }
    }

        String[] nlistString = new String[nlist.size()];
        nlist.toArray(nlistString);
        Arrays.sort(nlistString);

      int[] mapto = new int[] {R.id.contact_name}; 
//------------------------------------------------------------------      
//This is where I get the error. While trying to use the list adapter      
      ListAdapter mAdapter = new SimpleCursorAdapter(this,R.layout.phonelist,cur,test,mapto);
this.setListAdapter(mAdapter);      
//------------------------------------------------------------------ 

    }

}

phonelist.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageView
        android:src="@drawable/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Name: "
            />
            <TextView android:id="@+id/contact_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
            />
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal"      
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Number: "
            />
            <TextView android:id="@+id/contact_number"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
            />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

I know it should be something very simple, but for the past 3 hours, I just can't find it!!

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

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

发布评论

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

评论(1

靑春怀旧 2024-12-17 06:28:50

您可以使用 ArrayAdapter 而不是使用 SimpleCursorAdapter 类...因为您需要对名称进行排序...并且它是在迭代游标之后...
所以你的代码可以像下面这样

 private ArrayAdapter<String> myAdapter;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ContentResolver cr = getContentResolver();

    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

    ArrayList<String> nlist = new ArrayList<String>();

    if (cur.getCount() > 0) {
        if(cur.moveToFirst()) {
            do {
                Log.i("Test", "---------------------count-------------------" +  cur.getCount());
                String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                nlist.add(name);
                if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                    //Query phone here.  Covered next
                    //String num = cur.getString(cur.getColumnIndex(ContactsContract.PhoneLookup.NUMBER));
                }
            }while (cur.moveToNext()) ;
        }
    }

    String[] nlistString = new String[nlist.size()];

    nlist.toArray(nlistString);

    Arrays.sort(nlistString);

     myAdapter = new ArrayAdapter<String>(this, R.layout.phonelist, R.id.contact_name, nlistString);
     this.setListAdapter(myAdapter);
}

}

instead of using the SimpleCursorAdapter class you can use ArrayAdapter....because u need to sort the name...and it is after iterating cursor....
so your code can be like the following

 private ArrayAdapter<String> myAdapter;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ContentResolver cr = getContentResolver();

    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

    ArrayList<String> nlist = new ArrayList<String>();

    if (cur.getCount() > 0) {
        if(cur.moveToFirst()) {
            do {
                Log.i("Test", "---------------------count-------------------" +  cur.getCount());
                String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                nlist.add(name);
                if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                    //Query phone here.  Covered next
                    //String num = cur.getString(cur.getColumnIndex(ContactsContract.PhoneLookup.NUMBER));
                }
            }while (cur.moveToNext()) ;
        }
    }

    String[] nlistString = new String[nlist.size()];

    nlist.toArray(nlistString);

    Arrays.sort(nlistString);

     myAdapter = new ArrayAdapter<String>(this, R.layout.phonelist, R.id.contact_name, nlistString);
     this.setListAdapter(myAdapter);
}

}

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