0

我正在使用 Android Eclair 2.1 平台。

这段代码的工作原理是,它将访问模拟器中的所有联系人,并将像这样显示在每个联系人的列表视图中

联系人姓名、联系电话、电子邮件

这段代码有两个问题是

  1. 我确实运行了该程序,之后我创建了一个新联系人,它不会按字母顺序排列

[例如:当我创建一个从 B 开始的名字时,它不会去 A,而是去最后一个地方]

2.如果联系人没有电子邮件或号码,它将收到前一个联系人的电子邮件或号码

这是代码

public class GetAllDatas 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.lvitems); 
btnAdd = (Button)this.findViewById(R.id.btnAddItem);

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) {
    readContacts();
}
});

 } 

private void readContacts()
{
ContentResolver cr =getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null,    null);
while (cursor.moveToNext()) 
{
displayName =Cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)    );       
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));
    break;
}
emails.close();
    if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PH    ONE_NUMBER))) > 0)
{
    Cursor pCur =cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDat     aKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null);
    while (pCur.moveToNext()) 
    {
         phoneNumber =pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        break;  
    }
    pCur.close();
}

//  To display the Details
    contactlist.add(displayName+", "+phoneNumber+", "+ emailAddress+"\n");
    itemAdapter.notifyDataSetChanged();
 }
cursor.close(); 
 }
 }

如果有的话,有什么链接或网站可以参考和研究解决这个问题吗?把链接发给我?

4

3 回答 3

0

当您查询数据库时,您缺少 orderBy:

public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)

将最后一个 null 替换为要排序的列:

Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null,    null);
于 2012-06-13T14:02:11.423 回答
0

query方法的最后一个参数需要排序顺序。

请检查文档query (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)

sortOrder 如何对行进行排序,格式化为 SQL ORDER BY 子句(不包括 ORDER BY 本身)。传递 null 将使用默认排序顺序,可能是无序的。

于 2012-06-13T14:02:34.473 回答
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 GetAllDatas 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_items);  
       btnAdd = (Button)this.findViewById(R.id.btnAddItem);
       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) {
               readContacts();
           }
       });
    }

    private void readContacts()
    {
        ContentResolver cr =getContentResolver();
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        while (cursor.moveToNext()) 
        {
            displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));       
            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));
                break;
            }
            emails.close();
            if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
            {
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null);
                while (pCur.moveToNext()) 
                {
                     phoneNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    break;
                }
                pCur.close();
            }
                contactlist.add(displayName+","+phoneNumber+","+ emailAddress);

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

    private static void sortList(List<String> aItems){
        Collections.sort(aItems, String.CASE_INSENSITIVE_ORDER);
      }
}

要按字母顺序对 ArrayList 进行排序,您可以使用

Collections.sort(aItems, String.CASE_INSENSITIVE_ORDER);

快乐编码!

于 2012-06-15T04:34:39.870 回答