java - Android Type mismatch help. Convert ArrayList to List? -


ok.. might hard follow..

app saves contacts sqlite database named contacts.

contactactivity should load in sqlite database , create custom listview allows selection of multiple contacts via toggle button.

the example following create custom lv allowing multiple selections has local contact class. running problems converting external dao contact class local contact class.

contact.java dao class:

package dto;  /**  * dto describes contact can create , respond survey  * @author barnitek, bosticsa  *  */  public class contact {  private int id; private string firstname; private string lastname; private string phonenumber;  public contact(){  }  public contact(int id, string firstname, string lastname, string phonenumber){     this.id = id;     this.firstname = firstname;     this.lastname = lastname;     this.phonenumber = phonenumber; }  public contact(string firstname, string lastname, string phonenumber){     this.firstname = firstname;     this.lastname = lastname;     this.phonenumber = phonenumber; }  public int getid(){     return id; }  public void setid(int id){     this.id = id; }  public string getfirstname() {     return firstname; } public void setfirstname(string firstname) {     this.firstname = firstname; } public string getlastname() {     return lastname; } public void setlastname(string lastname) {     this.lastname = lastname; } public string getphonenumber() {     return phonenumber; } public void setphonenumber(string phonenumber) {     this.phonenumber = phonenumber; } } 

the contactactivity class:

package edu.uc.pollyo;  import java.util.arraylist; import java.util.list;  import android.app.activity; import android.content.context; import android.content.intent; import android.os.bundle; import android.view.layoutinflater; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.checkbox; import android.widget.listview; import android.widget.textview; import dao.pollyobasedao;  /**  *   * @author barnitek  *   *         activity builds custom list view lists array of names  *         , phone numbers checkable boxes. when box checks  *         name+number row added string  *         returned mainactivity , placed in contacts text view.  *   */  public class contactactivity extends mainactivity { private listview mainlistview; private arrayadapter<contact> listadapter; private list<contact> contactlist; private arraylist<dto.contact> contactarraylist;  public static string listcontacts = ""; pollyobasedao mydb;   // when activity created @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_contact);      //contactlist = new arraylist<dto.contact>();     mydb = new pollyobasedao(this);     contactarraylist = mydb.getallcontacts();       //**************probblem here***************     // problem is: type mismatch: cannot convert arraylist<contact>    //  list<contactactivity.contact>     contactlist = contactarraylist;      // set our custom array adapter listview's adapter.     listadapter = new contactarrayadapter( getapplicationcontext(), contactlist);     // find listview resource.     mainlistview = (listview) findviewbyid(r.id.lvcontactview);     mainlistview.setadapter(listadapter);      // when item tapped, toggle checked properties of checkbox ,     // contact.     mainlistview             .setonitemclicklistener(new adapterview.onitemclicklistener() {                 @override                 public void onitemclick(adapterview<?> parent, view item,                         int position, long id) {                     contact contact = listadapter.getitem(position);                     contact.togglechecked();                     contactviewholder viewholder = (contactviewholder) item                             .gettag();                     viewholder.getcheckbox()                             .setchecked(contact.ischecked());                      // build string of selected contacts when 1                     // checked                     listcontacts += contact.getfirstname() + " " + contact.getlastname() + " | ";                     system.out.println("listcontacts: (contactactivity) "                             + listcontacts);                  }             });      mainlistview.setadapter(listadapter); }  // holds contact data private static class contact {     private string firstname = "";     private string lastname = "";      private boolean checked = false;      public contact(){};      public contact(string firstname, string lastname){         this.firstname = firstname;         this.lastname = lastname;     }      public contact (string firstname, string lastname, boolean checked){         this.firstname = firstname;         this.lastname = lastname;         this.checked = checked;     }      public string getfirstname() {         return firstname;     }      public string getlastname(){         return lastname;     }      public boolean ischecked() {         return checked;     }      public void setchecked(boolean checked) {         this.checked = checked;     }      public string tostring() {         return firstname;     }      public void togglechecked() {         checked = !checked;     } }  // holds child views each row private static class contactviewholder {     private checkbox checkbox;     private textview textview;      public contactviewholder(){};      public contactviewholder(textview textview, checkbox checkbox) {         this.checkbox = checkbox;         this.textview = textview;     }      public checkbox getcheckbox() {         return checkbox;     }      public textview gettextview() {         return textview;     }      public void settextview(textview textview){         this.textview = textview;     } }  /** custom adapter displaying array of contact objects. */ private static class contactarrayadapter extends arrayadapter<contact> {      private layoutinflater inflater;      public contactarrayadapter(context context, list<contact> contactlist) {         super(context, r.layout.simplerow, r.id.rowtextview, contactlist);         // cache layoutinflate avoid asking new 1 each time.         inflater = layoutinflater.from(context);     }      @override     public view getview(int position, view convertview, viewgroup parent) {         // contact display         contact contact = (contact) this.getitem(position);          // child views in each row.         checkbox checkbox;         textview textview;          // create new row view         if (convertview == null) {             convertview = inflater.inflate(r.layout.simplerow, parent);              // find child views.             textview = (textview) convertview                     .findviewbyid(r.id.rowtextview);             checkbox = (checkbox) convertview.findviewbyid(r.id.checkbox01);              // optimization: tag row it's child views, don't             // have             // call findviewbyid() later when reuse row.             convertview.settag(new contactviewholder(textview, checkbox));              // if checkbox toggled, update contact tagged with.             checkbox.setonclicklistener(new view.onclicklistener() {                 public void onclick(view v) {                     checkbox cb = (checkbox) v;                     contact contact = (contact) cb.gettag();                     contact.setchecked(cb.ischecked());                 }             });         }         // reuse existing row view         else {             // because use viewholder, avoid having call             // findviewbyid().             contactviewholder viewholder = (contactviewholder) convertview                     .gettag();             checkbox = viewholder.getcheckbox();             textview = viewholder.gettextview();         }          // tag checkbox contact displaying,         // can         // access contact in onclick() when checkbox toggled.         checkbox.settag(contact);          // display contact data         checkbox.setchecked(contact.ischecked());         textview.settext(contact.getfirstname() + " " + contact.getlastname());          return convertview;     } }  @override public boolean onoptionsitemselected(menuitem item) {     // handle item selection     switch (item.getitemid()) {     case r.id.help:         showhelp();         return true;     case r.id.about:         showabout();         return true;     default:         return super.onoptionsitemselected(item);     } }  // launch activity private void showabout() {     intent = new intent(this, aboutactivity.class);     startactivity(about); }  // launch activity private void showhelp() {     intent = new intent(this, helpactivity.class);     startactivity(help); }  // when user clicks ok, send list of selected contacts public void onokclicked(view v) {     intent output = new intent();     output.putextra("selectedcontacts", listcontacts);     setresult(activity.result_ok, output);     finish(); }  // when user clicks new contact, start new activity add new // contact list public void onnewcontactclicked(view v) {     intent newcontact = new intent(this, addcontactactivity.class);     startactivity(newcontact);  } } 

the getallcontacts() method, incase needed:

public arraylist<contact> getallcontacts()        {           arraylist<contact> array_list = new arraylist<contact>();            string selectquery = "select * " + contacts;            sqlitedatabase db = this.getwritabledatabase();            cursor res =  db.rawquery( selectquery, null );            if (res.movetofirst()) {                 {                     contact contact = new contact();                     contact.setid(integer.parseint(res.getstring(0)));                     contact.setfirstname(res.getstring(1));                     contact.setlastname(res.getstring(2));                     contact.setphonenumber(res.getstring(3));                     // adding contact list                     array_list.add(contact);                 } while (res.movetonext());             }              // return contact list             return array_list;        } 

does have idea on way this? or easier way read in sqlite database , select multiple objects it?

you have write method in contactactivity looks this:

i wrote dto.contact , contactactiviy.contact make clear, 1 which

private list<contactactivity.contact> transformlistfromdb(list<dto.contact> listfromdb) {     list<contactactivity.contact> result = new arraylist<contactactivity.contact>();     (dto.contact con : listfromdb) {          result.add(new contactactivity.contact(con.firstname, con.lastname);      }     return result; } 

then use: contactlist = transformlistfromdb(contactarraylist);


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -