3

I have made the concatenation so far, but in the results, it displays

UncleSam.

What I want to do is to put a space between the two columns so the result would be

Uncle Sam.

This is the code I'm working on:

   public Cursor getAllPatients()
{
    Cursor localCursor = //  
            this.myDataBase.query(DB_TABLE, new String[] { 
                    KEY_ID, KEY_FNAME + "||" + KEY_LNAME, KEY_DIAGNOSIS, KEY_LASTFFUP }, null, null, null, null, null);
    if (localCursor != null)
      localCursor.moveToFirst();
    return localCursor;
}

from DBHelper.java

and

 Cursor cursor = dbHelper.getAllPatients();
    String[] data = new String[]{
            DBHelper.KEY_FNAME + "||" + DBHelper.KEY_LNAME, DBHelper.KEY_DIAGNOSIS, DBHelper.KEY_LASTFFUP};
    // 
    int[] to = new int[] {R.id.fullname, R.id.diagnosis, R.id.lastffup};

    dataAdapter = new SimpleCursorAdapter(this, R.layout.custom_row, cursor, data, to, 0);

from my MainActivity.

Any help will do. Thanks.

4

3 回答 3

3

你可以像这样连接:

Cursor localCursor = this.myDataBase.rawQuery("SELECT (KEY_FNAME  || ' ' || KEY_LNAME) AS fullname, KEY_ID, KEY_DIAGNOSIS, KEY_LASTFFUP FROM DB_TABLE");

您的连接全名将在光标列“全名”中。

在主要活动中:

String[] data = new String[]{ "fullname", DBHelper.KEY_DIAGNOSIS, DBHelper.KEY_LASTFFUP};

(您可能应该为“fullname”分配一个 DBHelper 常量)。

于 2013-08-06T16:31:21.260 回答
0

Android Sqlite concat 两列

在与该线程的作者进行了一些交谈后,我建议您不要连接列(您真的不需要它)并连接从 Cursor 检索的字符串。您的陈述将更具人类可读性和更清晰的解决方案。

解释:

通常是非常有用和有效的方法来表示应用层上的表,对象将表示表。例如,如果您有表 User,那么创建新类 User 并且表中的列将等于该类中的属性。这种方式我猜很优雅(如果其他人会看到你的代码,他不会感到困惑和害怕)

最后,您可以简单地将 fname 和 lname 添加到ListAdapter


所以我更喜欢这种方式:

List<User> users = new ArrayList<User>();
User u = null;
String query = "select * from Table";
Cursor c = db.rawQuery(query, null);
if (c != null && c.moveToFirst()) {
   do { 
      u = new User();
      u.setFirstName(c.getString(c.getColumnIndex("fname")));
      u.setLastName(c.getString(c.getColumnIndex("lname")));
      ...
      users.add(u);
   } while (c.moveToNext());
}

然后在某个地方ListAdapter你可以做:

textView.setText(u.getFirstname() + " " + u.getLastName());

希望能帮助到你。

于 2013-08-06T16:46:42.277 回答
0

我终于让它工作了。

 public Cursor getAllPatients()
 {
Cursor localCursor = //  
        this.myDataBase.query(DB_TABLE, new String[] { 
                KEY_ID, KEY_FNAME + "|| ' ' ||" + KEY_LNAME, KEY_DIAGNOSIS, KEY_LASTFFUP }, null, null, null, null, null);
if (localCursor != null)
  localCursor.moveToFirst();
return localCursor;
}

Cursor cursor = dbHelper.getAllPatients();
String[] data = new String[]{
        DBHelper.KEY_FNAME + "|| ' ' ||" + DBHelper.KEY_LNAME, DBHelper.KEY_DIAGNOSIS, DBHelper.KEY_LASTFFUP};
// 
int[] to = new int[] {R.id.fullname, R.id.diagnosis, R.id.lastffup};

dataAdapter = new SimpleCursorAdapter(this, R.layout.custom_row, cursor, data, to, 0);
于 2013-08-06T17:05:33.857 回答