object - Android : Display details of Item in list view when clicked -
i working on android project, displaying list of items user. now, trying when user clicks on item, want id of item, can call upon object details via network , display them in activity. problem is, object displayed contain names. cannot find way, set id of item, hidden, , can retrieve when user clicks , subsequent task. there way set id somewhere cannot seen user , can used.
here code :
public class restaurantlist extends activity { string restaurantlist = "http://192.168.178.60:8080/restaurant/listing"; private responseentity<restrestaurant[]> responseentity; @override public void oncreate(bundle savedinstancestate) { listview listview = (listview) findviewbyid(r.id.restrestaurantlist); super.oncreate(savedinstancestate); setcontentview(r.layout.restos); resttemplate resttemplate = staticresttemplate.getrest(); thread thread = new thread(new runnable() { @override public void run() { httpheaders requestheaders = new httpheaders(); requestheaders.add("cookie", "jsessionid=" + staticresttemplate.jsessionid); requestheaders.setaccept(collections.singletonlist(new mediatype("application", "json"))); httpentity<?> requestentity = new httpentity<object>(requestheaders); resttemplate.getmessageconverters().add(new mappingjackson2httpmessageconverter()); responseentity= resttemplate.exchange(restaurantlist, httpmethod.get,requestentity,restrestaurant[].class); } }); thread.start(); progressdialog progress = new progressdialog(this); progress.settitle("loading"); progress.setmessage("wait while loading..."); while (thread.getstate()!=thread.state.terminated){ progress.show(); } progress.dismiss(); restrestaurant[] restrestaurantlist = responseentity.getbody(); arraylist<string> list = new arraylist<>(); (restrestaurant restrestaurant1 : restrestaurantlist){ log.d("restrestaurant name", restrestaurant1.getrestaurantname()); list.add(restrestaurant1.getrestaurantname()); } // getting error @ below line, saying stablearrayadapter cannot applied restrestaurant. final stablearrayadapter adapter = new stablearrayadapter(this, android.r.layout.simple_list_item_1, list); listview.setadapter(adapter); listview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, final view view, int position, long id) { final string item = list.get(position); view.animate().setduration(2000).alpha(0) .withendaction(new runnable() { @override public void run() { } }); } }); } private class stablearrayadapter extends arrayadapter<restrestaurant> { hashmap<integer, integer> midmap = new hashmap<integer, integer>(); public stablearrayadapter(context context, int textviewresourceid, list<restrestaurant> objects) { super(context, textviewresourceid, objects); (int = 0; < objects.size(); ++i) { midmap.put(objects.get(0).getrestaurantid(), i); } } @override public long getitemid(int position) { restrestaurant item = getitem(position); return midmap.get(item); } @override public boolean hasstableids() { return true; } } @override public void onbackpressed() { intent intent = new intent(restaurantlist.this, login.class); staticresttemplate.jsessionid = null; staticresttemplate.replystring = null; startactivity(intent); finish(); } }
xml file :
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <listview android:layout_width="match_parent" android:layout_height="457dp" android:id="@+id/restrestaurantlist" android:layout_gravity="center_horizontal" android:layout_margintop="20dp"/> </linearlayout>
kindly let me know doing wrong.
you can set entire object in view , object (in case : restrestaurant)
1. in activity, implement list - onclick listener: listview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { } });
in adapter, add tag each item inside getview():
view.settag(object of restrestaurant);
inside activity, in onclick, when view: need call:
restrestaurant rest = ((restrestaurant)v.gettag());
so, here have selected item.
Comments
Post a Comment