Android : Custom ArrayAdapter not working to display text -


i working on android project, want create customarray adapter first display names of restaurants server, , when user clicks on 1 of object, it's id retrieved in backend(not displayed, nothing on ui). currently, having issues not working.

note please note, not android programmer, thankful if me out, please try understand, not familiar android concepts, may not understand trying , might have follow comments. if have time it, please post answer in simple manner code. sorry this, had similar problem , not experience. thanks. apologies if rude, felt important.

here layout code :

<?xml version="1.0" encoding="utf-8"?> <listview xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@android:id/list"     android:layout_width="fill_parent"     android:layout_height="fill_parent" > </listview> 

here restrestaurant class :

public class restrestaurant {      private int restaurantid;      private string restaurantname; //getters , setters ommitted; } 

and finally, here activity code :

public class restaurantlist extends listactivity {      string restaurantlist = "http://192.168.178.60:8080/restaurant/listing";      private responseentity<restrestaurant[]> responseentity;      private orderadapter m_adapter;      private arraylist<restrestaurant> m_orders = null;      @override     public void oncreate(bundle savedinstancestate) {          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();          m_orders = new arraylist<restrestaurant>();         this.m_adapter = new orderadapter(this, android.r.layout.simple_list_item_1, m_orders);          (restrestaurant restrestaurant1 : restrestaurantlist){             m_adapter.add(restrestaurant1);          }          setlistadapter(this.m_adapter);     }       private class orderadapter extends arrayadapter<restrestaurant> {          private arraylist<restrestaurant> items;          public orderadapter(context context, int textviewresourceid, arraylist<restrestaurant> items) {             super(context, textviewresourceid, items);             this.items = items;         }          @override         public view getview(int position, view convertview, viewgroup parent) {             view v = convertview;             restrestaurant o = items.get(position);             log.d("restaurant id ",string.valueof(o.getrestaurantid()));             return v;         }     }      @override     public void onbackpressed()     {         intent intent = new intent(restaurantlist.this, login.class);         staticresttemplate.jsessionid = null;         staticresttemplate.replystring = null;         startactivity(intent);         finish();     } } 

error log :

07-14 11:23:52.690    1534-1543/android.process.acore e/strictmode﹕ resource acquired @ attached stack trace never released. see java.io.closeable information on avoiding resource leaks.     java.lang.throwable: explicit termination method 'close' not called             @ dalvik.system.closeguard.open(closeguard.java:184) 

please understand, want display name of restaurant, , when clicked, id in activity class, deal rest of stuff own. lot.

update

row.xml :

<?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">  </linearlayout> 

error log :

07-15 10:25:13.951    2666-2666/com.example.testlunch e/androidruntime﹕ fatal exception: main     process: com.example.testlunch, pid: 2666     java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.textview.settext(java.lang.charsequence)' on null object reference             @ com.example.testlunch.activity.restaurantlist$orderadapter.getview(restaurantlist.java:125) 

happens here :

restrestaurant item = getitem(position);             if (item != null) { // on below line                 holder.tvrestourantname.settext(item.getrestaurantname());             } 

the error means have opened without closing it. interface closable provides method close() should call in order release resources no longer required.

you can take here , check if using class implements closable

the correct implementation of getview() is:

public view getview(int position, view convertview, viewgroup parent) {      view rowview = convertview;     viewholder holder;      if(rowview == null){         layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service);                    rowview = inflater.inflate(r.layout.custom_row_layout, parent, false);         holder = new viewholder();          holder.tvrestourantname= (textview) rowview.findviewbyid(r.id.restourant_name); // restourant_name id of view inside yout custom layout           rowview.settag(holder);      }else{         holder = (viewholder) rowview.gettag();      }      contact item = getitem(position);     if (item!= null) {         holder.tvrestourantname.settext(item.getrestourantname());     }      return rowview; }  static class viewholder {      textview tvrestourantname;  } 

you need define layout of row inside list, in separate xml file. named custom_row_layout.xml

then in list need set click listener:

getlistview().setonitemclicklistener(new onitemclicklistener() {             @override             public void onitemclick(adapterview<?> parent, view view, int position, long id) {                    restrestaurant item = (restrestaurant)getlistview().getadapter().getitem(position);                   int id = item.getrestaurantid(); // have id              }         }); 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -