java - Every other icon in ListView not showing up -
i have list custom arrayadapter loads icons next list items except every other list item showing icon
public class myadapter extends arrayadapter<string>{ context mcontext; public myadapter(context context, int resource, int textviewresourceid, string[] strings) { super(context, resource, textviewresourceid, strings); this.mcontext = context; } @override public view getview(int position, view convertview, viewgroup parent) { layoutinflater inflater = (layoutinflater)mcontext.getsystemservice(context.layout_inflater_service); view row = inflater.inflate(r.layout.filetypeslist, parent, false); string[] items = mcontext.getresources().getstringarray(r.array.filetypes); imageview iv = (imageview) row.findviewbyid(r.id.imageview1); textview tv = (textview) row.findviewbyid(r.id.tvtype); tv.settext(items[position]); if(items[position].equals("txt")){ iv.setimageresource(r.drawable.txticon);//not showing }else if(items[position].equals("html")){ iv.setimageresource(r.drawable.htmlicon); }else if(items[position].equals("js")){ iv.setimageresource(r.drawable.javascripticon);//not showing }else if(items[position].equals("css")){ iv.setimageresource(r.drawable.cssicon); } homecoming row; }
}
public class newfile extends listactivity { edittextlinenumbers newet; layoutinflater minflater; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.new_file); setlistadapter(new myadapter(this, android.r.layout.simple_list_item_1, r.id.tvtype, getresources().getstringarray(r.array.filetypes))); } } ***edit*** public view getview(int position, view convertview, viewgroup parent) { view row = convertview; if (row == null) { layoutinflater inflater = (layoutinflater)mcontext.getsystemservice(context.layout_inflater_service); row = inflater.inflate(r.layout.filetypeslist, parent, false); } string[] items = mcontext.getresources().getstringarray(r.array.filetypes); imageview iv = (imageview) row.findviewbyid(r.id.imageview1); textview tv = (textview) row.findviewbyid(r.id.tvtype); tv.settext(items[position]); if(items[position].equals("txt")){ iv.setimageresource(r.drawable.txticon); }else if(items[position].equals("html")){ iv.setimageresource(r.drawable.htmlicon); }else if(items[position].equals("js")){ iv.setimageresource(r.drawable.javascripticon); }else if(items[position].equals("php")){ iv.setimageresource(r.drawable.phpicon); }else if(items[position].equals("css")){ iv.setimageresource(r.drawable.cssicon); } homecoming row; } <?xml version="1.0" encoding="utf-8"?> <resources xmlns:android="http://schemas.android.com/apk/res/android"> <string-array name="filetypes"> <item name="txt">text</item> <item name="html">html</item> <item name="js">javascript class</item> <item name="css">css</item> <item name="php">php class</item> </string-array> </resources>
your chain of string.equals
comparisons not checking right values 2 aren't showing up. checking "js" instead of "javascript class" , "txt" instead of "text".
aside that, have few suggestions:
with arrayadapter, can string current row callinggetitem(position)
instead. make int-array
in resources , have each item point drawable corresponding item in string-array
. let adapter store local copies of arrays instead of constructing them resources every time getview
called. same layoutinflater
(build these in constructor of adapter). can drawable id intarray[position]
instead of chain of string.equals()
comparisons. use convertview whenever possible shoshi pointed out. java android listview icons
No comments:
Post a Comment