listview - Android BaseAdapter's getCount() getView() -
i have baseadapter 3 kind of layout used set jsonobject listview. adapter getcount() returns right number of items should displayed on listview, display first one.
i tried find response problem here, i've found none.
this code:
public class perfilinfoadapter extends baseadapter { public static final int view_type_titulo = 0; public static final int view_type_descricao = 1; public static final int view_type_key_value = 2; private jsonarray list; private activity activity; private viewholder viewholder; public perfilinfoadapter(activity activity, jsonarray list) { this.activity = activity; this.list = list; } protected class viewholder { textview textviewtitulo; textview textviewdescricao; textview textviewkey; textview textviewvalue; } @override public int getcount() { log.d("perfilinfoadapter", "number of items in array: " + integer.tostring(this.list.length())); homecoming this.list.length(); } @override public jsonobject getitem(int position) { jsonobject json = null; seek { json = this.list.getjsonobject(position); } grab (jsonexception e) { e.printstacktrace(); } homecoming json; } @override public long getitemid(int position) { homecoming position; } @override public int getitemviewtype(int position) { int retorno = -1; jsonobject json = null; seek { json = this.list.getjsonobject(position); if (json.getstring("key").equals("titulo")) { retorno = view_type_titulo; } else if (json.getstring("key").equals("descrição") || json.getstring("key").equals("sou")) { retorno = view_type_descricao; } else { retorno = view_type_key_value; } } grab (jsonexception e) { e.printstacktrace(); } homecoming retorno; } @override public int getviewtypecount() { homecoming 3; } @override public view getview(int position, view container, viewgroup viewgroup) { system.out.println("getview " + position + " " + container); this.viewholder = null; int type = this.getitemviewtype(position); if (container == null) { this.viewholder = new viewholder(); switch (type) { case view_type_titulo: container = this.activity.getlayoutinflater().inflate( r.layout.perfil_info_full_titulo, viewgroup, false); this.viewholder.textviewtitulo = (textview) container .findviewbyid(r.id.perfil_info_full_textviewtitulo); break; case view_type_descricao: container = this.activity.getlayoutinflater().inflate( r.layout.perfil_info_full_descricao, viewgroup, false); this.viewholder.textviewdescricao = (textview) container .findviewbyid(r.id.perfil_info_full_textvewdescricao); break; case view_type_key_value: container = this.activity.getlayoutinflater().inflate( r.layout.perfil_info_list, viewgroup, false); this.viewholder.textviewkey = (textview) container .findviewbyid(r.id.perfil_info_full_chave_valor_textfieldchave); this.viewholder.textviewvalue = (textview) container .findviewbyid(r.id.perfil_info_full_chave_valor_textfieldvalor); break; } container.settag(this.viewholder); } else { this.viewholder = (viewholder)container.gettag(); } seek { jsonobject json = this.list.getjsonobject(position); switch (type) { case view_type_titulo: this.viewholder.textviewtitulo.settext(json.getstring("value")); break; case view_type_descricao: this.viewholder.textviewdescricao.settext(json .getstring("value")); break; case view_type_key_value: this.viewholder.textviewkey.settext(json.getstring("key")); this.viewholder.textviewvalue.settext(json.getstring("value")); break; } } grab (jsonexception e) { e.printstacktrace(); } homecoming container; } }
this log returns:
10-26 09:42:30.568: d/perfilinfoadapter(17228): number of items in array: 11
another of import info listview within gridview, has 4 different kinds of views, gridview working perfectly, not listview.
public class perfiladapter extends baseadapter { private list<jsonobject> jsonlist; private activity activity; private perfilhelper helper; private imageloader imageloader; private viewholder viewholder; private boolean exibirfull; public static final int view_type_foto_principal = 0; public static final int view_type_info = 1; public static final int view_type_info_list = 2; public static final int view_type_galeria = 3; public perfiladapter(activity activity, list<jsonobject> json, perfilhelper helper) { this.activity = activity; this.helper = helper; this.jsonlist = json; this.exibirfull = true; if (!imageloader.getinstance().isinited()) { imageloader.getinstance().init(new imageloaderconfiguration.builder(this.activity).build()); } imageloader = imageloader.getinstance(); } public void exibirfull(boolean exibir) { this.exibirfull = exibir; this.notifydatasetchanged(); } @override public int getcount() { homecoming this.jsonlist.size(); } @override public object getitem(int i) { homecoming this.jsonlist.get(i); } @override public long getitemid(int i) { homecoming i; } @override public int getitemviewtype(int position) { int retorno = -1; if (this.jsonlist.get(0).has("foto")) { if (position == 0) { retorno = view_type_foto_principal; } else if (position == 1) { retorno = view_type_info; } else if (position == 2) { if (this.exibirfull) { retorno = view_type_info_list; } else { retorno = view_type_galeria; } } else { retorno = view_type_galeria; } } else { if (position == 0) { retorno = view_type_info; } else if (position == 1) { if (this.exibirfull) { retorno = view_type_info_list; } else { retorno = view_type_galeria; } } else { retorno = view_type_galeria; } } homecoming retorno; } @override public int getviewtypecount() { homecoming 4; } public void updatejsonperfil(list<jsonobject> json) { this.jsonlist = json; this.notifydatasetchanged(); } @override public view getview(int i, view container, viewgroup viewgroup) { this.viewholder = null; int type = this.getitemviewtype(i); if (container == null) { this.viewholder = new viewholder(); switch (type) { case view_type_foto_principal: container = this.activity.getlayoutinflater().inflate(r.layout.perfil_foto, viewgroup, false); this.viewholder.imageview = (imageview) container.findviewbyid(r.id.perfil_foto_imageview); break; case view_type_info: container = this.activity.getlayoutinflater().inflate(r.layout.perfil_info, viewgroup, false); this.viewholder.textviewapelido = (textview) container.findviewbyid(r.id.perfil_info_apelido); this.viewholder.textviewcidade = (textview) container.findviewbyid(r.id.perfil_info_textvewcidade); this.viewholder.textviewdistancia = (textview) container.findviewbyid(r.id.perfil_info_textviewdistancia); break; case view_type_info_list: container = this.activity.getlayoutinflater().inflate(r.layout.perfil_info_list, viewgroup, false); this.viewholder.listviewinfo = (listview) container.findviewbyid(r.id.perfil_info_list_listview); break; case view_type_galeria: container = this.activity.getlayoutinflater().inflate(r.layout.perfil_info, viewgroup, false); break; } container.settag(this.viewholder); } else { this.viewholder = (viewholder)container.gettag(); } if (this.jsonlist.size() > 0) { jsonobject json = this.jsonlist.get(i); seek { if (type == view_type_foto_principal) { jsonobject foto = json.getjsonobject("foto"); this.imageloader.displayimage(foto.getstring("full"), this.viewholder.imageview); } else if (type == view_type_info) { jsonobject perfil = json.getjsonobject("perfil"); this.viewholder.textviewapelido.settext(perfil.getstring("apelido")); this.viewholder.textviewcidade.settext(perfil.getstring("cidade")); this.viewholder.textviewdistancia.settext(perfil.getstring("distancia")); } else if (type == view_type_info_list) { // utilize sec listview this.viewholder.listviewinfo.setadapter(new perfilinfoadapter(this.activity, json.getjsonarray("info"))); } else { log.d("perfiladapter", "populando: view_type_galeria"); } } grab (jsonexception e) { e.printstacktrace(); } } homecoming container; } protected class viewholder { imageview imageview; textview textviewapelido; textview textviewcidade; textview textviewdistancia; listview listviewinfo; } }
i have tried changing listview gridview, problem adapter.
can help me? appreciate it!
your first problem: trying combine listview
, gridview
. never ever ever set 1 within other. you'll have sorts of problems...as noticing. first big problem scrolling. android not when scrollable view embedded within , both scroll same direction. comical sketch android employee this. doing viable when 1 scrolls horizontally , other vertically.
your next big problem, embedding listadapter
within listadapter
. have remember, getview()
method can invoked 3-4 times per position. when embed adapter each position invoked 3-4 times per it's own position...holy performance hit! has bad thought written on it.
a concern see jsonarray
/list
referencing. perfilinfoadapter
maintains same reference jsonarray
used instantiate it...which same info referenced perfiladapter
list
. farther perfiladapter
maintains same list referenced whomever using it. sets unsafe chain of references can cause issues when modifying if not careful. ideally, each adapter should maintain same info in on list
or jsonarray
instance.
to sum up, reply alter design choice. there other ways display info other needing vertical scrolling within vertical scrolling. if listview
doesn't need scrolling utilize linearlayout
. if gridview
doesn't need scrolling utilize tablelayout
or gridlayout
. or alter ux coming different ui.
as side note, if need total fledged jsonarray adapter check out advanced-adapters. jsonadapter releasing within week or , can found on redesign
branch. code complete, demo app that's holding release.
android listview gridview baseadapter
No comments:
Post a Comment