[英]How to sort the phone contacts in listview?
I have created a listview based on this tutorial: 我已经基于本教程创建了一个listview:
http://www.vogella.com/articles/AndroidListView/article.html http://www.vogella.com/articles/AndroidListView/article.html
In that link, I've implemented 在那个链接中,我已经实现了
13> Selecting multiple items in the ListView 13>在ListView中选择多个项目
My list view is working correctly. 我的列表视图工作正常。 I've customized the list view by populating it with phone contacts using the following code:
我已使用以下代码通过手机联系人填充列表视图来自定义:
private List<Model> getModel()
{
List<Model> list = new ArrayList<Model>();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
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));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
// This inner cursor is for contacts that have multiple numbers.
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
while (pCur.moveToNext())
{
list.add(get(name));
}
pCur.close();
}
}
}
cur.close();
return list;
}
Now the name of the contacts are getting displayed, but they are not sorted. 现在,将显示联系人的姓名,但不会对其进行排序。 How do I sort this?
我该如何排序?
Kindly help! 请帮忙! Thanks in advance!
提前致谢!
Let's look at this declaration. 我们来看看这个宣言。
List<Model> list = new ArrayList<Model>();
This is an ArrayList
object, paramaterized to type Model
. 这是一个
ArrayList
对象,paramaterized输入Model
。 Now , in the Model
class that you have created, you can simply use the Comparable
interface. 现在,在您创建的
Model
类中,您可以简单地使用Comparable
接口。 To do this you add: 要执行此操作,请添加:
implements Comparable<Model>
to the end of your Model
class header. 到
Model
类标题的末尾。
Then you will be forced to add an implementation for a method called compareTo
. 然后,您将被迫为名为
compareTo
的方法添加实现。 This is simply how you're comparing things. 这就是你比较事物的方式。
When you have added this implementation, you can call Collections.sort()
, and pass it the ArrayList
object, list. 添加此实现后,可以调用
Collections.sort()
,并将其传递给ArrayList
对象列表。 This will sort the list for you. 这将为您排序列表。
A little more In depth I think
我想更深入一点
So, the first thing to do is go to your Model
class, which at the moment has a header that looks like this: 所以,要做的第一件事就是去你的
Model
类,它现在有一个如下所示的标题:
public class Model
What you want to do, is change it to this: 你想要做的是将其改为:
public class Model implements Comparable<Model>
What does this mean?
这是什么意思?
Well this means that you have agreed to provide code for the Comparable
interface. 这意味着您已同意为
Comparable
接口提供代码。 You now need to write a method, compareTo
, in your Model
class. 您现在需要在
Model
类中编写一个方法compareTo
。 Something like: 就像是:
@Override
public int compareTo(Object arg0) {
// At this point, you put your comparison code in.
}
Your comparison Code
你的比较代码
If you wanted to sort them alphabetically, you can use the compareTo
method that exists in a String
object. 如果要按字母顺序对它们进行排序,可以使用
String
对象中存在的compareTo
方法。 You first, need to turn arg0
into a String
object that you can work with: 首先,需要将
arg0
转换为可以使用的String
对象:
String otherName = ((Member)arg0).getName();
Then you can simply return the value of the String compareTo
method: 然后你可以简单地返回String
compareTo
方法的值:
return otherName.compareTo(this.name);
Add Collections.sort(list) before return list. 在返回列表之前添加Collections.sort(列表)。 And check if the class Model implements Comparable.
并检查类Model是否实现了Comparable。 If not, you can use Collections.sort(list,comparator)
如果没有,您可以使用Collections.sort(列表,比较器)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.