java - Android Custom BaseAdapter getView not called -


so, have scoured interwebs , cannot find solution based on other people's experiences, posting issue. (please note 1st android app experience , debugging / updating existing app.)

when implement custom noteslistadapter (extends baseadapter) on listview, mlistnotesview (mlistnotesview.setadapter(this)), , load data arraylist mnotelist, getview function not being called. also, found mlistnotesview.setbackgroundresource not chaning background of control, either. have similar implementation on previous activity works correct. when copied on class , changed handle arraylist, broke. have getcount returning arraylist size(), not 0, , getitemid returns position. have feeling may xml or setup because it's acting listview not visible. perplexed. how listview show? inside of getview has not been reached may buggy.

viewticketorderactivity (some parts ommitted size)

public class viewticketorderactivity extends activity {     mysqldatabase mydatabase;     ticket mticket;     public arraylist<notes> mnotes = new arraylist<notes>();     string merrorstring;     button maddupdatebutton;     button macceptbutton;     //button mviewnotesbutton;     noteslistadapter mnoteslistadapter;     static final int error_dialog = 0;     static final int success_dialog = 1;     static final int completed_dialog = 2;     static final int restart_dialog = 3;     static final int loading = 0;     static final int load_error = 1;     static final int loaded = 4;     static final string ticket_extra = "ticket_extra";     static final string tag = "viewticketorderactivity";     private static final boolean gdebuglog = false;       public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.viewticketorder);         activity context = this;          string thetitle = "sundance ticket order";         thetitle += (mysqldatabase.testing == true) ? " (dev server)" : " (live)";         settitle(thetitle);         mydatabase = mysqldatabase.getmysqldatabase(this);         if (gdebuglog)      {             debuglogger.logstring(tag, ".oncreate");         }         mnoteslistadapter = new noteslistadapter(context, r.id.note_list);         log.d(this.tostring(),this.mnoteslistadapter.tostring());     }     private class noteslistadapter extends baseadapter {          private layoutinflater minflater;         private arraylist<notes> mnotelist;         private listview mlistnotesview;         private activity mactivity;         int mstate = loading;         string merrormessage;          private noteslistadapter(activity context, int listviewid) {             mactivity = context;             mnotelist = new arraylist<notes>();             minflater = layoutinflater.from(context);             mlistnotesview = (listview)context.findviewbyid(listviewid);             mlistnotesview.setbackgroundresource(r.color.emergency_red);             mlistnotesview.setadapter(this);             log.d(mlistnotesview.tostring(), string.valueof(mlistnotesview.getcount()));             this.notifydatasetchanged();              //mlistnotesview.setvisibility(view.visible);         }          void setloading()         {             mstate = loading;             this.notifydatasetchanged();          }          void setloaderror(string errorstring)         {             mstate = load_error;             merrormessage = errorstring;             this.notifydatasetchanged();         }          void setnotelist(arraylist<notes> innotes)         {             mstate = loaded;             mnotelist.clear();             mnotelist.addall(innotes);              log.d("setnotelist", "true " + innotes);             //mnotelist = mnotes;             this.notifydatasetchanged();          }           /**          * use array index unique id.          *          * @see android.widget.listadapter#getitemid(int)          */         @override         public long getitemid(int position) {             return position;         }          public int getcount(){             if (mstate == loaded) {                 log.d("getcount",string.valueof(mnotelist.size()));                 return mnotelist.size();             } else {                 return 0;             }         }          /**          * make view hold each row.          *          * @see android.widget.listadapter#getview(int, android.view.view,          *      android.view.viewgroup)          */         @override         public view getview(int position, view convertview, viewgroup parent) {             // viewholder keeps references children views avoid unneccessary calls             // findviewbyid() on each row.             log.d("getview",this.tostring());             if (mstate == loaded) {                 viewholder holder;                 // when convertview not null, can reuse directly, there                 // no need                 // reinflate it. inflate new view when                 // convertview supplied                 // listview null.                 notes note = this.getitem(position);                 if (convertview == null) {                     /*if (ticket.emergency())                     {                         convertview = minflater.inflate(r.layout.emergency_ticket_list_item_opt,                             null);                     }                     else                     {                         convertview = minflater.inflate(r.layout.ticket_list_item,                                 null);                      }*/                     convertview = minflater.inflate(r.layout.noteslist_item,                             null);                     // creates viewholder , store references 2                     // children views                     // want bind data to.                     holder = new viewholder();                     holder.notetext = (textview) convertview                             .findviewbyid(r.id.text_note);                     holder.datetext = (textview) convertview                             .findviewbyid(r.id.text_note_date);                     holder.createbytext = (textview) convertview                             .findviewbyid(r.id.text_note_by);                     holder.createbyidtext = (textview) convertview                             .findviewbyid(r.id.text_note_by_id);                       convertview.settag(holder);                 } else {                     // viewholder fast access                     // textview                     // , imageview.                     holder = (viewholder) convertview.gettag();                 }                 // bind data efficiently holder.                 holder.notetext.settext(note.note());                 holder.datetext.settext(note.date());                 holder.createbytext.settext(note.createby());                 holder.createbyidtext.settext(note.employeeid());                 if(!mticket.employeeid().equals(note.employeeid())){                     convertview.setbackgroundresource(r.drawable.solid_purple);                 }              } else if (mstate == loading ) {                 if (convertview == null) {                     convertview = minflater.inflate(r.layout.loading_view,                             null);                 }                 textview messagetext = (textview)convertview.findviewbyid(r.id.message);                 messagetext.settext("loading tickets");              } else if (mstate == load_error) {                 if (convertview == null) {                     convertview = minflater.inflate(r.layout.load_error_view,                             null);                 }                 textview messagetext = (textview)convertview.findviewbyid(r.id.message);                 messagetext.settext("error loading tickets");                 string errorstring = merrormessage != null ? merrormessage : "";                 textview errortext = (textview)convertview.findviewbyid(r.id.errortext);                 errortext.settext(errorstring);             }             return convertview;         }          class viewholder {             textview notetext;             textview datetext;             textview createbytext;             textview createbyidtext;         }         //@override         /*public int getcount() {             *//*if (mstate == loaded) {                 *//*             log.d("getcount mstate " + mstate,string.valueof(mnotelist.size())+", "+string.valueof(mnotes.size()));             return mnotelist.size();             *//*} else {                 log.d("getcount mstate " + mstate,"0");                 return 0;             }*//*         }*/         @override         public notes getitem(int position) {             log.d("getitem",mnotelist.get(position).tostring());             return mnotelist.get(position);         }         @override         public int getitemviewtype (int position) {             int result = mstate;             log.d("getitemid",string.valueof(position));             return result;         }         @override         public int getviewtypecount ()         {             return 4;         }      }      protected void onresume() {         super.onresume();         bundle extras = getintent().getextras();          if(extras !=null)         {             mticket = (ticket)extras.getserializable(ticket_extra);         }         else         {             mticket = new ticket();         }         if (mticket.emergency())         {             setcontentview(r.layout.view_emergency_ticketorder);                 }         else         {             setcontentview(r.layout.viewticketorder);                }          if (gdebuglog)         {             debuglogger.logstring(tag, ".onresume mticket " + mticket);         }         ticketcheckservice.clearnotificationfornewticket(mticket);         new gettickettask().execute();         new getnotestask().execute();         updatedisplayedticket();      }      private void updatedisplayedticket() {         maddupdatebutton = (button)findviewbyid(r.id.addupdatebutton);         macceptbutton = (button)findviewbyid(r.id.acceptbutton);         //mviewnotesbutton = (button)findviewbyid(r.id.viewnotesbutton);          string ticketstatus = mydatabase.getdescriptionstringforstatusstring(mticket.status());         if(ticketstatus == "job rejected") {             maddupdatebutton.settext("restart job");         } else {             maddupdatebutton.settext("add update");         }         if(ticketstatus == "requested") {             macceptbutton.settext("accept");         } else if(ticketstatus != "requested") {             macceptbutton.settext("back");         }         //mviewnotesbutton.settext(r.string.viewnotes);          textview idtext = (textview)findviewbyid(r.id.textticketid);          idtext.settext(mticket.id());          //textview descriptiontext = (textview)findviewbyid(r.id.textdescription);          //descriptiontext.settext(mticket.description());          textview titletext = (textview)findviewbyid(r.id.texttitle);         titletext.settext(mticket.title());          textview storeidtext = (textview)findviewbyid(r.id.textstoreid);         storeidtext.settext(mticket.store());          string formatphone;         textview storephonetext = (textview)findviewbyid(r.id.textstorephone);         if(mticket.phoneno().isempty()){             formatphone = "no phone no.";         } else {             storephonetext = (textview) findviewbyid(r.id.textstorephone);             formatphone = mticket.phoneno().replacefirst("(\\d{3})(\\d{3})(\\d+)", "($1)$2-$3");             storephonetext.setonclicklistener(new callclicklistener(mticket.phoneno()));         }         storephonetext.settext(formatphone);          textview categorytext = (textview)findviewbyid(r.id.textcategory);         string categorydescription = mysqldatabase.getdescriptionstringforcategorystring(mticket.category());         categorytext.settext(categorydescription);          if(ticketstatus == "completed pending") {             showdialog(completed_dialog);         }     }      public void onclickaccept(view v) {         try {             boolean maint = mydatabase.getsystemmaintstatus();             if(maint) {                 setloaderror("the phone app down maintenance.");                 return;             }         } catch (ioexception e) {             // todo auto-generated catch block             e.printstacktrace();         }         if(macceptbutton.gettext() =="accept") {             maddupdatebutton.setenabled(false);             macceptbutton.setenabled(false);             new accepttickettask().execute();         } else {             finish();         }     }      public void onclickaddupdate(view v) {         try {             boolean maint = mydatabase.getsystemmaintstatus();             if(maint) {                 setloaderror("the phone app down maintenance.");                 return;             }         } catch (ioexception e) {             // todo auto-generated catch block             e.printstacktrace();         }         if(maddupdatebutton.gettext() =="add update") {             intent = new intent(this, updateticketactivity.class);             i.putextra(updateticketactivity.ticket_extra, mticket);             startactivity(i);         } else if(maddupdatebutton.gettext() =="restart job") {             maddupdatebutton.setenabled(false);             macceptbutton.setenabled(false);             new restarttickettask().execute();         }     }      private class accepttickettask extends asynctask<void, integer, string>      {          protected string doinbackground(void... parent) {              merrorstring = null;              string result = null;                 string updatetime = dateformat.getdatetimeinstance().format(new date(0));             try {                 boolean success = mydatabase.updateticket(mticket.id(), mticket.employeeid(), mticket.description(), "1", updatetime, null);                 if (!success)                 {                     result = "could not update ticket";                 }             } catch (ioexception e) {                 // todo auto-generated catch block                 result = "could not update ticket - " + e.getlocalizedmessage();                 e.printstacktrace();             }              return result;          }           protected void onprogressupdate(integer... progress) {          }           protected void onpostexecute(string errorstring) {             if (null != errorstring) {                 merrorstring = errorstring;                 showdialog(error_dialog);             } else {                 showdialog(success_dialog);                 macceptbutton.settext("back");             }             maddupdatebutton.setenabled(true);             macceptbutton.setenabled(true);         }      }      private class restarttickettask extends asynctask<void, integer, string>      {          protected string doinbackground(void... parent) {              merrorstring = null;              string result = null;                 string updatetime = dateformat.getdatetimeinstance().format(new date(0));             try {                 boolean success = mydatabase.updateticket(mticket.id(), mticket.employeeid(), mticket.description(), "7", updatetime, null);                 if (!success)                 {                     result = "could not update ticket";                 }             } catch (ioexception e) {                 // todo auto-generated catch block                 result = "could not update ticket - " + e.getlocalizedmessage();                 e.printstacktrace();             }              return result;          }           protected void onprogressupdate(integer... progress) {          }           protected void onpostexecute(string errorstring)   {             if (null != errorstring) {                 merrorstring = errorstring;                 showdialog(error_dialog);             } else {                 showdialog(restart_dialog);                 macceptbutton.settext("done");                 maddupdatebutton.settext("add update");             }             maddupdatebutton.setenabled(true);             macceptbutton.setenabled(true);         }      }      private class gettickettask extends asynctask<void, integer, ticket>      {         string merror = null;          protected ticket doinbackground(void... parent) {              ticket result = null;             try {                 result = mydatabase.getticketwithid(mticket.id(), mticket.employeeid());             } catch (ioexception e) {                 // todo auto-generated catch block                 merror = e.getlocalizedmessage();                 e.printstacktrace();             }             return result;          }           protected void onprogressupdate(integer... progress) {          }           protected void onpostexecute(ticket result)           {               if (null != result) {                 mticket = result;              } else {                 setloaderror(merror);             }         }     }      private class getnotestask extends asynctask<void, integer, arraylist<notes>> {         string merror = null;         protected arraylist<notes> doinbackground(void... parent) {             arraylist<notes> result = new arraylist<notes>();             try {                 result = mydatabase.getticketnotes(mticket.id());             } catch (ioexception e) {                 // todo auto-generated catch block                 mydatabase.debuglog("error caught" + e);                 merror = e.getlocalizedmessage();                 e.printstacktrace();             }             return result;         }          protected void onprogressupdate(integer... progress) {         }          protected void onpostexecute(arraylist<notes> result) {             if (null != result) {                 log.d("result", result.tostring());                 mnotes = result;             } else {                 log.d("setnotelist","false");                 mnoteslistadapter.setloaderror(merror);             }          }     }      private void updatedisplayednotes(){         arraylist<notes> newnotes = mnotes;         if(newnotes != null) {             mnoteslistadapter.setnotelist(newnotes);         }     }      /*private class updatedisplayednotes extends asynctask<void, integer, arraylist<notes>> {          public arraylist<notes> newnotes = new arraylist<notes>();         public updatedisplayednotes(){             super();             log.d(this.tostring(), "updating");         }         protected arraylist<notes> doinbackground(void... parent) {              log.d(this.tostring(), "background task");             (notes note : mnotes) {                 log.d(this.tostring(),note.tostring());                 if(note != null) {                     log.d(this.tostring(), "note added");                     newnotes.add(note);                 }             }              return newnotes;         }         protected void onpostexecute(arraylist<notes> newnotes)         {             if(newnotes != null) {                 mnotes.clear();                 mnotes.addall(newnotes);                 mnoteslistadapter.setnotelist(mnotes);             }         }     }*/       void setloaderror(string error) {         setcontentview(r.layout.load_error_view);         textview messagetext = (textview)findviewbyid(r.id.message);         messagetext.settext("error loading ticket");         string errorstring = error != null ? error : "";         textview errortext = (textview)findviewbyid(r.id.errortext);         errortext.settext(errorstring);         finish();     } } 

viewticketorder.xml (where note_list is)

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="@drawable/background"     android:orientation="vertical"     tools:context=".viewticketorderactivity"     tools:ignore="hardcodedtext" >     <textview         android:id="@+id/texttitle"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_marginleft="10dp"         android:layout_marginright="10dp"         android:layout_margintop="3dp"         android:paddingleft="3dp"         android:paddingright="3dp"         android:text="@string/loadingticket"         android:textappearance="?android:attr/textappearancelarge"         android:textcolor="@color/white_color"         android:textsize="20dp"         android:textisselectable="true"         android:background="@drawable/title_transparent_bg"/>       <linearlayout         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:orientation="horizontal" >          <textview             android:id="@+id/textview01"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_margintop="3dp"             android:paddingtop="2dp"             android:paddingleft="10dp"             android:text="@string/ticketid"             android:textappearance="?android:attr/textappearancesmall"             tools:ignore="hardcodedtext" />          <textview             android:id="@+id/textticketid"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_marginleft="10dp"             android:layout_marginright="10dp"             android:layout_margintop="3dp"             android:textappearance="?android:attr/textappearancesmall"             android:focusable="true"             android:textcolor="@color/white_color"             android:textisselectable="true"/>          <textview             android:id="@+id/textview"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_marginleft="10dp"             android:layout_margintop="3dp"             android:paddingtop="2dp"             android:text="@string/storeid"             android:textappearance="?android:attr/textappearancesmall"             tools:ignore="hardcodedtext" />          <textview             android:id="@+id/textstoreid"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_marginleft="10dp"             android:layout_marginright="10dp"             android:layout_margintop="3dp"             android:textappearance="?android:attr/textappearancesmall"             android:focusable="true"             android:textcolor="@color/white_color"             android:textisselectable="true"/>          <textview             android:id="@+id/textstorephone"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_marginleft="10dp"             android:layout_marginright="10dp"             android:layout_margintop="3dp"             android:textappearance="?android:attr/textappearancesmall"             android:focusable="true"             android:textcolor="@color/white_color"             android:textisselectable="true"             />      </linearlayout>      <linearlayout         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:orientation="horizontal" >     <textview         android:id="@+id/textview05"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_marginleft="10dp"         android:paddingtop="2dp"         android:text="@string/category"         android:textappearance="?android:attr/textappearancesmall" />      <textview         android:id="@+id/textcategory"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_marginleft="10dp"         android:layout_marginright="10dp"         android:gravity="center_vertical"         android:text=""         android:textappearance="?android:attr/textappearancesmall"         android:textcolor="@color/white_color"         android:focusable="true"         android:textisselectable="true"/>      </linearlayout>     <listview         android:id="@+id/note_list"         android:layout_width="match_parent"         android:layout_height="0dp"         android:layout_weight="5"         android:divider="@drawable/ticket_item_divider"         android:dividerheight="1dp"         tools:ignore="nestedweights"         android:choicemode="singlechoice"         android:clickable="true"         android:background="@drawable/title_transparent_bg">      </listview>      <linearlayout         android:layout_width="match_parent"         android:layout_height="wrap_content" >         <button            android:id="@+id/acceptbutton"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginleft="10dp"            android:layout_marginright="2dp"            android:layout_weight="1"            android:onclick="onclickaccept" />          <button             android:id="@+id/addupdatebutton"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_marginleft="2dp"             android:layout_marginright="10dp"             android:layout_weight="1"             android:onclick="onclickaddupdate" />      </linearlayout>  </linearlayout> 

notelist_item.xml (inflator)

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"               android:id="@+id/text_note_item"               android:layout_width="match_parent"               android:layout_height="wrap_content"               android:background="#80ffffff"               android:orientation="vertical"     >     <textview         android:id="@+id/text_note"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:paddingleft="10dp"         android:paddingright="10dp"         android:text="@string/filler_string"         android:textappearance="?android:attr/textappearancemedium"         android:textcolor="@color/text_item_color"         android:textsize="@dimen/big_text_item_size" />      <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"                   android:layout_width="wrap_content"                   android:layout_height="wrap_content"                   android:orientation="horizontal"                   android:layout_weight=".70"         >         <textview             android:id="@+id/text_note_date"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:paddingleft="10dp"             android:paddingright="10dp"             android:text="@string/filler_string"             android:textappearance="?android:attr/textappearancesmall"             android:textcolor="@color/text_sub_item_color" />          <textview             android:id="@+id/text_note_by"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:paddingleft="10dp"             android:paddingright="10dp"             android:text="@string/filler_string"             android:textappearance="?android:attr/textappearancesmall"             android:textcolor="@color/text_sub_item_color" />          <textview             android:id="@+id/text_note_by_id"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:paddingleft="10dp"             android:paddingright="10dp"             android:text="@string/filler_string"             android:textappearance="?android:attr/textappearancesmall"             android:textcolor="@color/text_sub_item_color" />      </linearlayout> </linearlayout> 

i'm going recommend reorganize code. here general tips:

1) keep views listview in activity class. don't try inflate view in adapter class. in activity's oncreate() after setcontentview() should have like:

listview listview = (listview) findviewbyid(r.id.listview); 

2) next need data shown in listview , store in list. didn't see in code data comes from, let's comes database. should create arraylist , store data want show in listview in arraylist

3) next need create adapter , pass list of data adapter.

4) once has been done listview has adapter supply data it. if you've done correctly system call getview() automatically , code inside should run , render view.

not exact solution, explanation figure out.


Comments