java - is not an enclosing class / new Intent Cannot Resolve Constructor -


thanks checking out question!

i have relativly ismple loop add onclicklistener each imageview add layout, when trying add new intent it, gives me 1 of following errors:

    string[] imageurls = imageurlsstring.split("/");     (int = 0; < imageurls.length; i++){         imageview image = new imageview(this);         image.setlayoutparams(new android.view.viewgroup.layoutparams(getpx(182),getpx(256)));         image.setpadding(getpx(3),getpx(3),getpx(3),getpx(3));         final string imageurl = ".../images/" + imageurls[i];         picasso.with(this).load(imageurl).into(image);          image.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view view) {                 intent intent;                 intent = new intent(this, imageactivity.class);                 intent.putextra("url", imageurl);                 startactivity(intent);             }         });         linearlayout.addview(image);     } 

will result in "cannot resolve constructor 'intent ...." error. when looking solution, people suggested change "this" "mainactivity.this", but...

    string[] imageurls = imageurlsstring.split("/");     (int = 0; < imageurls.length; i++){         imageview image = new imageview(this);         image.setlayoutparams(new android.view.viewgroup.layoutparams(getpx(182),getpx(256)));         image.setpadding(getpx(3),getpx(3),getpx(3),getpx(3));         final string imageurl = ".../images/" + imageurls[i];         picasso.with(this).load(imageurl).into(image);          image.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view view) {                 intent intent;                 intent = new intent(mainactivity.this, imageactivity.class);                 intent.putextra("url", imageurl);                 startactivity(intent);             }         });         linearlayout.addview(image);     } 

resulted into: com.myname.appname.mainactivity not enclosing class

here imageactivity class:

public class imageactivity extends appcompatactivity {      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_image);          string url = getintent().getextras().getstring("url");          imageview imageview = (imageview) findviewbyid(r.id.image);         imageview.getlayoutparams().height = (int) (imageview.getlayoutparams().width * (float.valueof(string.valueof(1.41))));         picasso.with(this).load(url).into(imageview);     } 

the weird thing have done numerous new intent's during play time creating apps, cant seem solve one. missing here?

full code here

public class detailsactivity extends appcompatactivity {       textview tvwechatidvalue;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_details);          string datastring = getintent().getextras().getstring("datastring");         string[] itemdata = datastring.split(",");         string category = itemdata[0];         log.e("detailsactivity", category);         string itemid = itemdata[1];         log.e("detailsactivity", itemid);         string imageurlsstring = itemdata[2];         log.e("detailsactivity", imageurlsstring);         string description = itemdata[3];         log.e("detailsactivity", description);         string price = itemdata[4];         log.e("detailsactivity", price);          string itemcode = category + itemid;          textview textview;         textview = (textview) findviewbyid(r.id.title);         textview.settext(getresources().getstring(r.string.app_name));         textview = (textview) findviewbyid(r.id.itemreferralvalue);         textview.settext(itemcode);         textview = (textview) findviewbyid(r.id.itempricevalue);         textview.settext(price);          tvwechatidvalue = (textview) findviewbyid(r.id.wechatidvalue);         new databasetask(this, "details", "getwechatid").execute();          linearlayout linearlayout = (linearlayout) findviewbyid(r.id.itemdetailscontent);          string[] imageurls = imageurlsstring.split("/");         (int = 0; < imageurls.length; i++){             imageview image = new imageview(this);             image.setlayoutparams(new android.view.viewgroup.layoutparams(getpx(182),getpx(256)));             image.setpadding(getpx(3),getpx(3),getpx(3),getpx(3));             final string imageurl = ".../images/" + imageurls[i];             picasso.with(this).load(imageurl).into(image);              image.setonclicklistener(new view.onclicklistener() {                 @override                 public void onclick(view view) {                     intent intent;                     intent = new intent(this, imageactivity.class);                     intent.putextra("url", imageurl);                     startactivity(intent);                 }             });             linearlayout.addview(image);         }          imageview imageview = (imageview) findviewbyid(r.id.itemdetailsdescription);         string imageurl = ".../images/" + description;         picasso.with(this).load(imageurl).into(imageview);      }      public void setwechatid(string mvalue) {         tvwechatidvalue.settext(mvalue);     }      public int getpx(int dimensiondp) {         float density = getresources().getdisplaymetrics().density;         return (int) (dimensiondp * density + 0.5f);     } } 

