简体   繁体   中英

How do I save changes to buttons in Shared Preferences with in Recyclerview?

I have a button inside the Recyclerview when I press it it shows an image and when I press it the image disappears I want to save these changes in the shared preferences I searched a lot and did not find the appropriate code and this is the complete code

Blockquote

public class RecyclerViewAdapterBookA extends RecyclerView.Adapter < RecyclerViewAdapterBookA
        .catViewHolderBookA > {

    ArrayList < BookA > bookAS;
    private RecyclerViewOnItmBookA recyclerViewOnItm;
    private ArrayList < BookA > arraylist;
    private Context context;


    public RecyclerViewAdapterBookA ( Context context , ArrayList < BookA > bookAS ,
                                      RecyclerViewOnItmBookA recyclerViewOnItmBookA ) {
        this.bookAS = bookAS;
        this.recyclerViewOnItm = recyclerViewOnItmBookA;
        this.arraylist = new ArrayList <> ( );
        this.arraylist.addAll ( bookAS );
        this.context = context;


    }


    @Override
    public RecyclerViewAdapterBookA.catViewHolderBookA onCreateViewHolder ( ViewGroup parent , int viewType ) {

        View v = LayoutInflater.from ( parent.getContext ( ) ).inflate ( R.layout.booka ,
                null , false );

        RecyclerViewAdapterBookA.catViewHolderBookA viewHolder = new RecyclerViewAdapterBookA.catViewHolderBookA ( v );
        return viewHolder;
    }

    @Override
    public void onBindViewHolder ( RecyclerViewAdapterBookA.catViewHolderBookA holder , final int position ) {

        final BookA a = bookAS.get ( position );

        holder.img.setImageResource ( a.getImg ( ) );
        holder.text1.setText ( a.getText1 ( ) );
        holder.text2.setText ( a.getText2 ( ) );
        holder.img2.setImageResource ( a.getImg2 ( ) );
        holder.img3.setImageResource ( a.getImg3 ( ) );
        


    }

    @Override
    public int getItemCount () {
        return bookAS.size ( );
    }

//    هذا الكود لعدم تكرار امر الضغط على الزرار

    @Override
    public int getItemViewType ( int position ) {

        return position;
    }








    public void filter ( String charText ) {
        charText = charText.toLowerCase ( Locale.getDefault ( ) );
        bookAS.clear ( );
        if (charText.length ( ) == 0) {
            bookAS.addAll ( arraylist );
        } else {
            for (BookA wp : arraylist) {
                if (wp.getText1 ( ).toLowerCase ( Locale.getDefault ( ) ).contains ( charText )) {
                    bookAS.add ( wp );
                }
            }
        }
        notifyDataSetChanged ( );
    }


    public class catViewHolderBookA extends RecyclerView.ViewHolder {

        private ImageView img;
        private TextView text1;
        private TextView text2;
        private ImageView img2;
        private ImageView img3;
        private LinearLayout layout;


        public catViewHolderBookA ( View itemView ) {
            super ( itemView );

            img = itemView.findViewById ( R.id.shm );
            text1 = itemView.findViewById ( R.id.titleid );
            text2 = itemView.findViewById ( R.id.partid );
            img2 = itemView.findViewById ( R.id.start1 );
            img3 = itemView.findViewById ( R.id.start2 );
            layout = itemView.findViewById ( R.id.layout );
            img2.setTag ( 0 );





            itemView.findViewById ( R.id.layout ).setOnClickListener ( new View.OnClickListener ( ) {
                @Override
                public void onClick ( View view ) {


                    recyclerViewOnItm.onLayoutItmClick ( getAdapterPosition ( ) );
                }
            } );


 This is the button where I want to save my shared preferences
    
            itemView.findViewById ( R.id.start1 ).setOnClickListener ( new View.OnClickListener ( ) {
                public void onClick ( View view ) {
                    final int status = ( Integer ) view.getTag ( );
                    switch (status) {
                        case 0:
                            img3.setVisibility ( View.VISIBLE );
                            Toast.makeText ( view.getContext ( ) , "تم تعليمه گ مقروء" , Toast.LENGTH_SHORT ).show ( );
                            view.setTag ( 1 );
                            

                            break;
                        case 1:
                            img3.setVisibility ( View.INVISIBLE );
                            Toast.makeText ( view.getContext ( ) , "تم تعليمه گ غير مقروء" , Toast.LENGTH_SHORT ).show ( );
                            view.setTag ( 0 );
                        



                            break;


                    }

                    recyclerViewOnItm.ImageClick ( getAdapterPosition ( ) );
                  


                }



            } );


            itemView.setOnLongClickListener ( new View.OnLongClickListener ( ) {
                @Override
                public boolean onLongClick ( View view ) {
                    recyclerViewOnItm.onLongItmClick ( getAdapterPosition ( ) );
                    return false;
                }
            } );

        }


    }


}

Follow the code this will solve your problem

SharedPrefsUtils.java

public class SharedPrefsUtils {
    public static boolean getIsImageVisible(Context context){
        SharedPreferences sharedPreferences = context.getSharedPreferences("IsImageVisiblePrefs", Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean("IsImageVisibleValue", false);
    }

    public static void setIsImageVisible(Context context, boolean isVisible){
        SharedPreferences sharedPreferences = context.getSharedPreferences("IsImageVisiblePrefs", Context.MODE_PRIVATE);
        SharedPreferences.Editor sharedPreferencesEditor  = sharedPreferences.edit();
        sharedPreferencesEditor.putBoolean("IsImageVisibleValue", isVisible);
        sharedPreferencesEditor.apply();
    }
}

CatViewHolderBookA

Here is the code where you want to save your shared preferences

   itemView.findViewById ( R.id.start1 ).setOnClickListener ( new View.OnClickListener ( ) {
                public void onClick ( View view ) {
                    final int status = ( Integer ) view.getTag ( );
                    switch (status) {
                        case 0:
                            img3.setVisibility ( View.VISIBLE );
                            Toast.makeText ( view.getContext ( ) , "تم تعليمه گ مقروء" , Toast.LENGTH_SHORT ).show ( );
                            view.setTag ( 1 );
                            SharedPrefsUtils.setIsImageVisible(view.getContext ( ),true)
                            break;
                        case 1:
                            img3.setVisibility ( View.INVISIBLE );
                            Toast.makeText ( view.getContext ( ) , "تم تعليمه گ غير مقروء" , Toast.LENGTH_SHORT ).show ( );
                            view.setTag ( 0 );
                            SharedPrefsUtils.setIsImageVisible(view.getContext ( ),false)
                            break;

                    }

                    recyclerViewOnItm.ImageClick ( getAdapterPosition ( ) );

                }

            } );

Use getIsImageVisible() function where you want to get SharedPreferences value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM