我正在使用Android 2.1平台,当我尝试检索将要使用的联系人的电子邮件时,我的模拟器中有7个联系人但不是按照我保存在联系人List中的顺序。如果我创建了一个新的联系人新的电子邮件,它将到最后。我正在使用此代码。
代码
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>
如何解决?
答案 0 :(得分:3)
在
中的查询中使用 orderByCursor 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 。
答案 1 :(得分:1)
看起来你有两个嵌套的quires,所以不容易实现在查询中提供顺序的最佳解决方案。所以if that is not possible then you can try
可以使用Collections.sort对字符串ArrayList<String>
Collections.sort(arrayList)
答案 2 :(得分: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);
}
}