列表视图未显示更新的值

时间:2017-05-24 08:28:04

标签: android listview android-fragments

我在标签布局中有四个片段。在其中一个片段中,我在列表视图中显示10个音符。除此之外,我还为用户提供了使用对话框添加新笔记的选项。因此,在添加新注释时,listview也会刷新以显示新注释,但是当我切换到另一个选项卡然后返回到我的原始选项卡时,新注释不会显示在列表视图中。如何解决这个问题?

这是我的java代码:

public class NoteFragment extends Fragment {

ListView lv_notes;
Button btn_newNote;
ArrayList<NotesModel> notesModelArrayList;
private static NotesAdapter notesAdapter;
NotesModel newNote;

public NoteFragment() {
    // 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_note, container, false);

    lv_notes = (ListView)rootView.findViewById(R.id.lv_notes);

    btn_newNote = (Button)rootView.findViewById(R.id.btn_newNote);

    notesModelArrayList = new ArrayList<>();

    for (int i = 1; i <= 10; i++){
        notesModelArrayList.add(new NotesModel("Note " + i,"24/05/2017"));
    }

    notesAdapter = new NotesAdapter(notesModelArrayList, getContext());

    lv_notes.setAdapter(notesAdapter);

    btn_newNote.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LayoutInflater newNoteInflater = LayoutInflater.from(getContext());
            View newNoteView = newNoteInflater.inflate(R.layout.noteprompt,null);

            final AlertDialog.Builder noteDialogBuilder = new AlertDialog.Builder(getContext());

            noteDialogBuilder.setView(newNoteView);

            final EditText et_newNote = (EditText)newNoteView.findViewById(R.id.et_newNote);

            noteDialogBuilder.setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    newNote = new NotesModel(et_newNote.getText().toString(),LogFragment.day_d + "/" + (LogFragment.month_d + 1) + "/" + LogFragment.year_d);

                    notesModelArrayList.add(newNote);
                    notesAdapter = new NotesAdapter(notesModelArrayList, getContext());

                    lv_notes.setAdapter(notesAdapter);
                }
            }).setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

            AlertDialog newNoteDialog = noteDialogBuilder.create();
            newNoteDialog.show();
        }
    });

    return rootView;
}
}

2 个答案:

答案 0 :(得分:1)

您的问题是由片段生命周期引起的。如果它被销毁,片段的新实例将不会保留更新的列表,而是保留原始列表。要解决此问题,您必须保存数组:

片段即将被销毁时保存数据:

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putParcelableArrayList("notesList", notesModelArrayList);
}

创建片段时检索/创建数据:

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null) {
        notesModelArrayList = savedInstanceState.getParcelableArrayList("notesList");
    } else {
        for (int i = 1; i <= 10; i++){
            notesModelArrayList.add(new NotesModel("Note " + i,"24/05/2017"));
        }
    }
}

除此之外,我还有两件事需要提及:您应该在Parcelable课程中实施NotesModel以便能够保存它们,并且只有在您不关闭时才会有效该应用。如果您想要持久解决方案,请考虑使用SharedPreferences

编辑 - 忘了提及你应该从onCreateView删除以下几行:

notesModelArrayList = new ArrayList<>();

for (int i = 1; i <= 10; i++){
    notesModelArrayList.add(new NotesModel("Note " + i,"24/05/2017"));
}

答案 1 :(得分:-1)

而不是这2行

notesAdapter = new NotesAdapter(notesModelArrayList, getContext());

lv_notes.setAdapter(notesAdapter);

尝试在notesAdapter.notifyDatasetChanged();内点击setPositiveButton onclick。