java - What is the use of the init() method in this code? -


i'm trying learn how use json , i'm going on code tutorial , there init() method. found online init() used entry point of applets. if why init() in android apps code , not website code? can please explain reason? common when using json in android or uncommon?

public class mainactivity extends appcompatactivity {  private recyclerview mrestaurantrecyclerview; private restaurantadapter madapter; private arraylist<restaurant> mrestaurantcollection;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      init();     new fetchdatatask().execute(); }  private void init() {     mrestaurantrecyclerview = (recyclerview) findviewbyid(r.id.restaurant_recycler);     mrestaurantrecyclerview.setlayoutmanager(new linearlayoutmanager(this));     mrestaurantrecyclerview.sethasfixedsize(true);     mrestaurantcollection = new arraylist<>();     madapter = new restaurantadapter(mrestaurantcollection, this);     mrestaurantrecyclerview.setadapter(madapter); }  public class fetchdatatask extends asynctask<void, void, void> {     private string mzomatostring;      @override     protected void doinbackground(void... params) {         httpurlconnection urlconnection = null;         bufferedreader reader = null;         uri builturi = uri.parse(getstring(r.string.zomato_api));         url url;         try {             url = new url(builturi.tostring());             urlconnection = (httpurlconnection) url.openconnection();             urlconnection.setrequestmethod("get");             urlconnection.setrequestproperty("user-key", "acfd3e623c5f01289bd87aaaff1926c1");             urlconnection.connect();              inputstream inputstream = urlconnection.getinputstream();             stringbuffer buffer = new stringbuffer();             if (inputstream == null) {                 //nothing                 return null;             }              reader = new bufferedreader(new inputstreamreader(inputstream));              string line;             while ((line = reader.readline()) != null) {                 buffer.append(line + "\n");             }              if (buffer.length() == 0) {                 return null;             }              mzomatostring = buffer.tostring();             jsonobject jsonobject = new jsonobject(mzomatostring);              log.v("response", jsonobject.tostring());              jsonarray restaurantsarray = jsonobject.getjsonarray("restaurants");              //list = new arraylist<>();             (int = 0; < restaurantsarray.length(); i++) {                  log.v("brad_", + "");                 string name;                 string address;                 string currency;                 string imageurl;                 long lon;                 long lat;                 long cost;                 float rating;                   jsonobject jrestaurant = (jsonobject) restaurantsarray.get(i);                 jrestaurant = jrestaurant.getjsonobject("restaurant");                 jsonobject jlocattion = jrestaurant.getjsonobject("location");                 jsonobject jrating = jrestaurant.getjsonobject("user_rating");                   name = jrestaurant.getstring("name");                 address = jlocattion.getstring("address");                 lat = jlocattion.getlong("latitude");                 lon = jlocattion.getlong("longitude");                 currency = jrestaurant.getstring("currency");                 cost = jrestaurant.getint("average_cost_for_two");                 imageurl = jrestaurant.getstring("thumb");                 rating = (float) jrating.getdouble("aggregate_rating");                   restaurant restaurant = new restaurant();                 restaurant.setname(name);                 restaurant.setaddress(address);                 restaurant.setlatitiude(lat);                 restaurant.setlongitude(lon);                 restaurant.setcurrency(currency);                 restaurant.setcost(string.valueof(cost));                 restaurant.setimageurl(imageurl);                 restaurant.setrating(string.valueof(rating));                  mrestaurantcollection.add(restaurant);             }         } catch (malformedurlexception e) {             e.printstacktrace();         } catch (ioexception e) {             e.printstacktrace();         } catch (jsonexception e) {             e.printstacktrace();         } {             if (urlconnection != null) {                 urlconnection.disconnect();             }             if (reader != null) {                 try {                     reader.close();                 } catch (final ioexception e) {                     log.e("mainactivity", "error closing stream", e);                 }             }         }         return null;     }      @override     protected void onpostexecute(void avoid) {         madapter.notifydatasetchanged();     } } 

basically init method setting recyclerview configuration

mrestaurantrecyclerview = (recyclerview) findviewbyid(r.id.restaurant_recycler); 

defining recycler xml , put variable mrestaurantrecyclerview

mrestaurantrecyclerview.setlayoutmanager(new linearlayoutmanager(this)); 

setting how recyclerview going displayed, later should defined in project

mrestaurantrecyclerview.sethasfixedsize(true); 

it's function said, rv have fixed size, can refer link particular function understanding recyclerview sethasfixedsize

 mrestaurantcollection = new arraylist<>();  

this mrestaurantcollection going hold list of data going dispayed in recyclerview.

madapter = new restaurantadapter(mrestaurantcollection, this); mrestaurantrecyclerview.setadapter(madapter); 

last not list, these 2 lines of codes going hook data recyclerview via adapter. first line initiating adapter inserting 2 parameters data , context, second line telling recyclerview, "hey adapter, display this.".

ps : last not list meant joke. haha sry if not funny.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -