1

我使用的是 Android 2.1 平台,当我尝试检索联系人的电子邮件时,我的模拟器中有 7 个联系人,它将正常工作,但不是按照我保存在联系人列表中的顺序。如果我使用新电子邮件创建了一个新联系人,它会到最后。我正在使用这段代码。

代码

public class RetrieveData extends Activity {

ListView lvItem;
private Button btnAdd;
String displayName="", emailAddress="", phoneNumber="";
ArrayList<String> contactlist=new ArrayList<String>();
ArrayAdapter<String> itemAdapter;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
   lvItem = (ListView)this.findViewById(R.id.listview);  
   btnAdd = (Button)this.findViewById(R.id.btn);
   itemAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,contactlist);
   lvItem.setAdapter(itemAdapter);
   btnAdd.setOnClickListener(new View.OnClickListener() {
       public void onClick(View v) {
           GetEmails();

       }
   }); 
}

private void GetEmails()
    {


    ContentResolver cr =getContentResolver();
    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    while (cursor.moveToNext()) 
    {       
        String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        Cursor emails = cr.query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = " + id, null, null);
        while (emails.moveToNext()) 
        {               
            emailAddress = emails.getString(emails.getColumnIndex(Email.DATA));
            contactlist.add(emailAddress);
            itemAdapter.notifyDataSetChanged();
        }
        emails.close();

    }
    cursor.close(); 
}
}

xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/listview"
    android:layout_width="fill_parent"
    android:layout_height="288dp"
    android:layout_weight="0.03" >

</ListView>

<Button
    android:id="@+id/btn"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
     android:text="Retrieve"
    android:layout_weight="0.01"  />


</LinearLayout>

如何解决?

4

3 回答 3

3

在查询中使用orderBy

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

Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, columnName + " ASC");

同样用于descending使用DESC

于 2012-06-14T13:33:58.730 回答
1

看起来你有两个嵌套的查询,所以实现在查询中提供 order by 的最佳解决方案看起来并不容易。所以if that is not possible then you can try

可以使用 Collections.sort 对字符串的数组列表进行排序ArrayList<String>

 Collections.sort(arrayList)
于 2012-06-14T13:34:21.107 回答
0

检查下面的测试代码,

package stack.examples;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class RetrieveData extends Activity {

    ListView lvItem;
    private Button btnAdd;
    String displayName="", emailAddress="", phoneNumber="";
    ArrayList<String> contactlist=new ArrayList<String>();
    ArrayAdapter<String> itemAdapter;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       lvItem = (ListView)this.findViewById(R.id.listView);  
       btnAdd = (Button)this.findViewById(R.id.btn);
       itemAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,contactlist);
       lvItem.setAdapter(itemAdapter);
       btnAdd.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v) {
               GetEmails();

           }
       }); 
    }

    private void GetEmails()
        {


        ContentResolver cr =getContentResolver();
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        while (cursor.moveToNext()) 
        {       
            String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            Cursor emails = cr.query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = " + id, null, null);
            while (emails.moveToNext()) 
            {               
                emailAddress = emails.getString(emails.getColumnIndex(Email.DATA));
                contactlist.add(emailAddress);

            }
            emails.close();
            sortList(contactlist);
            itemAdapter.notifyDataSetChanged();

        }
        cursor.close(); 
    }  
    private static void sortList(List<String> aItems){
        Collections.sort(aItems, String.CASE_INSENSITIVE_ORDER);
      }
}
于 2012-06-15T04:58:11.950 回答