Limited ListField items are drawn instead of complete list in Blackberry -


i trying draw list of contacts saved in device. fine when select contacts, contacts drawn on screen. in other words, list drawing contacts visible on screen. remaining contacts have scroll list.

here code:

public class checkboxlistfield extends verticalfieldmanager implements listfieldcallback, fieldchangelistener {     private static vector selectedcontacts ;     private checklistdata[] mlistdata = new checklistdata[] {};     private listfield mlistfield;     private static vector mcontacts;     private contactlist contactlist;     private enumeration allcontacts;     private sendemail sendemail;     private boolean ischecked=false;     private blackberrycontact contactitem;     private verticalfieldmanager _mainvfm = new verticalfieldmanager();     private int i;     private int j=0;     private string emails="";     private buttonfield _invitebutton;     private horizontalfieldmanager selectallhfm;     private custombuttonfield selectallbutton;     private bitmap _uncheckbmp;     private bitmap _checkbmp;     private labelfield selectalllabel;     private checkboxfield selectallcheckbox;     private verticalfieldmanager contactlistvfm;     private boolean listitemchecked=false;     private stringbuffer rowstring;     private boolean getcboxstatus;      // class hold strings in checkbox , it's checkbox state     // (checked or unchecked).     private class checklistdata {         private string _stringval;         private boolean _checked;         private string _telnumber;          checklistdata(string stringval, boolean checked) {             _stringval = stringval;             _checked = checked;             //_telnumber = telnumber;          }          // get/set methods.         private string getstringval() {             return _stringval;         }          private boolean ischecked() {             return _checked;         }           // toggle checked status.         public void togglechecked() {             _checked = !_checked;         }     }       public checkboxlistfield() {          _mainvfm.add(createcontactlist(ischecked));         add(_mainvfm);      }      public verticalfieldmanager createcontactlist(boolean checked){         ischecked = checked;         selectedcontacts = new vector();          //invite button         contactlistvfm = new verticalfieldmanager();         _invitebutton=new buttonfield("invite friend");         _invitebutton.setchangelistener(this);         _invitebutton.setmargin(2,0,10,0);          //select checkbox          selectallhfm = new horizontalfieldmanager();         _uncheckbmp = bitmap.getbitmapresource("uncheck.png");         _checkbmp = bitmap.getbitmapresource("checked.png");         selectallbutton = new custombuttonfield(29, "", _uncheckbmp, _checkbmp, buttonfield.consume_click);         selectallbutton.setchangelistener(this);         selectallbutton.setmargin(5,5,5,5);          selectallcheckbox = new checkboxfield("select all", ischecked){             protected boolean navigationclick(int status,                     int time) {                 selectedcontacts = new vector();                 emails = "";                 boolean getcboxstatus = selectallcheckbox.getchecked();                  if(listitemchecked == false){                     if(_mainvfm.getfieldcount()!= 0){                         _mainvfm.deleteall();                         _mainvfm.add(createcontactlist(getcboxstatus));                     }                 }                  return true;             }         };         selectallcheckbox.setchangelistener(this);          selectalllabel = new labelfield("select all");         selectalllabel.setmargin(5,5,5,5);          selectallhfm.add(selectallcheckbox);         //selectallhfm.add(selectalllabel);           // toggle list field item on navigation click           mlistfield = new listfield() {             protected boolean navigationclick(int status,                     int time) {                 toggleitem();                 return true;             };          };         // set 2 line row height         //mlistfield.setrowheight(getfont().getheight() * 2);         mlistfield.setcallback(this);         //contactlistvfm.add(new nullfield(nullfield.focusable));         contactlistvfm.add(_invitebutton);         contactlistvfm.add(selectallhfm);         contactlistvfm.add(new separatorfield());         contactlistvfm.add(mlistfield);          //load contacts         // load contacts in separate thread         loadcontacts.run();          return contactlistvfm;     }       protected runnable loadcontacts = new runnable() {         public void run() {             reloadcontactlist();             // fill list field control in ui event thread             uiapplication.getuiapplication().invokelater(                     filllist);         }     };      protected runnable filllist = new runnable() {         public void run() {             int size = mcontacts.size();             mlistdata = new checklistdata[size];             (int =0; < mcontacts.size() ; i++) {                 contactitem = (blackberrycontact) mcontacts                 .elementat(i);                  string displayname = getdisplayname(contactitem);                  //  string telcontact = getcontact(item);                 mlistdata[i] = new checklistdata(                         displayname, ischecked);                 mlistfield.invalidate(i);                 system.out.println(">>>>>>>>>"+mlistdata[i]);             }             mlistfield.setsize(size);             //invalidate();          }     };         protected void toggleitem() {         listitemchecked = true ;         selectallcheckbox.setchecked(false);          listitemchecked =false ;         // index of selected row.         int index = mlistfield.getselectedindex();         system.out.println("..............."+index);          if (index != -1) {             // checklistdata row.             checklistdata data = mlistdata[index];              // toggle status.             data.togglechecked();             mlistfield.invalidate(index);          }     }        private boolean reloadcontactlist() {         try {             contactlist = (contactlist) pim             .getinstance()             .openpimlist(pim.contact_list,                     pim.read_only);              allcontacts = contactlist.items();             mcontacts = enumtovector(allcontacts);             mlistfield.setsize(mcontacts.size());             system.out.println(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,>>>>>>>>>>"+mlistfield.getsize());             return true;         } catch (pimexception e) {             return false;         }     }      // convert list of contacts enumeration vector     private vector enumtovector(enumeration contactenum) {          vector v = new vector();          if (contactenum == null)             return v;          while (contactenum.hasmoreelements()){             contact contact = (contact) allcontacts.nextelement();             if(contactlist.issupportedfield(contact.email)&& (contact.countvalues(contact.email) > 0)) {                 string emailid=contact.getstring(contact.email, 0);                 if(emailid.length() !=0 && emailid != null ){                     v.addelement(contact);                 }             }         }         return v;     }       public void drawlistrow(listfield list,             graphics graphics, int index, int y, int w) {         rowstring = new stringbuffer();         object obj = this.get(list, index);         if (list.getselectedindex() != index) {             graphics.setbackgroundcolor(index % 2 == 0 ||index==0 ? color.white                     : color.lightgray);             graphics.clear();             //list.setfocus();         }          blackberrycontact contact = (blackberrycontact) mcontacts         .elementat(index);          string email= contact.getstring(contact.email, 0);         int vecindex = selectedcontacts.indexof(email);           if (obj != null) {             checklistdata currentrow = (checklistdata) obj;               if (currentrow.ischecked()) {                 if(vecindex == -1){                     selectedcontacts.addelement(email);                 }                 rowstring                 .append(characters.ballot_box_with_check);             } else {                 selectedcontacts.removeelement(email);                 rowstring.append(characters.ballot_box);             }             // append couple spaces , row's text.             rowstring.append(characters.space);             rowstring.append(characters.space);             rowstring.append(currentrow.getstringval());             // draw text.           }          graphics.drawtext(rowstring.tostring(), 0, y,                 0, w);      }      public static string getdisplayname(contact contact) {         if (contact == null) {             return null;         }         string displayname = null;         // first, see if there meaningful name set contact.         if (contact.countvalues(contact.name) > 0) {             final string[] name = contact.getstringarray(                     contact.name, 0);             final string firstname = name[contact.name_given];             final string lastname = name[contact.name_family];             if (firstname != null && lastname != null) {                 displayname = firstname + " " + lastname;             } else if (firstname != null) {                 displayname = firstname;             } else if (lastname != null) {                 displayname = lastname;             }             if (displayname != null) {                 final string nameprefix = name[contact.name_prefix];                 if (nameprefix != null) {                     displayname = nameprefix + " "                     + displayname;                 }                 return displayname;             }         }         return displayname;     }      // returns object @ specified index.     public object get(listfield list, int index) {         object result = null;         if (mlistdata.length > index) {             result = mlistdata[index];         }         system.out.println(",,,,,,,,,,,,,,,,,,,,,,,"+mlistdata.length);         return result;     }      // returns first occurrence of given string,     // beginning search @ index, , testing     // equality using equals method.     public int indexoflist(listfield list, string p, int s) {         return -1;     }      // returns screen width list uses entire screen width.     public int getpreferredwidth(listfield list) {         return graphics.getscreenwidth();         // return display.getwidth();     }       public void fieldchanged(field field, int context) {          if(field==_invitebutton){              for(int n=0 ; n<selectedcontacts.size() ; n++){                  emails= emails + selectedcontacts.elementat(n)+",";              }             //}             string mailbody =": "+jxa.loginusername+" invited on naijapings app. please download naijapings android app here "+"http://appworld.blackberry.com/webstore/content/77264/?lang=en" ;             sendemail=new sendemail(mailbody);             sendemail.email(emails,constant.emailsubject);             emails ="" ;             selectedcontacts.removeallelements();           }else if(field == selectallcheckbox){              selectedcontacts = new vector();             emails = "";             getcboxstatus = selectallcheckbox.getchecked();             //selectedcontacts.removeallelements();              if(listitemchecked == false){                 if(_mainvfm.getfieldcount()!= 0){                     _mainvfm.deleteall();                     _mainvfm.add(createcontactlist(getcboxstatus));                 }             }          }     }  } 

here ,in drawlistrow() , get() method called many times number of contacts visible on screen. remaining contact add, have scroll list.

in drawlistrow() method adding contacts selectedcontacts vector , using vector contact send mail. contacts added when particular list item drawn.

so, how can selected contact without scrolling list?

this similar problem had in 1 of other recent questions. problem drawlistrow() callback designed let draw rows need drawing. it's not meant else, assembling list of contacts email.

the blackberry os tries efficient, ask drawlistrow() rows visible user (on screen). more wasteful.

so, if want assemble list of selected rows, should somewhere else, not in drawlistrow().

it looks me can build list of selected rows using code, wherever want:

public vector getselectedcontacts() {      selectedcontacts.removeallelements();      (int = 0; < mlistdata.length; i++) {         object obj = mlistdata[i];         if (obj != null) {              blackberrycontact contact = (blackberrycontact) mcontacts.elementat(i);             string email = contact.getstring(contact.email, 0);             int vecindex = selectedcontacts.indexof(email);              checklistdata currentrow = (checklistdata) obj;              if (currentrow.ischecked()) {                 if(vecindex == -1){                     selectedcontacts.addelement(email);                 }             } else {                 // line not needed, since                  //   call removeallelements() @ start of method                 selectedcontacts.removeelement(email);             }         }      }      return selectedcontacts; } 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -