android - How do i get the position of a URL in an array? -
what i'm trying achieve obtain url received instagram , pass array position onlick listener each corresponding photo in grid view.
here code:
mainactivity.java
public class mainactivity extends activity { private instagramsession minstagramsession; private instagram minstagram; private progressbar mloadingpb; private gridview mgridview; private static final string client_id = "83549f9eb76f4a5b90daf6e4e14da107"; private static final string client_secret = "6df26b0c8f664323a07126bfe8511651"; private static final string redirect_uri = "http://www.yahoo.com"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); minstagram = new instagram(this, client_id, client_secret, redirect_uri); minstagramsession = minstagram.getsession(); if (minstagramsession.isactive()) { setcontentview(r.layout.activity_user); instagramuser instagramuser = minstagramsession.getuser(); mloadingpb = (progressbar) findviewbyid(r.id.pb_loading); mgridview = (gridview) findviewbyid(r.id.gridview); ((textview) findviewbyid(r.id.tv_name)).settext(instagramuser.fullname); ((textview) findviewbyid(r.id.tv_username)).settext(instagramuser.username); ((button) findviewbyid(r.id.btn_logout)).setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { minstagramsession.reset(); startactivity(new intent(mainactivity.this, mainactivity.class)); finish(); } }); imageview useriv = (imageview) findviewbyid(r.id.iv_user); displayimageoptions displayoptions = new displayimageoptions.builder() .showimageonloading(r.drawable.ic_user) .showimageforemptyuri(r.drawable.ic_user) .showimageonfail(r.drawable.ic_user) .cacheinmemory(true) .cacheondisc(false) .considerexifparams(true) .build(); imageloaderconfiguration config = new imageloaderconfiguration.builder(this) .writedebuglogs() .defaultdisplayimageoptions(displayoptions) .build(); imageloader imageloader = imageloader.getinstance(); imageloader.init(config); animatefirstdisplaylistener animate = new animatefirstdisplaylistener(); imageloader.displayimage(instagramuser.profilpicture, useriv, animate); new downloadtask().execute(); } else { setcontentview(r.layout.activity_main); ((button) findviewbyid(r.id.btn_connect)).setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { minstagram.authorize(mauthlistener); } }); } } private void showtoast(string text) { toast.maketext(getapplicationcontext(), text, toast.length_long).show(); } private instagram.instagramauthlistener mauthlistener = new instagram.instagramauthlistener() { @override public void onsuccess(instagramuser user) { finish(); startactivity(new intent(mainactivity.this, mainactivity.class)); } @override public void onerror(string error) { showtoast(error); } @override public void oncancel() { showtoast("ok. maybe later?"); } }; public static class animatefirstdisplaylistener extends simpleimageloadinglistener { static final list<string> displayedimages = collections.synchronizedlist(new linkedlist<string>()); @override public void onloadingcomplete(string imageuri, view view, bitmap loadedimage) { if (loadedimage != null) { imageview imageview = (imageview) view; boolean firstdisplay = !displayedimages.contains(imageuri); if (firstdisplay) { fadeinbitmapdisplayer.animate(imageview, 500); displayedimages.add(imageuri); } } } } arraylist<string> photolist; public class downloadtask extends asynctask<url, integer, long> { protected void oncancelled() { } protected void onpreexecute() { } protected long doinbackground(url... urls) { long result = 0; try { list<namevaluepair> params = new arraylist<namevaluepair>(1); params.add(new basicnamevaluepair("count", "20")); instagramrequest request = new instagramrequest(minstagramsession.getaccesstoken()); string response = request.createrequest("get", "/users/21846697/media/recent", params); if (!response.equals("")) { jsonobject jsonobj = (jsonobject) new jsontokener(response).nextvalue(); jsonarray jsondata = jsonobj.getjsonarray("data"); int length = jsondata.length(); if (length > 0) { photolist = new arraylist<string>(); (int = 0; < length; i++) { jsonobject jsonphoto = jsondata.getjsonobject(i).getjsonobject("images").getjsonobject("standard_resolution"); photolist.add(jsonphoto.getstring("url")); } } } } catch (exception e) { e.printstacktrace(); } return result; } protected void onprogressupdate(integer... progress) { } protected void onpostexecute(long result) { mloadingpb.setvisibility(view.gone); if (photolist == null) { toast.maketext(getapplicationcontext(), "no photos available", toast.length_long).show(); } else { displaymetrics dm = new displaymetrics(); getwindowmanager().getdefaultdisplay().getmetrics(dm); int width = (int) math.ceil((double) dm.widthpixels / 2); width=width-50; int height = width; photolistadapter adapter = new photolistadapter(mainactivity.this); adapter.setdata(photolist); adapter.setlayoutparam(width, height); mgridview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // maybe you'll have cast string, i'm writing directly in string url = (string) parent.getadapter().getitem(position); intent myintent=new intent(mainactivity.this, fullscreenimageview.class).putextra("selectedimageurl", position); startactivity(myintent); } }); mgridview.setadapter(adapter); } } } }
photolistadapter.java
public class photolistadapter extends baseadapter { private context mcontext; private imageloader mimageloader; private animatefirstdisplaylistener manimator; private arraylist<string> mphotolist; private int mwidth; private int mheight; public photolistadapter(context context) { mcontext = context; displayimageoptions displayoptions = new displayimageoptions.builder() .showimageonloading(r.drawable.instagram_logo) .showimageforemptyuri(r.drawable.instagram_logo) .showimageonfail(r.drawable.instagram_logo) .cacheinmemory(true) .cacheondisc(false) .considerexifparams(true) .build(); imageloaderconfiguration config = new imageloaderconfiguration.builder(context) .writedebuglogs() .defaultdisplayimageoptions(displayoptions) .build(); mimageloader = imageloader.getinstance(); mimageloader.init(config); manimator = new animatefirstdisplaylistener(); } public void setdata(arraylist<string> data) { mphotolist = data; } public void setlayoutparam(int width, int height) { mwidth = width; mheight = height; } @override public int getcount() { return (mphotolist == null) ? 0 : mphotolist.size(); } @override public object getitem(int position) { return null; } @override public long getitemid(int position) { return 0; } @override public view getview(int position, view convertview, viewgroup parent) { imageview imageiv; if (convertview == null) { imageiv = new imageview(mcontext); imageiv.setlayoutparams(new gridview.layoutparams(mwidth, mheight)); imageiv.setscaletype(imageview.scaletype.center_inside); imageiv.setpadding(0, 0, 0, 0); } else { imageiv = (imageview) convertview; } mimageloader.displayimage(mphotolist.get(position), imageiv, manimator); return imageiv; } }
fullscreenimageview.java
import android.app.activity; public class fullscreenimageview extends activity { string imageurl = getintent().getstringextra("selectedimageurl"); }
the workflow of gridview understood have adapter populate view.
what getitem(int position) returns url which, if understood, contained in mphotolist. like:
@override public object getitem(int position) { return mphotolist.get(position); }
and use need this:
@override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // maybe you'll have cast string, i'm writing directly in string url = parent.getadapter().getitem(position) intent myintent=new intent(mainactivity.this, fullscreenimageview.class).putextra("selectedimageurl", url); startactivity(myintent); } });
hope helps.
Comments
Post a Comment