在 recyclerview 中删除项目后我的数据 return
my data return after remove item in recyclerview
我有一个应用程序,它所做的其中一件事就是将项目放入自定义列表视图中。现在我想使用 setOnClickListener 删除一个项目。
该项目确实消失了,但问题是当我退出并打开应用程序或在选项卡之间滚动时,项目 return 到自定义列表视图。
谁能帮帮我?
activity
loadData();
mRecyclerView = v.findViewById(R.id.recyclerview2);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getContext());
mAdapter = new exampleAdapter(mExampleList);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter.notifyDataSetChanged();
mRecyclerView.setAdapter(mAdapter);
mAdapter.setOnItemClickListener(new exampleAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
Intent intent = new Intent(getActivity(),show.class);
intent.putExtra("Example Item", mExampleList.get(position));
startActivity(intent);
}
@Override
public void onDeleteClickListener(int position) {
removeItem(position);
}
});
return v;
}
public void removeItem(int position) {
mExampleList.remove(position);
mAdapter.notifyItemRemoved(position);
mAdapter.notifyDataSetChanged();
}
private void loadData() {
SharedPreferences sharedPreferences = this.getActivity().getSharedPreferences("shared preferences12", Context.MODE_PRIVATE);
Gson gson = new Gson();
String json = sharedPreferences.getString("task list12", null);
Type type = new TypeToken<ArrayList<exampleItem>>() {
}.getType();
mExampleList = gson.fromJson(json, type);
if (mExampleList == null) {
mExampleList = new ArrayList<>();
}
}
private void buildRecyclerView(View view) {
}
}
适配器
public static class ExampleViewHolder extends RecyclerView.ViewHolder {
public TextView mTextViewLine1;
public TextView mTextViewLine2;
public TextView mTextViewLine3;
public ImageView imageView;
public ImageView imageViewD;
RecyclerView mRecyclerView;
public ExampleViewHolder(View itemView,final OnItemClickListener listener) {
super(itemView);
mTextViewLine1 = itemView.findViewById(R.id.textview_line1);
mTextViewLine2 = itemView.findViewById(R.id.textview_line2);
mTextViewLine3 = itemView.findViewById(R.id.textview_line3);
imageView = itemView.findViewById(R.id.icond);
imageViewD = itemView.findViewById(R.id.imageViewDdd);
mRecyclerView = itemView.findViewById(R.id.recyclerview2);
imageViewD.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
listener.onDeleteClickListener(position);
}
}
}
});
}
}
删除项目后忘记保存列表:
public void removeItem(int position) {
mExampleList.remove(position);
// saving the list to shared preference:
SharedPreferences sharedPreferences = this.getActivity().getSharedPreferences("shared preferences12", Context.MODE_PRIVATE);
sharedPreferences.edit().putString("task list12", new Gson().toJson(mExampleList)).apply();
///////////// end of saving
mAdapter.notifyItemRemoved(position);
mAdapter.notifyDataSetChanged();
}
我有一个应用程序,它所做的其中一件事就是将项目放入自定义列表视图中。现在我想使用 setOnClickListener 删除一个项目。 该项目确实消失了,但问题是当我退出并打开应用程序或在选项卡之间滚动时,项目 return 到自定义列表视图。 谁能帮帮我?
activity
loadData();
mRecyclerView = v.findViewById(R.id.recyclerview2);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getContext());
mAdapter = new exampleAdapter(mExampleList);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter.notifyDataSetChanged();
mRecyclerView.setAdapter(mAdapter);
mAdapter.setOnItemClickListener(new exampleAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
Intent intent = new Intent(getActivity(),show.class);
intent.putExtra("Example Item", mExampleList.get(position));
startActivity(intent);
}
@Override
public void onDeleteClickListener(int position) {
removeItem(position);
}
});
return v;
}
public void removeItem(int position) {
mExampleList.remove(position);
mAdapter.notifyItemRemoved(position);
mAdapter.notifyDataSetChanged();
}
private void loadData() {
SharedPreferences sharedPreferences = this.getActivity().getSharedPreferences("shared preferences12", Context.MODE_PRIVATE);
Gson gson = new Gson();
String json = sharedPreferences.getString("task list12", null);
Type type = new TypeToken<ArrayList<exampleItem>>() {
}.getType();
mExampleList = gson.fromJson(json, type);
if (mExampleList == null) {
mExampleList = new ArrayList<>();
}
}
private void buildRecyclerView(View view) {
}
}
适配器
public static class ExampleViewHolder extends RecyclerView.ViewHolder {
public TextView mTextViewLine1;
public TextView mTextViewLine2;
public TextView mTextViewLine3;
public ImageView imageView;
public ImageView imageViewD;
RecyclerView mRecyclerView;
public ExampleViewHolder(View itemView,final OnItemClickListener listener) {
super(itemView);
mTextViewLine1 = itemView.findViewById(R.id.textview_line1);
mTextViewLine2 = itemView.findViewById(R.id.textview_line2);
mTextViewLine3 = itemView.findViewById(R.id.textview_line3);
imageView = itemView.findViewById(R.id.icond);
imageViewD = itemView.findViewById(R.id.imageViewDdd);
mRecyclerView = itemView.findViewById(R.id.recyclerview2);
imageViewD.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
listener.onDeleteClickListener(position);
}
}
}
});
}
}
删除项目后忘记保存列表:
public void removeItem(int position) {
mExampleList.remove(position);
// saving the list to shared preference:
SharedPreferences sharedPreferences = this.getActivity().getSharedPreferences("shared preferences12", Context.MODE_PRIVATE);
sharedPreferences.edit().putString("task list12", new Gson().toJson(mExampleList)).apply();
///////////// end of saving
mAdapter.notifyItemRemoved(position);
mAdapter.notifyDataSetChanged();
}