android - i dont know how to get the position of the img that i click so i can pass it to next acitvity -
my grid view working okay , displays image sdcard.when long press image contextual task box open.one of option view(to view in full screen).i dont know how id or postion of selected image can pass activity open in fullscreen.
public class mainactivity extends activity {
public class imageadapter extends baseadapter { private context mcontext; arraylist<string> itemlist = new arraylist<string>(); public imageadapter(context c) { mcontext = c; } void add(string path){ itemlist.add(path); } @override public int getcount() { return itemlist.size(); } @override public object getitem(int position) { // todo auto-generated method stub return itemlist.get(position); } @override public long getitemid(int position) { // todo auto-generated method stub return 0; } @override public view getview(int position, view convertview, viewgroup parent) { imageview imageview; if (convertview == null) { // if it's not recycled, initialize attributes imageview = new imageview(mcontext); imageview.setlayoutparams(new gridview.layoutparams(165, 165)); imageview.setscaletype(imageview.scaletype.center_crop); //imageview.setpadding(8, 8, 8, 8); } else { imageview = (imageview) convertview; } bitmap bm = decodesampledbitmapfromuri(itemlist.get(position), 250, 250); imageview.setimagebitmap(bm); return imageview; } public bitmap decodesampledbitmapfromuri(string path, int reqwidth, int reqheight) { bitmap bm = null; // first decode injustdecodebounds=true check dimensions final bitmapfactory.options options = new bitmapfactory.options(); options.injustdecodebounds = true; bitmapfactory.decodefile(path, options); // calculate insamplesize options.insamplesize = calculateinsamplesize(options, reqwidth, reqheight); // decode bitmap insamplesize set options.injustdecodebounds = false; bm = bitmapfactory.decodefile(path, options); return bm; } public int calculateinsamplesize( bitmapfactory.options options, int reqwidth, int reqheight) { // raw height , width of image final int height = options.outheight; final int width = options.outwidth; int insamplesize = 1; if (height > reqheight || width > reqwidth) { if (width > height) { insamplesize = math.round((float)height / (float)reqheight); } else { insamplesize = math.round((float)width / (float)reqwidth); } } return insamplesize; } } imageadapter myimageadapter; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); gridview gridview = (gridview) findviewbyid(r.id.gridview); myimageadapter = new imageadapter(this); gridview.setadapter(myimageadapter); string externalstoragedirectorypath = environment .getexternalstoragedirectory() .getabsolutepath(); string targetpath = externalstoragedirectorypath ; toast.maketext(getapplicationcontext(), targetpath, toast.length_long).show(); file targetdirector = new file(targetpath); file[] files = targetdirector.listfiles(); (file file : files){ myimageadapter.add(file.getabsolutepath()); } registerforcontextmenu(gridview); } @override public void oncreatecontextmenu(contextmenu menu, view v,contextmenuinfo menuinfo) { super.oncreatecontextmenu(menu, v, menuinfo); menu.setheadertitle("context menu"); menu.add(0, v.getid(), 0, "view"); menu.add(0, v.getid(), 0, "details"); menu.add(0, v.getid(), 0, "delete"); } @override public boolean oncontextitemselected(menuitem item) { if(item.gettitle()=="view"){function1(item.getitemid());} else if(item.gettitle()=="details"){function2(item.getitemid());} else if(item.gettitle()=="delete"){function3(item.getitemid());} else {return false;} return true; } public void function1(int id){ // sending image id fullscreenactivity intent = new intent(getapplicationcontext(), fullimageactivity.class); // here problem want position or id of clicked strong textimage , pass other activity can view full screen i.putextra("id", position); startactivity(i); toast.maketext(this, "function 1 called", toast.length_short).show(); }
}`
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); gridview gridview = (gridview) findviewbyid(r.id.gridview); myimageadapter = new imageadapter(this); gridview.setadapter(myimageadapter); gridview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { system.out.println("position"+position); } } }); string externalstoragedirectorypath = environment .getexternalstoragedirectory() .getabsolutepath(); string targetpath = externalstoragedirectorypath ; toast.maketext(getapplicationcontext(), targetpath, toast.length_long).show(); file targetdirector = new file(targetpath); file[] files = targetdirector.listfiles(); (file file : files){ myimageadapter.add(file.getabsolutepath()); } registerforcontextmenu(gridview); }
Comments
Post a Comment