Reputation: 11
I am new to programming and have a problem. I wan't to save an Integer clicks
with sharedPreferences so i can save the state of my button
mButton
, but i can't get it done. So maybe you guys could help me out. I know it's a lot to ask but im really starting to get desperate.
Heres my code. I am also trying to assign the shared prefs inside a RecyclerView.Adapter class.
public class ExampleAdapter extends
RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {
private ArrayList<MyItem> mExampleList;
Context mContext;
SharedPreferences sharedPreferences;
private Button mButton;
private int clicks = 0;
public static class ExampleViewHolder extends RecyclerView.ViewHolder {
private TextView mTextView;
public ExampleViewHolder(View itemView) {
super(itemView);
mTextView = itemView.findViewById(R.id.tv_country);
}
}
public ExampleAdapter(ArrayList<MyItem> exampleList, Context context) {
mContext = context;
mExampleList = exampleList;
}
@Override
public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v =
LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item,
parent,
false);
ExampleViewHolder evh = new ExampleViewHolder(v);
mButton = v.findViewById(R.id.tv_button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clicks=clicks+1;
int previousclicks=sharedPreferences.getInt("totalclick",0);
if (clicks % 2 == 0)
mButton.setBackgroundResource(R.drawable.button_green);
else
mButton.setBackgroundResource(R.drawable.button_red);
}
});
return evh;
}
@Override
public void onBindViewHolder(ExampleViewHolder holder, int position) {
MyItem currentItem = mExampleList.get(position);
holder.mTextView.setText(currentItem.getTaskText());
sharedPreferences =
mContext.getSharedPreferences("click_sharedpreference",Context.MODE_PRIVATE);
}
@Override
public int getItemCount() {
return mExampleList.size();
}
}
Upvotes: 0
Views: 456
Reputation: 381
You need to pass context for sharedPreference in constructor of ExampleAdapter
private mContext;
SharedPreferences sharedPreference ;
public ExampleAdapter(ArrayList<MyItem> exampleList,Context context) {
mExampleList = exampleList;
mContext=context;
}
create SharedPreference
in onBindViewHolder
method
@Override
public void onBindViewHolder(ExampleViewHolder holder, int position) {
MyItem currentItem = mExampleList.get(position);
holder.mTextView.setText(currentItem.getTaskText());
sharedPreference=mContext.getSharedPreferences("click_sharedpreference",MODE_PRIVATE);
}
set the value of shared preference in onclick
of button
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clicks=clicks+1;
//if sharedPreference doesn't contain totalclick you will get default value 0
//if it contains then you will get the previous clicks
//then add those clicks to current clicks
int previousclicks=sharedPreference.getInt("totalclick",0); //default 0 click
sharedPreference.edit().putInt("totalclick",previousclicks+clicks).commit();
if (clicks % 2 == 0)
mButton.setBackgroundResource(R.drawable.button_green);
else
mButton.setBackgroundResource(R.drawable.button_red);
}
});
Upvotes: 0
Reputation: 303
first create a custom sharedPreferences class
import android.content.Context;
import android.content.SharedPreferences;
public class SavePref {
private Context context;
public SavePref(Context context){
this.context = context;
}
public static void saveInt(String key, int value) {
SharedPreferences sharedPref =
PreferenceManager.getDefaultSharedPreferences(context)
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(key, value);
editor.commit();
}
}
now create a cunstroctor for your adapter:
public static class ExampleViewHolder extends RecyclerView.ViewHolder {
privare SavePref savePref;
public ExampleViewHolder(SavePref savePref){
this.savePref = savePref;
}
}
then you can use savePref
in your code for saving integer in sharedPreferences
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clicks++;
savePref.saveInt("clickNumber",clicks)
if (clicks % 2 == 0) {
mButton.setBackgroundResource(R.drawable.button_green);
}else
mButton.setBackgroundResource(R.drawable.button_red);
}
});
Upvotes: 1