I got a Viewpager2
adapter , with 2 EditText
and 2 TextView
in it.
At the beginning, the EditTexts are visible, and the Textviews are gone. If I click on a button, if what is written inside 1 (or both) EditText is the good answer, the dedicated EditText is set to gone, and it Textview (displaying the answer) is now set to visible. The problem is that these visibility changes are not saved, when I quit and come back to this Viewpager,Textviews are gone and EditText are visible, as if nothing happened.
"Artiste" and "Titre" are the EditTexts
"titretext" and "artistetext" are the textviews
.
I tried to use SaveInstanceState
, but since EditTexts are only defined in the adapter, it doesn't seems to me to be the solution.
That's why I tried to use SharedPreferences
, but I don't really know how it works inside an adapter, and I get errors when launching it. I tried to save the changes of visibility inside a boolean as I saw on other programms.
Errors :
error: missing return statement
}
error: unreachable statement
artistetextVisibility = preferences.getBoolean("Atextvisibility", false);
Here is my code :
Adapter :
public class viewpageradapter_rap_level1 extends RecyclerView.Adapter<viewpageradapter_rap_level1.ViewHolder> {
private List<Integer> mimages;
private LayoutInflater mInflater;
private ViewPager2 viewPager2;
private List<String> artistetext;
private List<String> titretext;
private boolean artistetextVisibility, titretextVisibility, artisteVisibility, titreVisibility;
Context context;
SharedPreferences preferences = context.getSharedPreferences("MyPref", MODE_PRIVATE);
viewpageradapter_rap_level1(Context context, ViewPager2 viewPager2, List<Integer> images,int position, List<String> artistetext, List<String> titretext) {
this.mInflater = LayoutInflater.from(context);
this.mimages = images;
this.viewPager2 = viewPager2;
this.artistetext= artistetext;
this.titretext= titretext;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.viewpager_content_rap_level1, parent, false);
return new ViewHolder(view);
artistetextVisibility = preferences.getBoolean("Atextvisibility", false);
titretextVisibility = preferences.getBoolean("Ttextvisibility", false);
artisteVisibility = preferences.getBoolean("Avisibility", true);
titreVisibility = preferences.getBoolean("Tvisibility", true);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.gridIcon.setImageResource(mimages.get(position));
holder.ArtisteTexts.setText(artistetext.get(position));
holder.TitreTexts.setText(titretext.get(position));
}
@Override
public int getItemCount() {
return mimages.size();
}
// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder {
EditText Artiste;
Button button;
ImageView gridIcon;
EditText Titre;
TextView ArtisteTexts;
TextView TitreTexts;
ViewHolder(View itemView) {
super(itemView);
Artiste = itemView.findViewById(R.id.Artiste);
button = itemView.findViewById(R.id.btnToggle);
gridIcon = itemView.findViewById(R.id.imageView2);
Titre = itemView.findViewById(R.id.titre);
ArtisteTexts = itemView.findViewById(R.id.Artistetext);
TitreTexts = itemView.findViewById(R.id.TitreTexts);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences preferences = context.getSharedPreferences("MyPref", MODE_PRIVATE);
artistetextVisibility = preferences.getBoolean("Atextvisibility", false);
titretextVisibility = preferences.getBoolean("Ttextvisibility", false);
artisteVisibility = preferences.getBoolean("Avisibility", true);
titreVisibility = preferences.getBoolean("Ttextvisibility", true);
String ValueTitre = Titre.getText().toString();
String ValueArtiste = Artiste.getText().toString();
if (getAdapterPosition() == 0)
{
if(ValueTitre.equalsIgnoreCase("Random Access Memories"))
{
Toast.makeText(v.getContext(), "done", Toast.LENGTH_LONG).show();
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("Ttextvisibility", true);
editor.putBoolean("Tvisibility", false);
editor.commit();
Titre.setVisibility(View.GONE);
TitreTexts.setVisibility(View.VISIBLE);
}
if(ValueArtiste.equalsIgnoreCase("Daft Punk"))
{
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("Atextvisibility", true);
editor.putBoolean("Avisibility", false);
editor.commit();
Titre.setVisibility(View.GONE);
TitreTexts.setVisibility(View.VISIBLE);
}
else {
Toast.makeText(v.getContext(), "error", Toast.LENGTH_LONG).show();
}
}
}
});
}
}
Thanks for your time.
EDIT:
I managed to store boolean values after I validate an answer but it is still not working when going back on the activity with this code (Onclick inside Viewholder) :
if (getAdapterPosition() == 0)
{
if(ValueTitre.equalsIgnoreCase("Random Access Memories"))
{
Toast.makeText(v.getContext(), "Album validé!", Toast.LENGTH_LONG).show();
SharedPreferences preferences = mContext.getSharedPreferences("MyPref", MODE_PRIVATE);
preferences.edit().putBoolean("Ttextvisibility", true).commit();
preferences.edit().putBoolean("Tvisibility", false).commit();
Titre.setVisibility(View.GONE);
TitreTexts.setVisibility(View.VISIBLE);
}
I have some questions.
1)The boolean is set to false, as my Textview is invisible. I validate the answer, and replace it true. I'm testing,inside my viewholder, if the boolean is correctly replaced inside sharedpreferences, with a button which checks if the boolean equals to "True" after the validation, and it seems to be equals to "True" everytime, even when I terminate and launch the app once again. Why? If I terminate the app, and so the answer has never been validate, why is it saying that the boolean is True ?
button_orientation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View u) {
SharedPreferences preferences = mContext.getSharedPreferences("MyPref", MODE_PRIVATE);
boolean value =preferences.getBoolean("Ttextvisibility", false);
if (value == true) {
Toast.makeText(u.getContext(), "Value", Toast.LENGTH_LONG).show();
}}
With this change of boolean, I try to change the visibility of the Item with this inside my public class Viewholder :
SharedPreferences preferences = mContext.getSharedPreferences("MyPref", MODE_PRIVATE); boolean value =preferences.getBoolean("Ttextvisibility", false);
and then this inside my viewholder, since the getAdapterPosition() isn't working inside the viewholder class alone (I need this method because it has to change specific textviews depending on the position inside the viewpager).
ViewHolder(View itemView) {
super(itemView);
...
ArtisteTexts = itemView.findViewById(R.id.Artistetext);
TitreTexts = itemView.findViewById(R.id.TitreTexts);
if (getAdapterPosition() == 0) {
if (value == true) {
Titre.setVisibility(View.GONE);
TitreTexts.setVisibility(View.VISIBLE);
}
}
It changes nothing, the visibilities are set as at the beginning.... I'm really lost, I don't know what to do anymore, maybe it is in the wrong class? Hope anyone can help me..