android - ListFragment error -
i have list in "listview" scroll list using mess, grouping , list has header icons.
public class mycustomadapter extends baseadapter {
private static final string assets_dir = "images/"; private static final int type_head = -1; private static final int type_item = 0; private static final int type_separator = 1; private static final int type_max_count = type_separator + 1; private context ctx; private arraylist<string> mdata = new arraylist<string>(); private layoutinflater minflater; private treeset<integer> mseparatorsset = new treeset<integer>(); public mycustomadapter(context context) { this.ctx = context; minflater = (layoutinflater) ctx .getsystemservice(context.layout_inflater_service); } public void additem(final string item) { mdata.add(item); notifydatasetchanged(); } public void addseparatoritem(final string item) { mdata.add(item); mseparatorsset.add(mdata.size() - 1); notifydatasetchanged(); } public void addheaditem(){ mdata.add(""); mseparatorsset.add(0); notifydatasetchanged(); } @override public int getcount() { homecoming mdata.size(); //return equipos.size(); } @override public string getitem(int position) { homecoming mdata.get(position) ; } @override public long getitemid(int position) { // todo auto-generated method stub homecoming position; } @override public int getitemviewtype(int position) { if (position==0) homecoming type_head; homecoming mseparatorsset.contains(position) ? type_separator : type_item; } @override public int getviewtypecount() { homecoming type_max_count; } @override public view getview(int position, view convertview, viewgroup parent) { viewholder holder = null; int type = getitemviewtype(position); system.out.println("getview " + position + " " + convertview + " type = " + type); if (convertview == null) { holder = new viewholder(); switch (type) { case type_item: convertview = minflater.inflate(r.layout.list_item, null); holder.textview1 = (textview) convertview.findviewbyid(r.id.textview1); holder.textview2 = (textview) convertview.findviewbyid(r.id.textview2); holder.textview3 = (textview) convertview.findviewbyid(r.id.textview3); holder.imageview1 = (imageview) convertview.findviewbyid(r.id.imageview1); string[] datos = mdata.get(position).split("-"); holder.textview1.settext(string.format(" %s - %s", datos[0],datos[1])); holder.textview2.settext(datos[2]); holder.textview3.settext(datos[3]); string sel_bandera = datos[4].trim() ; string imgfilepath = ""; if (sel_bandera.equals("verde")){ imgfilepath = assets_dir + "circle_green.png" ; }else if (sel_bandera.equals("amarilla")){ imgfilepath = assets_dir + "circle_yellow.png"; }else { imgfilepath = assets_dir + "circle_red.png"; } seek { bitmap bitmap = bitmapfactory.decodestream(this.ctx.getresources().getassets().open(imgfilepath)); holder.imageview1.setimagebitmap(bitmap); //bandera.setimagebitmap(bitmap); } grab (ioexception e) { e.printstacktrace(); } break; case type_separator: convertview = minflater.inflate(r.layout.list_group, null); holder.textview1 = (textview) convertview.findviewbyid(r.id.textseparator); holder.textview1.settext(mdata.get(position)); break; case type_head: convertview = minflater.inflate(r.layout.list_head, null); break; } convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } homecoming convertview; } public static class viewholder { public textview textview1; public textview textview2; public textview textview3; public imageview imageview1; public textview gettextview1() { homecoming textview1; } public void settextview1(textview textview1) { this.textview1 = textview1; } public textview gettextview2() { homecoming textview2; } public void settextview2(textview textview2) { this.textview2 = textview2; } public textview gettextview3() { homecoming textview3; } public void settextview3(textview textview3) { this.textview3 = textview3; } public imageview getimageview1() { homecoming imageview1; } public void setimageview1(imageview imageview1) { this.imageview1 = imageview1; } }
}
public class equiposactivity extends listfragment implements ontouchlistener {
public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { madapter = new mycustomadapter(getactivity());
if (lista.length > 0) { string[] datos = lista[0].split("-"); cabecera_grupo = datos[4]; } madapter.addheaditem(); (int = 0; < lista.length; i++) { string[] datos = lista[i].split("-"); string grupo = datos[4]; if (i == 0) { madapter.addseparatoritem(grupo.replace("_", " ")); } if (!grupo.equals(cabecera_grupo)) { madapter.addseparatoritem(grupo.replace("_", " ")); cabecera_grupo = grupo; } madapter.additem(string.format("%s - %s - %s - %s - %s", datos[0], datos[1], datos[2], datos[3], datos[5])); } setlistadapter(madapter);
return super.oncreateview(inflater, container, savedinstancestate); }
i suggest rewrite getview() method, because think wrongly using viewholder pattern. read this: http://www.jmanzano.es/blog/?p=166 or rid of viewholder , code getview() without it.
android listview grouping fragment
No comments:
Post a Comment