使用微调框删除由Firebase填充的列表视图,删除后不刷新

时间:2018-07-13 01:37:48

标签: android firebase listview spinner

我正在尝试根据微调框的选择过滤列表视图。我可以成功过滤列表视图。但是,当我尝试删除列表中的项目时,列表视图将不会刷新,甚至不会将未过滤的数据加载到列表中。以下是我到目前为止尝试过的。

我的适配器

public class NoteList extends ArrayAdapter<Notes> {
    private Activity context;
    private List<Notes> noteList;

    public NoteList(Activity context, List<Notes> noteList){
        super(context, R.layout.list_layout, noteList);
        this.context = context;
        this.noteList = noteList;
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View listViewItem = inflater.inflate(R.layout.list_layout, null, true);

        TextView textViewCategory = (TextView) listViewItem.findViewById(R.id.textViewCategory);
    // TextView textViewNote = (TextView) listViewItem.findViewById(R.id.textViewNote);
        TextView textViewRating = (TextView) listViewItem.findViewById(R.id.textViewRating);

        Notes note = noteList.get(position);

        textViewCategory.setText(note.getNoteCategory());
    // textViewNote.setText(note.getNote());
        textViewRating.setText(note.getRating());

        return listViewItem;
    }
}

以下是我的MyNotes活动

    public class MyNotes extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

        ListView listViewNotes;
        List<Notes> notesList;
        DatabaseReference databaseNotes;
        TextView category, date;
        Spinner spinner;
        NoteList adapter;
        String display;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_my_notes);

            Slidr.attach(this);

            listViewNotes = (ListView) findViewById(R.id.listViewNotes);
            category = (TextView) findViewById(R.id.textViewCategory);
            notesList = new ArrayList<>();
            date = (TextView) findViewById(R.id.DateTime);

            spinner = (Spinner) findViewById(R.id.notes_spinner);
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                    R.array.notes_array, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(adapter);

            spinner.setOnItemSelectedListener(this);

            databaseNotes = FirebaseDatabase.getInstance().getReference("notes");
            databaseNotes.keepSynced(true);

            String minutes = DateFormat.getDateTimeInstance().format(new Date());
            date.setText(minutes);


            listViewNotes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    Notes notes = notesList.get(i);
                    showUpdateDialog(notes.getNoteId(), notes.getNote(), notes.getRating(), notes.getNoteCategory(), notes.getCreationDateLong());
                    //return false;
                }
            });


        }

        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            display = adapterView.getItemAtPosition(i).toString();
            DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
            notesList.clear();

            if (display.equals("Grateful")) {
                notesList.clear();
                Query query = reference.child("notes").orderByChild("rating").equalTo(display);
                query.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {

                        notesList.clear();
                            for (DataSnapshot noteSnapshot : dataSnapshot.getChildren()) {

                                Log.d("Grateful Datasnapshot", ":"+noteSnapshot);
                                Notes notes = noteSnapshot.getValue(Notes.class);
                                notesList.add(notes);

                            }

                            adapter = new NoteList(MyNotes.this, notesList);
                            listViewNotes.setAdapter(adapter);
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
                Toast.makeText(this, "Selected: " + display, Toast.LENGTH_SHORT).show();
            }
            if (display.equals("Ungrateful")) {
                notesList.clear();
                Query query = reference.child("notes").orderByChild("rating").equalTo(display);
                query.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        notesList.clear();
                    // if (dataSnapshot.exists()) {

                            for (DataSnapshot noteSnapshot : dataSnapshot.getChildren()) {
                                Notes notes = noteSnapshot.getValue(Notes.class);
                                notesList.add(notes);
                                Log.d("Ungrateful Datasnapshot", ":"+noteSnapshot);
                            }

                            adapter = new NoteList(MyNotes.this, notesList);
                            listViewNotes.setAdapter(adapter);
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
                Toast.makeText(this, "Selected: " + display, Toast.LENGTH_SHORT).show();

            }  if (display.equals("All")){
                notesList.clear();
                databaseNotes.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                    notesList.clear();

                        for (DataSnapshot noteSnapshot : dataSnapshot.getChildren()) {
                            if(noteSnapshot.exists()) {
                                Notes notes = noteSnapshot.getValue(Notes.class);
                                notesList.add(notes);
                                Log.d("All Datasnapshot", ":"+noteSnapshot);
                            }

                        }
                        adapter = new NoteList(MyNotes.this, notesList);
                        listViewNotes.setAdapter(adapter);
                        //adapter.notifyDataSetChanged();
                    //  adapter.notifyDataSetInvalidated();

                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
                Toast.makeText(this, "Selected: " + display, Toast.LENGTH_SHORT).show();
            }

        }
        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }



            buttonDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    AlertDialog diaBox = AskOption(noteId);
                    diaBox.show();
                    alertDialog.dismiss();

                }
            });


        }

        private void deleteNote(String noteId) {
            DatabaseReference drNote = FirebaseDatabase.getInstance().getReference("notes").child(noteId);
            drNote.removeValue();
        // notesList.clear();


            //    Toast.makeText(this, "Note is deleted", Toast.LENGTH_LONG).show();
        }

    }

正如您在上面看到的那样,我试图通过选定的值进行过滤,例如All,Grateful和Ungrateful。删除过滤的“感恩”列表中的项目后,该列表将生成未过滤的列表。我已经搜索并尝试了类似的问题,但没有任何效果。

谢谢。

0 个答案:

没有答案