我创建了一个recyclerView并处理了被单击的项目。现在,我需要更改单击一项时显示的文本。首先它说“天”,然后单击它应该说“完成”。它起作用了,但是当我关闭并重新打开活动后,它又说“天”。那么,我该怎么做才能使项目文本保持不变?
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();
}
}
答案 0 :(得分:0)
实际上,您所做的只是更改View,换句话说,适配器从数据存储时(即数据库)获取数据并将其显示在recyclerView中,因此此处更改的是上一次显示的值在回收站视图中,要永久更改值,您应该从recyclerView中获取数据,并在视图和数据库中设置数据。
@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();
}
答案 1 :(得分:0)
您可以在arrayList中设置您单击的项目,并将该数组列表保存在sharedPreference中,当您再次访问时,可以从sharedPrefrence检索arrayList并从arrayList中检查选中项的ID,而在设置适配器时,可以设置签入您的适配器onBindViewHolder(其ID包含arrayList)然后更改“完成”中的文本,否则将文本设置为“天”。
您可以创建一个AppPreference类:-
import emoji # Needs 'pip install emoji'
s['icons'] = s['emoji'].apply(lambda x: emoji.emojize(x))
或者您可以从项目中的任何地方调用此方法:-
保存您的列表:-
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();
}
}
或获取此列表:-
SdkPreferences.setCamEval(activity, yourList);
答案 2 :(得分:0)
我找到了问题并使用gSon进行了解决,并为我的listview.bcs创建了新的Java类,在这种情况下,它一直都在创建,这就是问题