android - First time SharedPreferences use with GridView -


this apps screenshoot :

enter image description here

there 2 button in gridview : +/-.

so im gonna try when press "+" button or "-" button, quantity store in sharedpreferences.

but im confused this.

this code far :

package com.android.customer_blanjapasar.utility;      import android.content.context;     import android.content.sharedpreferences;     import android.preference.preferencemanager;     import android.text.editable;     import android.text.textwatcher;     import android.view.layoutinflater;     import android.view.view;     import android.view.viewgroup;     import android.widget.arrayadapter;     import android.widget.baseadapter;     import android.widget.button;     import android.widget.imageview;     import android.widget.textview;      import com.android.customer_blanjapasar.r;     import com.squareup.picasso.picasso;      import java.util.arraylist;     import java.util.list;      /**      * created leon on 5/3/2016.      */     public class customgridview2 extends baseadapter {         private arraylist<listitem> listdata;         private layoutinflater layoutinflater;         private context context;         private string[] imageurls;         private int count = 0;         int arraycount[];         sharedpreferences prefs ;         sharedpreference sharedpreference;         public customgridview2(context context, arraylist<listitem> listdata) {             this.listdata = listdata;             layoutinflater = layoutinflater.from(context);             this.context = context;             sharedpreference = new sharedpreference();         }          @override         public int getcount() {             return listdata.size();         }          @override         public object getitem(int position) {             return listdata.get(position);         }          @override         public long getitemid(int position) {             return position;         }          public view getview(final int position, view convertview, viewgroup parent) {             final viewholder holder;             if (convertview == null) {                 convertview = layoutinflater.inflate(r.layout.afterlogin_product_gridview, null);                 holder = new viewholder();                 holder.headlineview = (textview) convertview.findviewbyid(r.id.nama_produk);                 holder.teaserview = (textview) convertview.findviewbyid(r.id.harga);                 holder.imageview = (imageview) convertview.findviewbyid(r.id.img_produk);                 holder.cmdminus = (button) convertview.findviewbyid(r.id.btn_min);                 holder.cmdplus = (button) convertview.findviewbyid(r.id.btn_plus);                 holder.qty = (textview) convertview.findviewbyid(r.id.lbl_qty);                 convertview.settag(holder);              } else {                 holder = (viewholder) convertview.gettag();             }              listitem newsitem = listdata.get(position);             string satuan = newsitem.getsatuan().tostring();             string harga = newsitem.getreportername().tostring();             harga = "rp. " + harga + " / " + satuan;             holder.headlineview.settext(newsitem.getheadline().touppercase());             holder.teaserview.settext(harga);              string = newsitem.geturl();             holder.cmdplus.setonclicklistener(new view.onclicklistener() {                 @override                 public void onclick(view v) {                     count = integer.parseint( holder.qty.gettext().tostring());                     count++;                     holder.qty.settext(""+count);                 }             });              holder.cmdminus.setonclicklistener(new view.onclicklistener() {                 @override                 public void onclick(view v) {                     count = integer.parseint( holder.qty.gettext().tostring());                      if(count == 0) {                         holder.qty.settext("0");                     }                     else {                         count--;                         holder.qty.settext("" + count);                     }                 }             });              if (holder.imageview != null) {                 //new imagedownloadertask(holder.imageview).execute(newsitem.geturl());                 picasso                         .with(context)                         .load(a)                         .fit()                         .into(holder.imageview);             }                return convertview;         }          static class viewholder {             textview headlineview;             textview teaserview;             imageview imageview;             textview satuan,qty;             button cmdplus,cmdminus;         }      } 

i see tutorial. im still getting confused. please guide me step step.

edit

this listitem.class :

public class listitem {      private string headline;     private string reportername;     private string kode;     private string url;     private string satuan;      public string geturl() {         return url;     }      public void seturl(string url) {         this.url = url;     }      public string getheadline() {         return headline;     }      public void setheadline(string headline) {         this.headline = headline;     }      public string getreportername() {         return reportername;     }      public void setreportername(string reportername) {         this.reportername = reportername;     }      public string getkode() {         return kode;     }      public void setkode(string kode) {         this.kode = kode;     }      public string getsatuan() {         return satuan;     }      public void setsatuan(string satuan) {         this.satuan = satuan;     }      @override     public string tostring() {         return "[ headline=" + headline + ", reporter name=" + reportername + " , date=" + kode + "]";     } } 

and code inside mainactivity.class :

public class afterlogin_produk extends activity {     activity activity;     imagebutton btn_resep,btn_product;     static int jarray;     gridview product_gridview;     static string[] nama_prdct;     static string[] img_prdct;     static string[] harga_prdct;     static string[] satuan_prdct;     static string kode_ktgr;     @targetapi(build.version_codes.lollipop)     @override     protected void oncreate(bundle savedinstancestate) {         this.requestwindowfeature(window.feature_no_title);         super.oncreate(savedinstancestate);         setcontentview(r.layout.after_login_produk_main);         product_gridview = (gridview) findviewbyid(r.id.product_gridview);         new getlength().execute();     }  public arraylist<listitem> getlistdata() {         arraylist<listitem> listmockdata = new arraylist<listitem>();          (int = 0; < jarray; i++) {             listitem newsdata = new listitem();             newsdata.seturl(img_prdct[i]);             newsdata.setheadline(nama_prdct[i]);             newsdata.setreportername(harga_prdct[i]);             newsdata.setsatuan(satuan_prdct[i]);             listmockdata.add(newsdata);         }         return listmockdata;     }   class getlength extends asynctask<string, string, string> {         string nama_product,img_product,harga_product,satuan_product;         jsonparser2 jparser = new jsonparser2();         arraylist<string> list_nama_produk = new arraylist<string>();         arraylist<string> list_img_produk = new arraylist<string>();         arraylist<string> list_harga_produk = new arraylist<string>();         arraylist<string> list_satuan_produk = new arraylist<string>();         protected string doinbackground(string... params) {             try {                 list<namevaluepair> param = new arraylist<namevaluepair>();                 param.add(new basicnamevaluepair("kode_kategori", kode_ktgr));                 jsonobject json = jparser.makehttprequest("http:xxx.php", "post", param);                 jsonarray array = json.getjsonarray("categories");                 jarray = array.length();                 (int = 0; < array.length(); i++) {                     jsonobject row = array.getjsonobject(i);                     nama_product = row.getstring("nama_produk");                     img_product = row.getstring("img_produk");                     harga_product = row.getstring("harga_satuan");                     satuan_product = row.getstring("nama_satuan");                     list_nama_produk.add(nama_product);                     list_img_produk.add(img_product);                     list_harga_produk.add(harga_product);                     list_satuan_produk.add(satuan_product);                 }             } catch (exception e) {                 system.out.println("exception : " + e.getmessage());             }             return null;         }          @override         protected void onpostexecute(string s) {             super.onpostexecute(s);             //toast.maketext(getbasecontext(),"value : " + list_nama_kategori,toast.length_short).show();             nama_prdct = new string[list_nama_produk.size()];             img_prdct = new string[list_img_produk.size()];             harga_prdct = new string[list_harga_produk.size()];             satuan_prdct = new string[list_satuan_produk.size()];             nama_prdct = list_nama_produk.toarray(nama_prdct);             img_prdct = list_img_produk.toarray(img_prdct);             harga_prdct = list_harga_produk.toarray(harga_prdct);             satuan_prdct = list_satuan_produk.toarray(satuan_prdct);             arraylist<listitem> listdata = getlistdata();             product_gridview.setadapter(new customgridview2(afterlogin_produk.this, listdata));          }     } 

you using custom array list of object set propertie , on press of application can jsonify object string , store in shared preference , @ activity on create can regenerate object getting shared preference. in activity oncreate()

 sharedpreferences msettings = preferencemanager.getdefaultsharedpreferences                 (dashboard.this);         string data = msettings.getstring("data", "");         /* should activity check updates now? */         if ((data.equals(""))) { //do nothing data not in shared preference            }         else {             //data there convert object             data=msettings.getstring("data", "");             type listtype = new typetoken<arraylist<listrowitem>>() {             }.gettype();             arraylist<listrowitem> listrowitems = new gson().fromjson(data, listtype); //setadapter coming arraylist usual.         } 

and in backpressed can read data adapter , jsonify using json , put in sharedpreference hope understand login . clear preference after data set.


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -