android - OnClickItem() in gridView how do I show the same image in a separate imageView -
the gridview being populated contents of specific folder called "mydir". layout gallery_fragment split in half 2 linearlayouts. 1 on left has gridview populated images "mydir". layout on right has imageview.
the idea can select image gridview on left , appear on imageview on right. cannot use res/drawables folder in instance.
i have been told need use hashmap hmap; have suggestions?
public class galleryfragment4 extends fragment { private imageview imageview; private imagegridviewadapter imageadapter; private gridview gridview; private static final string gridview_tag = "android logo"; @suppresslint("usesparsearrays") public hashmap<integer, string> hmap = new hashmap<integer, string>(); // private linearlayout linear; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_gallery, container, false); // assigns gridview object gridview = (gridview) rootview.findviewbyid(r.id.gridview); gridarchitecture(rootview); extractfiles(); /** * gridview waiting image selected */ gridview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // image number int num = (position + 1); // when image selected show image number toast.maketext(getactivity().getapplicationcontext(), "image : " + num, toast.length_short).show(); ((context) getcameraimages(getactivity())).getapplicationcontext(); try{ imageview.setimageresource(position); }catch(nullpointerexception ex){ } } }); return rootview; } /* * save image external sd card , create new file if said file not * created */ public static final string directory_path = environment .getexternalstoragedirectory().tostring() + "/mydir/"; // public static final string camer_image_bucket_id = getbucketid(directory_path); /** * matches code in mediaprovider.computebucketvalues. should common * function. */ // public static string getbucketid(string path) { // return string.valueof(path.tolowercase().hashcode()); // } /** * retrieve camera images * * @param context * @return */ public static list<string> getcameraimages(context context) { final string[] projection = { mediastore.images.media.data }; final string selection = mediastore.images.media.bucket_id + " = ?"; final string[] selectionargs = { directory_path }; final cursor cursor = context.getcontentresolver().query( images.media.external_content_uri, projection, selection, selectionargs, null); arraylist<string> result = new arraylist<string>(cursor.getcount()); if (cursor.movetofirst()) { final int datacolumn = cursor .getcolumnindexorthrow(mediastore.images.media.data); { final string data = cursor.getstring(datacolumn); result.add(data); } while (cursor.movetonext()); } cursor.close(); return result; } private void extractfiles() { // name of folder final string targetpath = directory_path; // toast showing name of directory images saved to. toast.maketext(getactivity().getapplicationcontext(), targetpath, toast.length_long).show(); file targetdirector = new file(targetpath); final file[] files = targetdirector.listfiles(); (file file : files) { imageadapter.add(file.getabsolutepath()); } } private void gridarchitecture(view rootview) { // sets tag gridview.settag(gridview_tag); /* * adapt image gridview format */ imageadapter = new imagegridviewadapter(getactivity() .getapplicationcontext()); gridview.setadapter(imageadapter); // set orientation landscape getactivity().setrequestedorientation( activityinfo.screen_orientation_landscape); } }
if of use here imagegridviewadapter.java
public class imagegridviewadapter extends baseadapter { private context context; arraylist<string> imagelist = new arraylist<string>(); public imagegridviewadapter(context c) { context = c; } void add(string path){ imagelist.add(path); } @override public int getcount() { return imagelist.size(); } @override public object getitem(int arg0) { // todo auto-generated method stub return null; } @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) { imageview = new imageview(context); imageview.setlayoutparams(new gridview.layoutparams(220, 220)); imageview.setscaletype(imageview.scaletype.center_crop); //8,8,8,8 imageview.setpadding(20, 20, 20, 20); } else { imageview = (imageview) convertview; } // 200, 200 bitmap bm = decodesampledbitmapfromuri(imagelist.get(position), 100, 100); imageview.setimagebitmap(bm); return imageview; } public bitmap decodesampledbitmapfromuri(string path, int reqwidth, int reqheight) { bitmap bm = null; final bitmapfactory.options options = new bitmapfactory.options(); options.injustdecodebounds = true; bitmapfactory.decodefile(path, options); options.insamplesize = calculateinsamplesize(options, reqwidth, reqheight); options.injustdecodebounds = false; bm = bitmapfactory.decodefile(path, options); return bm; } public int calculateinsamplesize( bitmapfactory.options options, int reqwidth, int reqheight) { 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; } }
here xml fragment_gallery.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/top" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/darkblue" android:orientation="horizontal" > <linearlayout android:id="@+id/center_point" android:layout_width="0dp" android:layout_height="0dp" android:layout_centerinparent="true" android:orientation="horizontal" /> <linearlayout android:id="@+id/right_linear" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_torightof="@+id/center_point" android:background="@drawable/normal_shape" > <imageview android:id="@+id/imageview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginbottom="320dp" android:contentdescription="@drawable/android_icon" android:src="@drawable/android_icon" /> </linearlayout> <linearlayout android:id="@+id/left_linear" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignparentleft="true" android:layout_alignright="@+id/center_point" > <gridview android:id="@+id/gridview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:columnwidth="50dp" android:gravity="center" android:horizontalspacing="10dip" android:numcolumns="2" android:stretchmode="columnwidth" android:verticalspacing="10dip" > </gridview> </linearlayout> </relativelayout>
assuming target imageview
declared as:
private imageview targetimageview;
all need register listener below gridview
:
gridview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // know convertview returned adapter's // getview method imageview can cast here accordingly. targetimageview.setimagedrawable(((imageview) view).getdrawable()); } });
Comments
Post a Comment