java - Remove duplicate data from cursor and get data when value increase -
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 |
cursor hold data table. now, want data
gettopic = ab gettitle= bcd gettype = ref names = ferari toyota audi bmw
i tried this
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());
but, it's not showing expected result. it's showing bc
column names
ferari toyota audi bmw.
as checking lastitem!= currentitem
it's not showing last author name.
now, question is
1) should expected result ? 2) checking lastitem!= currentitem
it's not showing last author name. but, how can store name.
this should work (untested):
sparsearray<data> dataarray = new sparsearray<data>(); cursor.movetofirst(); { 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()); // can names (int = 0; < dataarray.size(); i++) { data d = dataarray.get(i); string names = textutils.join(" ", d.names); }
where data
is:
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>(); } }
Comments
Post a Comment