java - The Android program does not execute getView function inside BaseAdapter Class -
this simple program uses custom layout using baseadapter. logcat shows other functions have been executed, except getview function. on running app, receive blank screen.
here code in mainactivity.java
public class mainactivity extends activity { public final string tag = "task:"; //objects arraylist created class. public class codelearnchapter { string chaptername; string chapterdescription; } //defined custome adapter. myadapter mmyadapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main_simple_list); log.i(tag, "listview set contentview"); //initialized custom adapter. mmyadapter = new myadapter(); log.i(tag,"new adapter mmyadapter created"); listview codelearnlessons = (listview) findviewbyid(r.id.listid); log.i(tag,"listview inflated"); codelearnlessons.setadapter(mmyadapter); log.i(tag, "mmyadapter set listview codelearnlessons"); } public class myadapter extends baseadapter{ //data arraylist codelearnchapterlist obtained getdataforlistview() method. list<codelearnchapter> codelearnchapterlist = getdataforlistview(); @override public int getcount() { log.i(tag, "arraylist size sent"); return codelearnchapterlist.size(); } @override public object getitem(int arg0) { log.i(tag, "object of particular row sent"); return codelearnchapterlist.get(arg0); } @override public long getitemid(int arg0) { return arg0; } @override public view getview(int arg0, view arg1, viewgroup arg2) { if(arg1==null){ layoutinflater inflater = (layoutinflater) mainactivity.this.getsystemservice(context.layout_inflater_service); arg1 = inflater.inflate(r.layout.listitem, arg2, false); log.i(tag, "row of listitem inflater"); } textview mchaptername = (textview) arg1.findviewbyid(r.id.textview1); log.i(tag,"main description referenced value"); textview mchapterdescription = (textview) arg1.findviewbyid(r.id.textview2); log.i(tag, "sub description referenced value"); codelearnchapter mcodelearnchapter = codelearnchapterlist.get(arg0); log.i(tag, "particular of object corresponding row arg0 passed object mcodelearnchapter"); mchaptername.settext(mcodelearnchapter.chaptername); log.i(tag, "chaptername set"); mchapterdescription.settext(mcodelearnchapter.chapterdescription); log.i(tag,"chapterdescription set"); return arg1; } } public list<codelearnchapter> getdataforlistview(){ list<codelearnchapter> codelearnchapterlist = new arraylist<codelearnchapter>(); //the arraylist codelearnchapterlist populated 3 objects. for(int i=10;i<3;i++){ codelearnchapter mcodelearnchapter = new codelearnchapter(); mcodelearnchapter.chaptername = "chapter"+ i; mcodelearnchapter.chapterdescription = "description chapter"+i; codelearnchapterlist.add(mcodelearnchapter); } log.i(tag, "data (list of objects ) populated in arraylist "); return codelearnchapterlist; } }
and here's logcat output
06-26 01:41:15.815: i/task:(1403): listview set contentview 06-26 01:41:15.825: i/task:(1403): data (list of objects ) populated in arraylist 06-26 01:41:15.825: i/task:(1403): new adapter mmyadapter created 06-26 01:41:15.835: i/task:(1403): listview inflated 06-26 01:41:15.835: i/task:(1403): arraylist size sent 06-26 01:41:15.845: i/task:(1403): arraylist size sent 06-26 01:41:15.845: i/task:(1403): arraylist size sent 06-26 01:41:15.845: i/task:(1403): mmyadapter set listview codelearnlessons 06-26 01:41:16.055: i/task:(1403): arraylist size sent 06-26 01:41:16.055: i/task:(1403): arraylist size sent 06-26 01:41:16.705: i/task:(1403): arraylist size sent 06-26 01:41:16.705: i/task:(1403): arraylist size sent
i'm not sure if java or android issue since i'm relatively new programing. helpful clarification i've been re reading code past couple of days.
problem:
for(int i=10;i<3;i++){
your arraylist
not filled object getview
not called because empty.
what did in above code initializing i 10 end forloop immediately.
solution:
change 0 if want fill array 3 object.
for(int i=0;i<3;i++){
Comments
Post a Comment