在Android回收器中单击项目时如何保存状态 [英] How to save state when item is Clicked in Android recyclerVIew

查看:71
本文介绍了在Android回收器中单击项目时如何保存状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个recyclerView并处理了被单击的项目.现在,我需要更改单击一项时显示的文本.首先它说天",然后单击它应该说完成".它起作用了,但是当我关闭并重新打开活动后,它又说了一天".那么,我该怎么做才能使项目文本保持不变?

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

        }

    }

和适配器代码

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

}

推荐答案

实际上,您所做的只是更改视图",换句话说,适配器从存储数据(即数据库)时获取数据并显示它在recyclerView中,因此您要更改的是在recycler视图中显示的最后一个值,要永久更改该值,您应该从recyclerView中获取位置并在视图和数据库中都设置数据.

Actually, what your making is changing the View only, in other words, your adapter is fetching 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 displayed in the recycler view, to change the value permanently you should get the position 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();
}

这篇关于在Android回收器中单击项目时如何保存状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