java - Android - How to put a item click in ListView with Hashmap and List -
in app, use asynctask
named getimagetask
image url
int given user.
example : url = "http://example.com/image.php?index=" + int
i put image in imageview
, in "doinbackground"
, create 2 hashmap
add element in 2 list.
the first display list of index in listview
when click in button "onclickindex"
.
the second 1 save image index given because want element of listview
image when user click on (onclickindex
).
but i'm blocked on part because can't bitmap in second list index , don't know ?
mainactivity.java
public class mainactivity extends actionbaractivity { private edittext mynumber; private imageview myimage; private listview myindexlist; private arraylist<hashmap<string,string>> list = new arraylist<>(); private arraylist<hashmap<integer,bitmap>> list2 = new arraylist<>(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mynumber = (edittext)findviewbyid(r.id.choiceofindex); myimage = (imageview)findviewbyid(r.id.imagerecup); myindexlist = (listview)findviewbyid(r.id.listindex); } class getimagetask extends asynctask<integer,integer,bitmap> { protected bitmap doinbackground(integer... index) { hashmap<string,string> hashmap = new hashmap<>(); hashmap<integer,bitmap> hashmap2 = new hashmap<>(); integer choix = index[0]; string urldisplay = "http://example.com/image.php?index=" + choix; bitmap recupimage = null; try { inputstream in = new java.net.url(urldisplay).openstream(); recupimage = bitmapfactory.decodestream(in); } catch (exception e){ log.e("error", e.getmessage()); e.printstacktrace(); } hashmap.put("index","index " +choix); hashmap2.put(choix,recupimage); list.add(hashmap); list2.add(hashmap2); return recupimage; } protected void onprogressupdate(integer... progress) { context context = getapplicationcontext(); super.onprogressupdate(progress); toast.maketext(context, "wait...", toast.length_long).show(); } protected void onpostexecute(bitmap result) { context context = getapplicationcontext(); super.onpostexecute(result); myimage.setimagebitmap(result); toast.maketext(context, "image!!", toast.length_long).show(); } } public void onclickrecup(view v) { try{ int index= integer.parseint(mynumber.gettext().tostring()); getimagetask getimage = new getimagetask(); getimage.execute(index); } catch (numberformatexception nfe) { context context = getapplicationcontext(); toast.maketext(context, "no image!", toast.length_long).show(); } } public void onclickindex(view v) { try { simpleadapter adapter = new simpleadapter(this,list,r.layout.row, new string[]{"index"}, new int[] {r.id.index}); myindexlist.setadapter(adapter); myindexlist.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { bitmap imageback = list2.get(position).get(r.id.index); myimage.setimagebitmap(imageback); } }); } catch (exception e){ context context = getapplicationcontext(); toast.maketext(context, "problem on listview", toast.length_long).show(); } } }
sorry didn't sooner busy.
don't use hashmap store bitmaps. it's on complicating things.
arraylist<bitmap> list2 = new arraylist<>();
then change line..
bitmap imageback = list2.get(position).get(r.id.index);
to this
bitmap imageback = list2.get(position);
the reason why wrote didn't work don't store images on key: r.id.index
integer choix = index[0]; ... hashmap2.put(choix,recupimage);
r.id.index integer generated r before app run uniquely identifies label. why wouldn't accept before (as far can tell)
Comments
Post a Comment