Shared Preference List View Not Working When App Closed
Im trying to learn how the listview works for an app im creating, I have got the app to show the listview with items, images and a checkbox. Im trying to achieve is: When they cli
Solution 1:
If u want to manage ur product using SharedPreference than u can do like this
MainActivity.java
publicclassMainActivityextendsAppCompatActivity {
ArrayList<Product> products = newArrayList<Product>();
ListAdapter boxAdapter;
private SharedPreferences mPrefs;
private String mData;
publicstaticfinalStringPREFS_NAME="Product_Prefrence";
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
products=getProductArray(this,"Products");
if(products==null)
{
fillData();
}
boxAdapter = newListAdapter(this, products);
ListViewlvMain= (ListView) findViewById(R.id.lvMain);
lvMain.setAdapter(boxAdapter);
}
voidfillData() {
products.add(newProduct("Al", "5230%", R.drawable.ic_launcher, false));
products.add(newProduct("Al", "5230%", R.drawable.ic_launcher, false));
products.add(newProduct("Alf", "5230%", R.drawable.ic_launcher, false));
products.add(newProduct("Alfa", "5230%", R.drawable.ic_launcher, false));
products.add(newProduct("Alfae", "5120%", R.drawable.ic_launcher, false));
products.add(newProduct("Alfsdfsdfakher", "50435%", R.drawable.ic_launcher, false));
products.add(newProduct("Alfasdfsdfkher", "5123120%", R.drawable.ic_launcher, false));
products.add(newProduct("Alfasdfsdfkher", "501231%", R.drawable.ic_launcher, false));
products.add(newProduct("Alfaksdfsdfher", "11250%", R.drawable.ic_launcher, false));
}
@OverridepublicvoidonBackPressed() {
saveProductArray(MainActivity.this,products,"Products");
finish();
}
publicvoidsaveProductArray(Context c, List<Product> arrayList, String key) {
SharedPreferencessettings= c.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editoreditor= settings.edit();
Gsongson=newGson();
Stringjson= gson.toJson(arrayList);
editor.putString(key, json).apply();
}
@NonNullpublic ArrayList<Product> getProductArray(Context c, String key) {
Gsongson=newGson();
SharedPreferencessettings= c.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
Stringjson= settings.getString(key, "");
Typetype=newTypeToken<ArrayList<Product>>() {
}.getType();
ArrayList<Product> arrayList = gson.fromJson(json, type);
return arrayList;
}
}
Adapter getView() Method:
@Overridepublic View getView(finalint position, View convertView, ViewGroup parent) {
Viewview= convertView;
if (view == null) {
view = lInflater.inflate(R.layout.item, parent, false);
}
finalProductp= getProduct(position);
((TextView) view.findViewById(R.id.tvDescr)).setText(p.name);
((TextView) view.findViewById(R.id.tvPrice)).setText(p.price + "");
((ImageView) view.findViewById(R.id.ivImage)).setImageResource(p.image);
finalCheckBoxcbBuy= (CheckBox) view.findViewById(R.id.cbBox);
//use click listener instead of checked change
cbBuy.setOnClickListener(newView.OnClickListener()
{
@OverridepublicvoidonClick(View v)
{
if(objects.get(position).isSelected)
{
cbBuy.setChecked(false);
objects.get(position).setSelected(false);
}
else
{
cbBuy.setChecked(true);
objects.get(position).setSelected(true);
}
}
});
//For scrolling issue use thisif(objects.get(position).isSelected)
{
cbBuy.setChecked(true);
}
else
{
cbBuy.setChecked(false);
}
return view;
}
}
No need to maintain sharepreference for single product value.
Hope this will help u..if u still face problem we will find other way
Post a Comment for "Shared Preference List View Not Working When App Closed"