0

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();
                        }}
  1. 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..

1 Answers1

0

Would be good if you shared what errors you are getting and the rest of your code! :)

Ok, then:

Note that here:

  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);

everything after the return is unreachable, as you have already returned a value and left the method. The rest of the code is never/would be never executed

Manuel
  • 127
  • 2
  • 11
  • 1
    Oh yeah of course, I forgot mb : `error: unreachable statement artistetextVisibility = preferences.getBoolean("Atextvisibility", false);` and `error: missing return statement }` – Hugo Becker Feb 06 '21 at 18:13
  • Tell me after trying those changes – Manuel Feb 06 '21 at 18:23
  • If I understood good, I need to put `return new ViewHolder(view);` after the preferences ? If yes I tried, and I can't open my activity (the viewpager) anymore, but no error at the compilation – Hugo Becker Feb 06 '21 at 18:36
  • `java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jeu/com.example.jeu.Viewpager_rap_level1}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference` but as I am new with android (and more with sharedpreferences), i don't really understand this log :/ – Hugo Becker Feb 06 '21 at 20:16
  • I think that these kind of problem have more to do with Java as such than with Android :). Check this: https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it https://stackoverflow.com/questions/10464547/how-to-solve-java-lang-nullpointerexception-error – Manuel Feb 07 '21 at 08:01
  • Ok, seems logical that I can't call a variable that is null, but I don't understand why my Sharedpreference is null, since I put 4 boolean in it. Edit : I have delete the sharedpreference here : `public class viewpageradapter_rap_level1 extends RecyclerView.Adapter {` , but now when I validate the second edittext, it set the wrong Textview visible :') (and still not saving). I really hate sharedpreference, it doesn't seems intuitive to me – Hugo Becker Feb 07 '21 at 15:21