6

我有一个应该替换 Android 原生联系人的 Android 应用程序。

我想为用户添加基于字符约束搜索用户的可能性。

例如:

这是我的联系人表:

id  firstName  lastName
1.    Smith      Jean
2.    allen      carr
3.               zetter
4.    john       Stewart
5.    Smith      Allen
6.    Smith      Davey
7.               Smitten
8.    barney     saltzberg

如果用户输入字符's',我想给他所有在他们的名字或姓氏中以“s”开头的联系人,先按名字排序,然后按姓氏排序。从我想要得到的结果之前的表中是:

id  firstName  lastName
1.    Smith      Allen
2.    Smith      Davey
3.    Smith      Jean
4.    barney     saltzberg
4.               Smitten
5.    john       Stewart

更新: 问题是当名字等于 NULL 时,排序不起作用,并且该行显示在它应该之前。我尝试了marcin的答案,它给了我错误的结果。

我尝试了以下方法:

  1. 字符串选择 = PeopleDataBase.COLUMN_FIRST_NAME + " LIKE '" + 约束 + "%' OR " + PeopleDataBase.COLUMN_LAST_NAME + " LIKE '" + 约束 + "%'";

    光标 cur = db.query(PeopleDataBase.TABLE_PEOPLE, null, selection, null, null, null, null);

我想通过两个不同的查询来实现这一点,一个用于名字,一个用于姓氏,然后将它们连接到一个游标,但我确信有更好的解决方案。

更新:我也尝试按以下方式排序,但没有成功。

   Cursor cur = db.query(PeopleDataBase.TABLE_PEOPLE, null, selection, null, null, null, PeopleDataBase.COLUMN_FIRST_NAME + "," + PeopleDataBase.COLUMN_LAST_NAME);

你有更好的解决方案吗?

4

5 回答 5

2

你试过ORDER BY吗?:

String selection =  
PeopleDataBase.COLUMN_FIRST_NAME + " LIKE '" + constraint + "%' OR " +  
PeopleDataBase.COLUMN_LAST_NAME + " LIKE '" + constraint + "%'" ORDER BY " + 
PeopleDataBase.COLUMN_FIRST_NAME + " ASC, " + PeopleDataBase.COLUMN_LAST_NAME + " ASC";
于 2013-05-27T08:06:54.783 回答
2

TL;博士

我认为这不容易。考虑放宽你的要求。

自定义排序规则

如果在 Android 中安装自定义排序序列很容易,您只需要实现一个null等于任何值的排序序列。但是,无法使用 sqlite Android Java API 安装排序规则序列。

替代问题

你能改变你的问题吗?

  • 是否可以接受空的名字首先或最后排序?那么查询可能类似于

    select * from contacts
      where firstname like 's%' or lastname like 's%'
      order by
        case when firstname like 's%' then 0 else 1 end,
        firstname collate nocase,
        lastname collate nocase;
    

    这里第一个order by表达式按名字是否匹配,然后按名字和姓氏排序。

  • 还有什么能让问题更容易解决的吗?

测试

首先,让我们准备我们的 sqlite3 沙箱:

sqlite> create table contacts(id integer primary key, firstname text, lastname text);
sqlite> insert into contacts values(1,'Smith','Jean');
sqlite> insert into contacts values(2,'allen','carr');
sqlite> insert into contacts values(3,null,'zetter');
sqlite> insert into contacts values(4,'john','Stewart');
sqlite> insert into contacts values(5,'Smith','Allen');
sqlite> insert into contacts values(6,'Smith','Davey');
sqlite> insert into contacts values(7,null,'Smitten');
sqlite> insert into contacts values(8,'barney','saltzberg');
sqlite> .head on

请注意,您只需按姓氏排序即可获得所需的示例结果:

sqlite> select * from contacts where firstname like 's%' or lastname like 's%' order by lastname collate nocase;
id|firstname|lastname
5|Smith|Allen
6|Smith|Davey
1|Smith|Jean
8|barney|saltzberg
7||Smitten
4|john|Stewart

显然这不是您想要的,所以让我们在沙箱中添加更多测试数据:

sqlite> insert into contacts values(9,'see','this');
sqlite> select * from contacts where firstname like 's%' or lastname like 's%' order by lastname collate nocase;
id|firstname|lastname
5|Smith|Allen
6|Smith|Davey
1|Smith|Jean
8|barney|saltzberg
7||Smitten
4|john|Stewart
9|see|this

现在有了这个额外的测试数据行,上面提到的替代问题解决方案将返回:

sqlite> select * from contacts where firstname like 's%' or lastname like 's%' order by     case when firstname like 's%' then 0 else 1 end, firstname collate nocase, lastname collate nocase;
id|firstname|lastname|sortorder
9|see|this|
5|Smith|Allen|
6|Smith|Davey|
1|Smith|Jean|
7||Smitten|
8|barney|saltzberg|
4|john|Stewart|
于 2013-06-11T12:17:01.637 回答
2

你如何显示联系人?在列表视图中?

另一个想法是在列表视图中加载所有条目,然后您可以过滤列表

您可以创建自定义 Listview 并实现 Filterable

public class YourContactsListAdapter extends BaseAdapter implements Filterable {
  //some methodes to override 

   @Override
    public Filter getFilter() {
        Filter filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence c) {}
 @Override
            protected void publishResults(CharSequence charSequence, FilterResults filterResults) {}
}
}

也许是最简单但不是最好的方法。希望我没有误解这个问题。

//编辑:

试试 cursorAdapter 而不是 BaseAdapter 这里是一个例子...... http://tausiq.wordpress.com/2012/08/22/android-list-view-from-database-with-cursor-adapter/

于 2013-06-17T17:06:34.010 回答
1

如果 MyDate 为空,则 1 else 0 endPeopleDataBase.COLUMN_FIRST_NAME + ", " + PeopleDataBase.COLUMN_LAST_NAME作为db.query(而不是null)的最后一个参数应该起作用。

编辑:如果您希望空值排在最后,您需要遵循: SQL 如何在升序排序时使空值排在最后

即:

Cursor cur = db.query(PeopleDataBase.TABLE_PEOPLE, null, selection, null, null, null,
  "case when " + PeopleDataBase.COLUMN_FIRST_NAME + " is null then 1 else 0 end,"
  + PeopleDataBase.COLUMN_FIRST_NAME + ","
  + "case when " + PeopleDataBase.COLUMN_LAST_NAME+ " is null then 1 else 0 end,"
  + PeopleDataBase.COLUMN_LAST_NAME);
于 2013-05-27T08:03:25.037 回答
1

对于您给出的“期望”示例结果,这似乎会满足您的要求:

SELECT * FROM contacts
    WHERE lastname LIKE 's%' OR firstname LIKE 's%'
    ORDER BY LOWER(lastname), UPPER(firstname); -- rudimentary namesorts via "casing"

sqlite> select * from contacts where lastname like 's%' or firstname like 's%'
order by lower(lastname), upper(firstname);
16|sadie||
13|Sarah||
11|sue||
5|Smith|Allen|
6|Smith|Davey|
1|Smith|Jean|
10||Saltzberg|
9|Arney|saltzberg|
8|barney|saltzberg|
14|Carnie|Saltzberg|
7||Smitten|
12|Sally|smitten|
4|john|Stewart|
15|Jon|stewart|
sqlite>
于 2013-06-16T06:29:26.643 回答