2013-07-31 23 views
0
1 | AB | BCD | ref | Ferari| 
---------------------------------- 
1 | AB | BCD | ref | TOYOTA| 
---------------------------------- 
1 | AB | BCD | ref| AUDI | 
---------------------------------- 
1 | AB | BCD | ref| BMW | 
--------------------------------- 
2 | BC | ABC | ref | NISSAN| 
---------------------------------- 
2 | BC | ABC | ref | SUZKI| 
---------------------------------- 
2 | BC | ABC | ref| TATA | 

游标保存类似此表的数据。现在,我希望得到像删除游标中的重复数据,并在增加值时获取数据

getTopic = AB 
getTitle= BCD 
getType = ref 
Names = FERARI TOYOTA AUDI BMW 

我这个

do{ 
     int current = cursor.getInt(cursor.getColumnIndexOrThrow("_id")); 
     String Title = cursor.getString(cursor.getColumnIndexOrThrow("TITLE")); 
     if(!StoreTitle.equalsIgnoreCase(Title) && lastId != current){ 

      getTopic = cursor.getString(cursor.getColumnIndexOrThrow("TOPIC")); 
      getTitle = cursor.getString(cursor.getColumnIndexOrThrow("TITLE")); 
      getType = cursor.getString(cursor.getColumnIndexOrThrow("TYPE")); 
      getName = cursor.getString(cursor.getColumnIndexOrThrow("NAME")); 

     }else{ 
       getName = getName +" "+ cursor.getString(cursor.getColumnIndexOrThrow("NAME")); 
      } 

     lastId = current; 
     StoreTitle = Title; 
}while(cursor.moveToNext()); 

试图数据,但是,它没有表现出预期的结果。它显示BCNames FERARI TOYOTA AUDI宝马。

由于我也正在检查lastitem!= currentitem,因此它也没有显示最后的作者姓名。

现在,我的问题是

1)我应该怎么做才能得到预期的结果? 2)因为我也在检查lastitem!= currentitem,所以它也没有显示上一个作者的名字。但是,如何存储该名称。

回答

0

这应该工作(未经测试)

SparseArray<Data> dataArray = new SparseArray<Data>(); 
    cursor.moveToFirst(); 
    do { 
     int id = cursor.getInt(cursor.getColumnIndexOrThrow("_id")); 
     String title = cursor.getString(cursor.getColumnIndex("TITLE")); 
     String topic = cursor.getString(cursor.getColumnIndex("TOPIC")); 
     String type = cursor.getString(cursor.getColumnIndex("TYPE")); 
     String name = cursor.getString(cursor.getColumnIndex("NAME")); 
     Data d = dataArray.get(id); 
     if (d == null) { 
      d = new Data(id, title, topic, type); 
      d.names.add(name); 
      dataArray.put(id, d); 
     } else { 
      d.names.add(name); 
     } 
    } while (cursor.moveToNext()); 


// now you can get names like this 
    for (int i = 0; i < dataArray.size(); i++) { 
     Data d = dataArray.get(i); 
     String names = TextUtils.join(" ", d.names); 
    } 

其中Data是:

private class Data { 
    int id; 
    String title, topic, type; 
    List<String> names; 

    public Data(int id, String title, String topic, String type) { 
     this.id = id; 
     this.title = title; 
     this.topic = topic; 
     this.type = type; 
     names = new ArrayList<String>(); 
    } 
} 
+0

我怎样才能得到的名字? – user2579475

+0

它的基本Java ...我会尽快更新我的答案... –