How to save state when item is Clicked in Android recyclerVIew

前提是你 提交于 2019-12-13 04:43:57

问题


I have created a recyclerView and handle items in it being clicked. Now I need to change the text displayed when one item is clicked. First it says 'day' and after I click it it should say 'finished'. It worked but after I close and re-open the activity it says 'day' again. So what do I need to do to make the item text persist?

  private OnFragmentInteractionListener mListener;
    List<String> list;
    MyListAdapter adapter;
    RecyclerView recyclerView;
    String pressed="finished";
 public MainFragment() {
        // Required empty public constructor
    }

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView= inflater.inflate(R.layout.fragment_main, container, false);

        recyclerView=(RecyclerView)rootView.findViewById(R.id.item_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        list=new ArrayList<>();
        for(int i=1;i<50;i++){
            list.add("Day"+ i);
        }

        adapter =new MyListAdapter(getActivity(),list,this);
        recyclerView.setAdapter(adapter);


        return rootView;

    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onNoteClick(int position) {
        if (position == 0) {
            Log.d(TAG, "onNoteClick: ");
            list.set(0, "finished");
            adapter.getItemId(0);


            adapter.notifyItemChanged(0);
            adapter.notifyDataSetChanged();







            Fragment fr = new Day1();
            FragmentManager fm = getFragmentManager();
            FragmentTransaction fragmentTransaction = fm.beginTransaction();
            fragmentTransaction.replace(R.id.Mainfragment1, fr,null).addToBackStack(null);
            fragmentTransaction.commit();

        }

    }

And adapter code

 Context context;
List<String>list;
public TextView textView;
public String finished="finished";
private MyViewHolder.OnNoteClickListener mOnNoteClickListener;

public MyListAdapter(Context context, List<String>list, MyViewHolder.OnNoteClickListener onNoteClickListener) {
    this.list=list;
    this.context=context;
    this.mOnNoteClickListener=onNoteClickListener;
}

public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    Button textView;


    OnNoteClickListener onNoteClickListener;

    public MyViewHolder(View itemView,OnNoteClickListener onNoteClickListener) {
        super(itemView);
        textView=(Button) itemView.findViewById(R.id.textView);
        itemView.setOnClickListener(this);
        textView.setOnClickListener(this);
        this.onNoteClickListener=onNoteClickListener;


        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        onNoteClickListener.onNoteClick(getAdapterPosition());





    }



    public interface OnNoteClickListener{
        void onNoteClick(int position);

    }
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, final int viewType) {
    View view= LayoutInflater.from(context).inflate(R.layout.layout_list_item,parent,false);
    final MyViewHolder vHolder=new MyViewHolder(view,mOnNoteClickListener);

    return vHolder;

}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    holder.textView.setText(list.get(position));




}

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

}


回答1:


Actually what your making is changing the View only, in other words your adapter is feching data from when the data is stored (i.e database) and display it in the recyclerView, so what you are changing here is the last value displyed in the recycler view, to change the value permanently you shoud get the posistion from the recyclerView and set the data both in the view and the database.

@Override
public void onNoteClick(int position) {
    if (position == 0) {
        Log.d(TAG, "onNoteClick: ");
        list.set(0, "Finished");
        adapter.getItemId(0);
        // Here make the change in the source of data
        adapter.notifyDataSetChanged();
}



回答2:


you can set your clicked item in arrayList and save that array list in sharedPreference and when you came again then retrieve the arrayList from sharedPrefrence and check the ids of checked items from arrayList and when you set adapter then you can set a check in your adapter onBindViewHolder which id contains the arrayList then change the text in "finished" other wise set your text "day".

you can create an AppPreference class:-

public class AppPreference {
    private static SharedPreferences mPreferences;
    private static SharedPreferences.Editor mPreferencesEditor;

    public static Set<String> getCamEval(Context ctx) {
            mPreferences = PreferenceManager.getDefaultSharedPreferences(ctx);
            return mPreferences.getStringSet("sdk_camEval", null);
        }

        public static void setCamEval(Context ctx, ArrayList<String> value) {
            mPreferences = PreferenceManager.getDefaultSharedPreferences(ctx);
            mPreferencesEditor = mPreferences.edit();
            Set<String> set = new HashSet<>();
            set.addAll(value);
            mPreferencesEditor.putStringSet("sdk_camEval", set);
            mPreferencesEditor.commit();
        }
}

or you can call this methods from anywhere in your project :-

to save your list :-

SdkPreferences.setCamEval(activity, yourList);

or to get this list:-

iconNameList.addAll(getCamEval(this));



回答3:


I found the problem and solved it using gSon,and creating new java class for my listview.bcs in this case it was be creating over and over all time and it was the problem



来源:https://stackoverflow.com/questions/56062842/how-to-save-state-when-item-is-clicked-in-android-recyclerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!