notifyDataSetChanged();不刷新ListView?

时间:2017-11-04 18:38:35

标签: java android listview notifydatasetchanged

我正在处理笔记应用程序,但我在删除笔记时遇到了麻烦。我遇到的问题是在删除笔记后,它仍会显示在列表视图中。我正在尝试使用notifyDataSetChanged();但它似乎并没有为我工作:/我知道它可能是我的适配器出错了但是我很难缩小我的错误,因为我对android很新。任何帮助将不胜感激,谢谢先进! :d

MainActivity 公共类MainActivity扩展了AppCompatActivity {

private ListView mListViewNotes;
ArrayAdapter<Note> listAdapter;
ArrayList<Note> list_items = new ArrayList<>();
int count = 0;
String list_item;
Object mActionMode;
Toolbar app_bar;
ArrayList<Note> notes;
private String mNoteFileName;
private Note mLoadedNote;
Note loadedNote;
ImageView picTest;
NoteAdapter na;

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    app_bar = (Toolbar) findViewById(R.id.app_bar);
    TextView title_1 = (TextView) findViewById(R.id.title1);
    TextView title_2 = (TextView) findViewById(R.id.title2);
    TextView title_m = (TextView) findViewById(R.id.title_m);
    Typeface music_font = Typeface.createFromAsset(getAssets(), "fonts/melodymakernotesonly.ttf");
    Typeface notes_font = Typeface.createFromAsset(getAssets(), "fonts/the unseen.ttf");
    setSupportActionBar(app_bar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    getSupportActionBar().setElevation(12);
    title_1.setTypeface(music_font);
    title_2.setTypeface(notes_font);
    title_m.setTypeface(music_font);

    listAdapter = new ArrayAdapter<Note>(this, R.layout.item_note, R.id.item_note, list_items);

    mNoteFileName = getIntent().getStringExtra("NOTE_FILE");
    if (mNoteFileName != null && !mNoteFileName.isEmpty()) {
        loadedNote = Utilities.getNoteByName(this, mNoteFileName);
    }

    mListViewNotes = (ListView) findViewById(R.id.listview_notes);
    mListViewNotes.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    mListViewNotes.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
        @Override
        public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
            if (list_items.contains(notes.get(position))) {
                count = count-1;
                list_items.remove(notes.get(position));
                mode.setTitle(count + " Notes Selected");
            } else {
                count = count+1;
                list_items.add(notes.get(position));
                mode.setTitle(count + " Notes Selected");
            }

            if (count == 0) {
                mode.setTitle("No Notes Selected");
            }
        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {

            app_bar.setVisibility(View.GONE);
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.context_menu, menu);
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            deleteNote();
            return true;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            count = 0;
            app_bar.setVisibility(View.VISIBLE);
            list_items.clear();
        }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.add_note:
            startActivity(new Intent(this, NoteActivity.class));
            break;
        case R.id.action_settings:
            Log.d("list:", "testing");
    }

    return true;
}

@Override
protected void onResume() {
    super.onResume();
    mListViewNotes.setAdapter(null);

    notes = Utilities.getAllSavedNotes(this);

    if (notes == null || notes.size() == 0) {
        Toast.makeText(this, "No Notes!", Toast.LENGTH_LONG).show();
    } else {
        na = new NoteAdapter(this, R.layout.item_note, notes);
        mListViewNotes.setAdapter(na);

        mListViewNotes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String filename = ((Note) mListViewNotes.getItemAtPosition(position)).getDateTime() + Utilities.FileExtention;

                Intent view_note = new Intent(getApplicationContext(), NoteActivity.class);
                view_note.putExtra("NOTE_FILE", filename);
                startActivity(view_note);
            }
        });
    }
}

private void deleteNote() {

        if(list_items.contains(null)) {
            finish();
        } else {
            AlertDialog.Builder dialog = new AlertDialog.Builder(this)
                    .setTitle("Delete")
                    .setMessage("Are you sure?")
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            for (Note loadedNote : list_items) {
                                Utilities.deleteNote(getApplicationContext(), loadedNote.getDateTime() + Utilities.FileExtention);
                            }
                            na.notifyDataSetChanged();
                            listAdapter.notifyDataSetChanged();
                        }
                    })
                    .setNegativeButton("No", null)
                    .setCancelable(false);
            dialog.show();
        }
}

NoteAdapter

public class NoteAdapter extends ArrayAdapter<Note> {

public NoteAdapter(Context context, int resource, ArrayList<Note> notes) {
    super(context, resource, notes);



}

@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
    //return super.getView(position, convertView, parent);

    if(convertView == null) {
        convertView = LayoutInflater.from(getContext())
                .inflate(R.layout.item_note, null);
    }

    Note note = getItem(position);

    if(note != null) {
        TextView title = (TextView) convertView.findViewById(R.id.list_note_title);
        TextView content = (TextView) convertView.findViewById(R.id.list_note_content);
        TextView date = (TextView) convertView.findViewById(R.id.list_note_date);

        Typeface scribble_card = Typeface.createFromAsset(getContext().getAssets(), "fonts/the unseen.ttf");
        title.setTypeface(scribble_card);
        content.setTypeface(scribble_card);

        title.setText(note.getTitle());
        date.setText(note.getDateTimeFormatted(getContext()));

        if(note.getContent().length() > 25) {
            content.setText(note.getContent().substring(0,25) + "...");
        } else {
            content.setText(note.getContent());
        }

        if(note.getContent().length() <= 0) {
            content.setText("(Empty Note..)");
        } else {
            content.setText(note.getContent());
        }

        if (note.getTitle().length() <= 0) {
            title.setText("(Untitled)");
        } else {
            title.setText(note.getTitle());
        }
    }

    return convertView;
}

1 个答案:

答案 0 :(得分:0)

您正在使用列表设置适配器: -

 na = new NoteAdapter(this, R.layout.item_note, notes);
    mListViewNotes.setAdapter(na);

na.notifyDataSetChanged(); 仅在您的备注列表发生更改时才有效。 而且我没有看到您在备注列表中删除/添加/修改。 如果要从列表中删除项目,则需要将其从注释列表中删除。请参阅下文: -

notes.remove(position_to_remove); na.notifyDataSetChanged();