gpt4 book ai didi

android - 如何在没有 finish() 的情况下从另一个类刷新 RecyclerView

转载 作者:行者123 更新时间:2023-11-29 02:31:49 30 4
gpt4 key购买 nike

我在 MainActivity 类中创建了一个 fragment ,它显示了带有 ArrayList 的 RecyclerView。在 NewNote 类中,我将一个新项目添加到列表中,并将更改后的列表传递给内部存储上的文件。当我在添加新笔记后从 NewNote 返回到 MainActivity 时,RecyclerView 显示旧列表而不是新列表。当我重新启动我的应用程序时,MainActivity 显示新列表,这要归功于 onCreate 方法,该方法使用内部存储中的新列表再次加载 fragment 。我的问题是如何重新加载显示列表,比如当我重新启动我的应用程序时。我在堆栈上阅读了很多类似的问题(大多数答案是 notifyDataSetChanged())但我不知道在我的情况下该怎么做,尝试了我发现的一切。这是我的代码:

MainActivity.java

public class MainActivity extends AppCompatActivity {
private FloatingActionButton addNewNoteButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ListFragment fragment = new ListFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragmencik, fragment);
fragmentTransaction.commit();

addNewNoteButton = findViewById(R.id.add_new_note);
addNewNoteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, NewNote.class);
startActivity(intent);
}
});
}
}

NewNote.java

public class NewNote extends AppCompatActivity {

private EditText text;
private ArrayList<Note> noteArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_note_view);
noteArrayList = new ArrayList<>();
text= findViewById(R.id.text);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(item.getIcon().getConstantState().equals(getResources().getDrawable(R.drawable.ic_save_white_36dp).getConstantState()))
{
Note note = new Note(text.getText().toString());
addNewNoteToList(note);
}
return super.onOptionsItemSelected(item);
}

public void addNewNoteToList(Note note)
{
SharedPreferences sharedPreferences = getSharedPreferences("notes file", MODE_PRIVATE);
Gson gson = new Gson();
String json = sharedPreferences.getString("notes list", null);
Type type= new TypeToken<ArrayList<Note>>() {}.getType();
noteArrayList = gson.fromJson(json, type);
if(noteArrayList==null)
noteArrayList=new ArrayList<>();
noteArrayList.add(note);

SharedPreferences.Editor editor = sharedPreferences.edit();
Gson gson2 = new Gson();
String json2 = gson2.toJson(noteArrayList);
editor.putString("notes list", json2);
editor.apply();

}
}

ListFragment.java

public class ListFragment extends Fragment {

private ArrayList<Note> noteArrayList;
private ListAdapter listAdapter;

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

loadArrayList();

View view = inflater.inflate(R.layout.fragment_list, container, false);

RecyclerView recyclerView = view.findViewById(R.id.listRecyclerView);

listAdapter = new ListAdapter(noteArrayList);
recyclerView.setAdapter(listAdapter);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);

return view;
}

public void loadArrayList()
{

SharedPreferences sharedPreferences = this.getActivity().getSharedPreferences("notes file", Context.MODE_PRIVATE);
Gson gson = new Gson();
String json = sharedPreferences.getString("notes list", null);
Type type= new TypeToken<ArrayList<Note>>() {}.getType();
noteArrayList = gson.fromJson(json, type);
}
}

ListAdapter.java

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListViewHolder> {
private ArrayList<Note> mCustomObjects;

public class ListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private TextView mItemText;

public ListViewHolder(View itemView) {

super(itemView);
mItemText = itemView.findViewById(R.id.itemText);
// mItemImage = itemView.findViewById(R.id.itemImage);
}}

@Override
public int getItemCount() {
int size=0;
if(mCustomObjects!=null)
size=mCustomObjects.size();
return size;
}

public ListAdapter(ArrayList<Note> arrayList) {
mCustomObjects = arrayList;
}

@Override
public ListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new ListViewHolder(view);
}

@Override
public void onBindViewHolder(ListViewHolder holder, int position) {
if(mCustomObjects!=null) {
Note note = mCustomObjects.get(position);
holder.mItemText.setText(note.getText());
}
}}

最佳答案

你可以做两件事:

第一个。如果你要添加一个新的笔记,但你有一个返回(返回)按钮,你可以启动一个 activityForResult 并比较笔记是否已添加然后刷新我的列表,用户按下返回只是什么都不做。

第二个。您可以使用事件总线发布事件以通知回收器 View 它必须更新,然后刷新数据。

如果没有实现,我假设您正在使用滑动来刷新。 https://developer.android.com/training/swipe/add-swipe-interface.html

关于android - 如何在没有 finish() 的情况下从另一个类刷新 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49307536/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com