working code

public class mainactivity extends appcompatactivity {      textview tvwechatidvalue;     linearlayout linearlayout;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          textview textview;         textview = (textview) findviewbyid(r.id.title);         textview.settext(getresources().getstring(r.string.app_name));         textview = (textview) findviewbyid(r.id.subtitle);         textview.settext(getresources().getstring(r.string.slogan));          textview = (textview) findviewbyid(r.id.moreforwomen);         textview.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view view) {                 opencatalog("women");             }         });         textview = (textview) findviewbyid(r.id.moreformen);         textview.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view view) {                 opencatalog("men");             }         });         textview = (textview) findviewbyid(r.id.moreforkids);         textview.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view view) {                 opencatalog("kids");             }         });     }      public void getdata() {             tvwechatidvalue = (textview) findviewbyid(r.id.wechatidvalue);             new databasetask(this, "main", "getwechatid").execute();              linearlayout = (linearlayout) findviewbyid(r.id.newitemsforwomencontent);             linearlayout.removeallviewsinlayout();             new databasetask(this, linearlayout, "women", "getnewestitems").execute();             linearlayout = (linearlayout) findviewbyid(r.id.newitemsformencontent);             linearlayout.removeallviewsinlayout();             new databasetask(this, linearlayout, "men", "getnewestitems").execute();             linearlayout = (linearlayout) findviewbyid(r.id.newitemsforkidscontent);             linearlayout.removeallviewsinlayout();             new databasetask(this, linearlayout, "kids", "getnewestitems").execute();     }      public void setwechatid(string mvalue) {         tvwechatidvalue.settext(mvalue);     }      public void processnewitems(linearlayout mlinearlayout, final string mcategory, final hashmap<integer, item> mitems){         (int = 0; < mitems.size(); i++) {             imageview image = new imageview(this);             image.setlayoutparams(new android.view.viewgroup.layoutparams(getpx(96),getpx(128)));             image.setpadding(getpx(3),getpx(3),getpx(3),getpx(3));             string imageurl = ".../images/" + mitems.get(i).getimageurls()[0];             picasso.with(this).load(imageurl).into(image);              mlinearlayout.addview(image);             final int index = i;             image.setonclicklistener(new view.onclicklistener() {                 @override                 public void onclick(view view) {                     showitemdetails(mcategory, mitems.get(index));                 }             });         }     }      void showitemdetails(string mcategory, item mitem){         intent intent = new intent(this, detailsactivity.class);         intent.putextra("datastring", mitem.getdatastring());         log.e("catalogactivity", mitem.getdatastring());         startactivity(intent);     }      void opencatalog(string mcategory){         //show catalog         intent intent = new intent(this, catalogactivity.class);         intent.putextra("category", mcategory);         startactivity(intent);     }      public int getpx(int dimensiondp) {         float density = getresources().getdisplaymetrics().density;         return (int) (dimensiondp * density + 0.5f);     }      @override     public void onresume() {         super.onresume();          getdata();     }  } 

you need use name of activity subclass contains code creates onclicklistener. in many examples, mainactivity. however, doesn't seem case in code. since in class named detailsactivity use name:

intent intent = new intent(detailsactivity.this, imageactivity.class); 

from can see of code here, suggest learn listview , recyclerview. bit complex, once understand them, of work similar trying here. 1 thing, more efficient own code because create many views visible.

let's more closely @ other example:

public class mainactivity extends appcompatactivity {      // ...     void showitemdetails(string mcategory, item mitem){         intent intent = new intent(this, detailsactivity.class);         intent.putextra("datastring", mitem.getdatastring());         startactivity(intent);      } } 

notice showitemdetails() inside mainactivity class. this refers instance of class can used wherever context needed. (for more details need read inheritance , polymorphism.)

on other hand, original code creates intent in method inside anonymous subclass of onclicklistener cannot used context. anonymous inner class in method inside detailsactivity inherits context. in order access instance of outer class, have use detailsactivity.this.

for more details should learn special this reference , inner classes.


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -