Implement SharedPreference in getview












-1















How can I implement SharedPreference in my Adapter class?
I want to use it for persisting CheckBox state. Now the CheckBox value unchecks itself when I change Activity or add a new item in the ListView.



public class listItems extends ArrayAdapter<Items> {

Activity context;
List<Items> listitems;

public listItems(Activity context, List<Items> listitems) {
super(context, R.layout.list_items, listitems);
this.context = context;
this.listitems = listitems;
}

@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();


View listViewItem = inflater.inflate(R.layout.list_items, null, true);


final Items items = listitems.get(position);

final TextView tvitemname = (TextView) listViewItem.findViewById(R.id.tvitemname);

final CheckBox simpleCheckedTextview = (CheckBox) listViewItem.findViewById(R.id.cb);
tvitemname.setText(items.getItemname());
simpleCheckedTextview.isChecked();




return listViewItem;
}


I've added my Items class. Is this looking good? I have a TextView that gets data from FireBase.



Class:



public class Items {

String itemId;
String itemname;
private boolean checked;

public Items(){

}


public Items(String itemId, String itemname, boolean checked) {
this.itemId = itemId;
this.itemname = itemname;
this.checked = checked;
}




public String getItemId() {
return itemId;
}

public String getItemname() {
return itemname;

}

public void setChecked(boolean checked) {
this.checked = checked;
}

public boolean isChecked() {
return checked;
}









share|improve this question




















  • 1





    You can pass the SharedPreference object to the adapter constructor but I recommend you to read this value outside the adapter and pass a Map<ItemId, Boolean> where the Boolean value is selected or unselected.

    – haroldolivieri
    Nov 28 '18 at 20:42











  • Ok, is it possible for you to show me in my code?

    – Alex
    Nov 28 '18 at 20:52











  • Just add an additional boolean property to your Items class--something like isItemChecked then when your list is populated set the value of simpleCheckedTextview accordingly and change the value of isItemChecked when the checkbox changes.

    – Barns
    Nov 28 '18 at 21:02













  • Yes, if this Item is just a model used for view creation there is no problem to add this property directly to it.

    – haroldolivieri
    Nov 28 '18 at 21:10











  • But I need to use sharedpreference to save the values?

    – Alex
    Nov 28 '18 at 21:16
















-1















How can I implement SharedPreference in my Adapter class?
I want to use it for persisting CheckBox state. Now the CheckBox value unchecks itself when I change Activity or add a new item in the ListView.



public class listItems extends ArrayAdapter<Items> {

Activity context;
List<Items> listitems;

public listItems(Activity context, List<Items> listitems) {
super(context, R.layout.list_items, listitems);
this.context = context;
this.listitems = listitems;
}

@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();


View listViewItem = inflater.inflate(R.layout.list_items, null, true);


final Items items = listitems.get(position);

final TextView tvitemname = (TextView) listViewItem.findViewById(R.id.tvitemname);

final CheckBox simpleCheckedTextview = (CheckBox) listViewItem.findViewById(R.id.cb);
tvitemname.setText(items.getItemname());
simpleCheckedTextview.isChecked();




return listViewItem;
}


I've added my Items class. Is this looking good? I have a TextView that gets data from FireBase.



Class:



public class Items {

String itemId;
String itemname;
private boolean checked;

public Items(){

}


public Items(String itemId, String itemname, boolean checked) {
this.itemId = itemId;
this.itemname = itemname;
this.checked = checked;
}




public String getItemId() {
return itemId;
}

public String getItemname() {
return itemname;

}

public void setChecked(boolean checked) {
this.checked = checked;
}

public boolean isChecked() {
return checked;
}









share|improve this question




















  • 1





    You can pass the SharedPreference object to the adapter constructor but I recommend you to read this value outside the adapter and pass a Map<ItemId, Boolean> where the Boolean value is selected or unselected.

    – haroldolivieri
    Nov 28 '18 at 20:42











  • Ok, is it possible for you to show me in my code?

    – Alex
    Nov 28 '18 at 20:52











  • Just add an additional boolean property to your Items class--something like isItemChecked then when your list is populated set the value of simpleCheckedTextview accordingly and change the value of isItemChecked when the checkbox changes.

    – Barns
    Nov 28 '18 at 21:02













  • Yes, if this Item is just a model used for view creation there is no problem to add this property directly to it.

    – haroldolivieri
    Nov 28 '18 at 21:10











  • But I need to use sharedpreference to save the values?

    – Alex
    Nov 28 '18 at 21:16














-1












-1








-1








How can I implement SharedPreference in my Adapter class?
I want to use it for persisting CheckBox state. Now the CheckBox value unchecks itself when I change Activity or add a new item in the ListView.



public class listItems extends ArrayAdapter<Items> {

Activity context;
List<Items> listitems;

public listItems(Activity context, List<Items> listitems) {
super(context, R.layout.list_items, listitems);
this.context = context;
this.listitems = listitems;
}

@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();


View listViewItem = inflater.inflate(R.layout.list_items, null, true);


final Items items = listitems.get(position);

final TextView tvitemname = (TextView) listViewItem.findViewById(R.id.tvitemname);

final CheckBox simpleCheckedTextview = (CheckBox) listViewItem.findViewById(R.id.cb);
tvitemname.setText(items.getItemname());
simpleCheckedTextview.isChecked();




return listViewItem;
}


I've added my Items class. Is this looking good? I have a TextView that gets data from FireBase.



Class:



public class Items {

String itemId;
String itemname;
private boolean checked;

public Items(){

}


public Items(String itemId, String itemname, boolean checked) {
this.itemId = itemId;
this.itemname = itemname;
this.checked = checked;
}




public String getItemId() {
return itemId;
}

public String getItemname() {
return itemname;

}

public void setChecked(boolean checked) {
this.checked = checked;
}

public boolean isChecked() {
return checked;
}









share|improve this question
















How can I implement SharedPreference in my Adapter class?
I want to use it for persisting CheckBox state. Now the CheckBox value unchecks itself when I change Activity or add a new item in the ListView.



public class listItems extends ArrayAdapter<Items> {

Activity context;
List<Items> listitems;

public listItems(Activity context, List<Items> listitems) {
super(context, R.layout.list_items, listitems);
this.context = context;
this.listitems = listitems;
}

@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();


View listViewItem = inflater.inflate(R.layout.list_items, null, true);


final Items items = listitems.get(position);

final TextView tvitemname = (TextView) listViewItem.findViewById(R.id.tvitemname);

final CheckBox simpleCheckedTextview = (CheckBox) listViewItem.findViewById(R.id.cb);
tvitemname.setText(items.getItemname());
simpleCheckedTextview.isChecked();




return listViewItem;
}


I've added my Items class. Is this looking good? I have a TextView that gets data from FireBase.



Class:



public class Items {

String itemId;
String itemname;
private boolean checked;

public Items(){

}


public Items(String itemId, String itemname, boolean checked) {
this.itemId = itemId;
this.itemname = itemname;
this.checked = checked;
}




public String getItemId() {
return itemId;
}

public String getItemname() {
return itemname;

}

public void setChecked(boolean checked) {
this.checked = checked;
}

public boolean isChecked() {
return checked;
}






android sharedpreferences android-adapter android-checkbox






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 29 '18 at 17:29









haroldolivieri

349315




349315










asked Nov 28 '18 at 20:21









AlexAlex

177




177








  • 1





    You can pass the SharedPreference object to the adapter constructor but I recommend you to read this value outside the adapter and pass a Map<ItemId, Boolean> where the Boolean value is selected or unselected.

    – haroldolivieri
    Nov 28 '18 at 20:42











  • Ok, is it possible for you to show me in my code?

    – Alex
    Nov 28 '18 at 20:52











  • Just add an additional boolean property to your Items class--something like isItemChecked then when your list is populated set the value of simpleCheckedTextview accordingly and change the value of isItemChecked when the checkbox changes.

    – Barns
    Nov 28 '18 at 21:02













  • Yes, if this Item is just a model used for view creation there is no problem to add this property directly to it.

    – haroldolivieri
    Nov 28 '18 at 21:10











  • But I need to use sharedpreference to save the values?

    – Alex
    Nov 28 '18 at 21:16














  • 1





    You can pass the SharedPreference object to the adapter constructor but I recommend you to read this value outside the adapter and pass a Map<ItemId, Boolean> where the Boolean value is selected or unselected.

    – haroldolivieri
    Nov 28 '18 at 20:42











  • Ok, is it possible for you to show me in my code?

    – Alex
    Nov 28 '18 at 20:52











  • Just add an additional boolean property to your Items class--something like isItemChecked then when your list is populated set the value of simpleCheckedTextview accordingly and change the value of isItemChecked when the checkbox changes.

    – Barns
    Nov 28 '18 at 21:02













  • Yes, if this Item is just a model used for view creation there is no problem to add this property directly to it.

    – haroldolivieri
    Nov 28 '18 at 21:10











  • But I need to use sharedpreference to save the values?

    – Alex
    Nov 28 '18 at 21:16








1




1





You can pass the SharedPreference object to the adapter constructor but I recommend you to read this value outside the adapter and pass a Map<ItemId, Boolean> where the Boolean value is selected or unselected.

– haroldolivieri
Nov 28 '18 at 20:42





You can pass the SharedPreference object to the adapter constructor but I recommend you to read this value outside the adapter and pass a Map<ItemId, Boolean> where the Boolean value is selected or unselected.

– haroldolivieri
Nov 28 '18 at 20:42













Ok, is it possible for you to show me in my code?

– Alex
Nov 28 '18 at 20:52





Ok, is it possible for you to show me in my code?

– Alex
Nov 28 '18 at 20:52













Just add an additional boolean property to your Items class--something like isItemChecked then when your list is populated set the value of simpleCheckedTextview accordingly and change the value of isItemChecked when the checkbox changes.

– Barns
Nov 28 '18 at 21:02







Just add an additional boolean property to your Items class--something like isItemChecked then when your list is populated set the value of simpleCheckedTextview accordingly and change the value of isItemChecked when the checkbox changes.

– Barns
Nov 28 '18 at 21:02















Yes, if this Item is just a model used for view creation there is no problem to add this property directly to it.

– haroldolivieri
Nov 28 '18 at 21:10





Yes, if this Item is just a model used for view creation there is no problem to add this property directly to it.

– haroldolivieri
Nov 28 '18 at 21:10













But I need to use sharedpreference to save the values?

– Alex
Nov 28 '18 at 21:16





But I need to use sharedpreference to save the values?

– Alex
Nov 28 '18 at 21:16












2 Answers
2






active

oldest

votes


















0














How can I implement SharedPreference in my adapter class:



Save:



SharedPreferences sharedPreferences =  context.getSharedPreferences("checkboxpref", 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("checkbox_value", true);
editor.apply();


Get:



SharedPreferences sharedPreferences =  context.getSharedPreferences("checkboxpref", 0);
final boolean getShared = sharedPreferences.getBoolean("checkbox_value", false);





share|improve this answer
























  • So where in my code should I put this?

    – Alex
    Nov 28 '18 at 21:18











  • Ok, I have a listview where I get data (Item) from firebase. I added a checkbox besides my textview (tvitemname). So I want this checkbox to save/hold its value till I check it again. Even if I close my app I want it to save the last value. But I don't need the checkbox value in firebase, but maybe I have to, since I use firebase for the textview item?

    – Alex
    Nov 28 '18 at 21:29











  • It depends. Do you want to persist this data related to a specific user even in different devices? If yes you should send it to firebase, otherwise just save it locally. You can use SharedPreferences, Room, Realm or any other available local storage.

    – haroldolivieri
    Nov 29 '18 at 9:49



















0














I suppose you are new in Android development, so I advise you to start working with RecyclerView instead ListView. This is a good article to help you handle checkBox states on RecyclerViewAdapters.



The way adapters handles states for each item is totally different from how the Activity does. Once you scroll and the items are not visible they are destroyed and recreated again only when visible. That’s why you need to store the state of all values at least in memory with a Map or a SparseArray for example. Just follow step by step the link and it will work.



Do you want to persist this data related to a specific user even in different devices? If yes you should send it to your Firebase, otherwise just save it locally. You can use SharedPreferences, Room, Realm or any other local storage.






share|improve this answer


























  • This is just for one device, so I don't need it to be stored in Firebase. I just thought it would be easier. I just having difficulties to implement sharedpreference in my adapter class, under getview. So that the values get saved. I manage to save the values if I do it in my MainActivity (I tested with a checkbox in my MainActivity, and that worked). But to do this in a listview that are getting data from firebase was not that easy.

    – Alex
    Nov 29 '18 at 17:28













  • Have you tried this link I sent? The way lists handle states for each item is totally different from Activity. Once you scroll and the items are not visible they are destroyed and recreated again when only when visible. That’s why you need another source with the state of all values, as a map or a sparsearray. Just follow step by step the link and it will work.

    – haroldolivieri
    Nov 29 '18 at 22:15






  • 1





    Ok, I'm going to check it out. Thank you for your help:)

    – Alex
    Nov 30 '18 at 7:13











  • If it works remember to vote for the answer :)

    – haroldolivieri
    Nov 30 '18 at 8:45











  • Offcourse I will :) gonna try it in a few days

    – Alex
    Nov 30 '18 at 15:00












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53527485%2fimplement-sharedpreference-in-getview%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














How can I implement SharedPreference in my adapter class:



Save:



SharedPreferences sharedPreferences =  context.getSharedPreferences("checkboxpref", 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("checkbox_value", true);
editor.apply();


Get:



SharedPreferences sharedPreferences =  context.getSharedPreferences("checkboxpref", 0);
final boolean getShared = sharedPreferences.getBoolean("checkbox_value", false);





share|improve this answer
























  • So where in my code should I put this?

    – Alex
    Nov 28 '18 at 21:18











  • Ok, I have a listview where I get data (Item) from firebase. I added a checkbox besides my textview (tvitemname). So I want this checkbox to save/hold its value till I check it again. Even if I close my app I want it to save the last value. But I don't need the checkbox value in firebase, but maybe I have to, since I use firebase for the textview item?

    – Alex
    Nov 28 '18 at 21:29











  • It depends. Do you want to persist this data related to a specific user even in different devices? If yes you should send it to firebase, otherwise just save it locally. You can use SharedPreferences, Room, Realm or any other available local storage.

    – haroldolivieri
    Nov 29 '18 at 9:49
















0














How can I implement SharedPreference in my adapter class:



Save:



SharedPreferences sharedPreferences =  context.getSharedPreferences("checkboxpref", 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("checkbox_value", true);
editor.apply();


Get:



SharedPreferences sharedPreferences =  context.getSharedPreferences("checkboxpref", 0);
final boolean getShared = sharedPreferences.getBoolean("checkbox_value", false);





share|improve this answer
























  • So where in my code should I put this?

    – Alex
    Nov 28 '18 at 21:18











  • Ok, I have a listview where I get data (Item) from firebase. I added a checkbox besides my textview (tvitemname). So I want this checkbox to save/hold its value till I check it again. Even if I close my app I want it to save the last value. But I don't need the checkbox value in firebase, but maybe I have to, since I use firebase for the textview item?

    – Alex
    Nov 28 '18 at 21:29











  • It depends. Do you want to persist this data related to a specific user even in different devices? If yes you should send it to firebase, otherwise just save it locally. You can use SharedPreferences, Room, Realm or any other available local storage.

    – haroldolivieri
    Nov 29 '18 at 9:49














0












0








0







How can I implement SharedPreference in my adapter class:



Save:



SharedPreferences sharedPreferences =  context.getSharedPreferences("checkboxpref", 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("checkbox_value", true);
editor.apply();


Get:



SharedPreferences sharedPreferences =  context.getSharedPreferences("checkboxpref", 0);
final boolean getShared = sharedPreferences.getBoolean("checkbox_value", false);





share|improve this answer













How can I implement SharedPreference in my adapter class:



Save:



SharedPreferences sharedPreferences =  context.getSharedPreferences("checkboxpref", 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("checkbox_value", true);
editor.apply();


Get:



SharedPreferences sharedPreferences =  context.getSharedPreferences("checkboxpref", 0);
final boolean getShared = sharedPreferences.getBoolean("checkbox_value", false);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 28 '18 at 21:14









AbdomnsAbdomns

39018




39018













  • So where in my code should I put this?

    – Alex
    Nov 28 '18 at 21:18











  • Ok, I have a listview where I get data (Item) from firebase. I added a checkbox besides my textview (tvitemname). So I want this checkbox to save/hold its value till I check it again. Even if I close my app I want it to save the last value. But I don't need the checkbox value in firebase, but maybe I have to, since I use firebase for the textview item?

    – Alex
    Nov 28 '18 at 21:29











  • It depends. Do you want to persist this data related to a specific user even in different devices? If yes you should send it to firebase, otherwise just save it locally. You can use SharedPreferences, Room, Realm or any other available local storage.

    – haroldolivieri
    Nov 29 '18 at 9:49



















  • So where in my code should I put this?

    – Alex
    Nov 28 '18 at 21:18











  • Ok, I have a listview where I get data (Item) from firebase. I added a checkbox besides my textview (tvitemname). So I want this checkbox to save/hold its value till I check it again. Even if I close my app I want it to save the last value. But I don't need the checkbox value in firebase, but maybe I have to, since I use firebase for the textview item?

    – Alex
    Nov 28 '18 at 21:29











  • It depends. Do you want to persist this data related to a specific user even in different devices? If yes you should send it to firebase, otherwise just save it locally. You can use SharedPreferences, Room, Realm or any other available local storage.

    – haroldolivieri
    Nov 29 '18 at 9:49

















So where in my code should I put this?

– Alex
Nov 28 '18 at 21:18





So where in my code should I put this?

– Alex
Nov 28 '18 at 21:18













Ok, I have a listview where I get data (Item) from firebase. I added a checkbox besides my textview (tvitemname). So I want this checkbox to save/hold its value till I check it again. Even if I close my app I want it to save the last value. But I don't need the checkbox value in firebase, but maybe I have to, since I use firebase for the textview item?

– Alex
Nov 28 '18 at 21:29





Ok, I have a listview where I get data (Item) from firebase. I added a checkbox besides my textview (tvitemname). So I want this checkbox to save/hold its value till I check it again. Even if I close my app I want it to save the last value. But I don't need the checkbox value in firebase, but maybe I have to, since I use firebase for the textview item?

– Alex
Nov 28 '18 at 21:29













It depends. Do you want to persist this data related to a specific user even in different devices? If yes you should send it to firebase, otherwise just save it locally. You can use SharedPreferences, Room, Realm or any other available local storage.

– haroldolivieri
Nov 29 '18 at 9:49





It depends. Do you want to persist this data related to a specific user even in different devices? If yes you should send it to firebase, otherwise just save it locally. You can use SharedPreferences, Room, Realm or any other available local storage.

– haroldolivieri
Nov 29 '18 at 9:49













0














I suppose you are new in Android development, so I advise you to start working with RecyclerView instead ListView. This is a good article to help you handle checkBox states on RecyclerViewAdapters.



The way adapters handles states for each item is totally different from how the Activity does. Once you scroll and the items are not visible they are destroyed and recreated again only when visible. That’s why you need to store the state of all values at least in memory with a Map or a SparseArray for example. Just follow step by step the link and it will work.



Do you want to persist this data related to a specific user even in different devices? If yes you should send it to your Firebase, otherwise just save it locally. You can use SharedPreferences, Room, Realm or any other local storage.






share|improve this answer


























  • This is just for one device, so I don't need it to be stored in Firebase. I just thought it would be easier. I just having difficulties to implement sharedpreference in my adapter class, under getview. So that the values get saved. I manage to save the values if I do it in my MainActivity (I tested with a checkbox in my MainActivity, and that worked). But to do this in a listview that are getting data from firebase was not that easy.

    – Alex
    Nov 29 '18 at 17:28













  • Have you tried this link I sent? The way lists handle states for each item is totally different from Activity. Once you scroll and the items are not visible they are destroyed and recreated again when only when visible. That’s why you need another source with the state of all values, as a map or a sparsearray. Just follow step by step the link and it will work.

    – haroldolivieri
    Nov 29 '18 at 22:15






  • 1





    Ok, I'm going to check it out. Thank you for your help:)

    – Alex
    Nov 30 '18 at 7:13











  • If it works remember to vote for the answer :)

    – haroldolivieri
    Nov 30 '18 at 8:45











  • Offcourse I will :) gonna try it in a few days

    – Alex
    Nov 30 '18 at 15:00
















0














I suppose you are new in Android development, so I advise you to start working with RecyclerView instead ListView. This is a good article to help you handle checkBox states on RecyclerViewAdapters.



The way adapters handles states for each item is totally different from how the Activity does. Once you scroll and the items are not visible they are destroyed and recreated again only when visible. That’s why you need to store the state of all values at least in memory with a Map or a SparseArray for example. Just follow step by step the link and it will work.



Do you want to persist this data related to a specific user even in different devices? If yes you should send it to your Firebase, otherwise just save it locally. You can use SharedPreferences, Room, Realm or any other local storage.






share|improve this answer


























  • This is just for one device, so I don't need it to be stored in Firebase. I just thought it would be easier. I just having difficulties to implement sharedpreference in my adapter class, under getview. So that the values get saved. I manage to save the values if I do it in my MainActivity (I tested with a checkbox in my MainActivity, and that worked). But to do this in a listview that are getting data from firebase was not that easy.

    – Alex
    Nov 29 '18 at 17:28













  • Have you tried this link I sent? The way lists handle states for each item is totally different from Activity. Once you scroll and the items are not visible they are destroyed and recreated again when only when visible. That’s why you need another source with the state of all values, as a map or a sparsearray. Just follow step by step the link and it will work.

    – haroldolivieri
    Nov 29 '18 at 22:15






  • 1





    Ok, I'm going to check it out. Thank you for your help:)

    – Alex
    Nov 30 '18 at 7:13











  • If it works remember to vote for the answer :)

    – haroldolivieri
    Nov 30 '18 at 8:45











  • Offcourse I will :) gonna try it in a few days

    – Alex
    Nov 30 '18 at 15:00














0












0








0







I suppose you are new in Android development, so I advise you to start working with RecyclerView instead ListView. This is a good article to help you handle checkBox states on RecyclerViewAdapters.



The way adapters handles states for each item is totally different from how the Activity does. Once you scroll and the items are not visible they are destroyed and recreated again only when visible. That’s why you need to store the state of all values at least in memory with a Map or a SparseArray for example. Just follow step by step the link and it will work.



Do you want to persist this data related to a specific user even in different devices? If yes you should send it to your Firebase, otherwise just save it locally. You can use SharedPreferences, Room, Realm or any other local storage.






share|improve this answer















I suppose you are new in Android development, so I advise you to start working with RecyclerView instead ListView. This is a good article to help you handle checkBox states on RecyclerViewAdapters.



The way adapters handles states for each item is totally different from how the Activity does. Once you scroll and the items are not visible they are destroyed and recreated again only when visible. That’s why you need to store the state of all values at least in memory with a Map or a SparseArray for example. Just follow step by step the link and it will work.



Do you want to persist this data related to a specific user even in different devices? If yes you should send it to your Firebase, otherwise just save it locally. You can use SharedPreferences, Room, Realm or any other local storage.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 30 '18 at 8:46

























answered Nov 29 '18 at 10:07









haroldolivieriharoldolivieri

349315




349315













  • This is just for one device, so I don't need it to be stored in Firebase. I just thought it would be easier. I just having difficulties to implement sharedpreference in my adapter class, under getview. So that the values get saved. I manage to save the values if I do it in my MainActivity (I tested with a checkbox in my MainActivity, and that worked). But to do this in a listview that are getting data from firebase was not that easy.

    – Alex
    Nov 29 '18 at 17:28













  • Have you tried this link I sent? The way lists handle states for each item is totally different from Activity. Once you scroll and the items are not visible they are destroyed and recreated again when only when visible. That’s why you need another source with the state of all values, as a map or a sparsearray. Just follow step by step the link and it will work.

    – haroldolivieri
    Nov 29 '18 at 22:15






  • 1





    Ok, I'm going to check it out. Thank you for your help:)

    – Alex
    Nov 30 '18 at 7:13











  • If it works remember to vote for the answer :)

    – haroldolivieri
    Nov 30 '18 at 8:45











  • Offcourse I will :) gonna try it in a few days

    – Alex
    Nov 30 '18 at 15:00



















  • This is just for one device, so I don't need it to be stored in Firebase. I just thought it would be easier. I just having difficulties to implement sharedpreference in my adapter class, under getview. So that the values get saved. I manage to save the values if I do it in my MainActivity (I tested with a checkbox in my MainActivity, and that worked). But to do this in a listview that are getting data from firebase was not that easy.

    – Alex
    Nov 29 '18 at 17:28













  • Have you tried this link I sent? The way lists handle states for each item is totally different from Activity. Once you scroll and the items are not visible they are destroyed and recreated again when only when visible. That’s why you need another source with the state of all values, as a map or a sparsearray. Just follow step by step the link and it will work.

    – haroldolivieri
    Nov 29 '18 at 22:15






  • 1





    Ok, I'm going to check it out. Thank you for your help:)

    – Alex
    Nov 30 '18 at 7:13











  • If it works remember to vote for the answer :)

    – haroldolivieri
    Nov 30 '18 at 8:45











  • Offcourse I will :) gonna try it in a few days

    – Alex
    Nov 30 '18 at 15:00

















This is just for one device, so I don't need it to be stored in Firebase. I just thought it would be easier. I just having difficulties to implement sharedpreference in my adapter class, under getview. So that the values get saved. I manage to save the values if I do it in my MainActivity (I tested with a checkbox in my MainActivity, and that worked). But to do this in a listview that are getting data from firebase was not that easy.

– Alex
Nov 29 '18 at 17:28







This is just for one device, so I don't need it to be stored in Firebase. I just thought it would be easier. I just having difficulties to implement sharedpreference in my adapter class, under getview. So that the values get saved. I manage to save the values if I do it in my MainActivity (I tested with a checkbox in my MainActivity, and that worked). But to do this in a listview that are getting data from firebase was not that easy.

– Alex
Nov 29 '18 at 17:28















Have you tried this link I sent? The way lists handle states for each item is totally different from Activity. Once you scroll and the items are not visible they are destroyed and recreated again when only when visible. That’s why you need another source with the state of all values, as a map or a sparsearray. Just follow step by step the link and it will work.

– haroldolivieri
Nov 29 '18 at 22:15





Have you tried this link I sent? The way lists handle states for each item is totally different from Activity. Once you scroll and the items are not visible they are destroyed and recreated again when only when visible. That’s why you need another source with the state of all values, as a map or a sparsearray. Just follow step by step the link and it will work.

– haroldolivieri
Nov 29 '18 at 22:15




1




1





Ok, I'm going to check it out. Thank you for your help:)

– Alex
Nov 30 '18 at 7:13





Ok, I'm going to check it out. Thank you for your help:)

– Alex
Nov 30 '18 at 7:13













If it works remember to vote for the answer :)

– haroldolivieri
Nov 30 '18 at 8:45





If it works remember to vote for the answer :)

– haroldolivieri
Nov 30 '18 at 8:45













Offcourse I will :) gonna try it in a few days

– Alex
Nov 30 '18 at 15:00





Offcourse I will :) gonna try it in a few days

– Alex
Nov 30 '18 at 15:00


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53527485%2fimplement-sharedpreference-in-getview%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks