在我的适配器中,我添加了 OnLongClickListener() 并打开一个弹出菜单,当您按下时从列表视图和 firestore 中删除该项目,但我没有成功刷新我的列表视图,所以当我删除该项目时,该项目将消失。
我使用了 notifyDataSetChanged() 并且当我添加一个新项目时它可以工作但是当我删除一个项目时它不起作用
我的列表视图: 公共类 NotesLV 扩展 AppCompatActivity {
private ArrayList<String> notes = new ArrayList<String>();
private ArrayAdapter<String> arrayAdapter;
private ListView listView;
private FirebaseFirestore db;
private static final String TAG="ALMA";
private ArrayList<Notes> allNotes;
private ArrayList<Notes> noNotes= new ArrayList<>();
private String mail;
private FloatingActionButton floatingbtn;
private NotesLVAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notes_lv);
listView = (ListView) findViewById(R.id.listView);
floatingbtn = (FloatingActionButton) findViewById(R.id.floatingActionButton);
floatingbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(NotesLV.this, CreateNote.class);
startActivity(intent);
/*Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();*/
}
});
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
mail = user.getEmail().toString();
//notes.add("Example Note");
//arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, notes);
allNotes = new ArrayList<>();
db = FirebaseFirestore.getInstance();
listView.setAdapter(arrayAdapter);
Log.d(TAG, "listview");
adapter = new NotesLVAdapter(NotesLV.this, allNotes);
listView.setAdapter(adapter);
//loadNotesToListView();
}
public void loadNotesToListView(){
allNotes.clear();
adapter.notifyDataSetChanged();
Log.d(TAG,"loading");
db.collection("Folder").document(mail).collection("Notes").orderBy("date",Query.Direction.DESCENDING).get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
// after getting the data we are calling on success method
// and inside this method we are checking if the received
// query snapshot is empty or not.
if (!queryDocumentSnapshots.isEmpty())
{
// if the snapshot is not empty we are hiding
// our progress bar and adding our data in a list.
List<DocumentSnapshot> list = queryDocumentSnapshots.getDocuments();
Log.d(TAG, "List size: " + list.size());
for (DocumentSnapshot d : list) {
// after getting this list we are passing
// that list to our object class.
Notes notes = d.toObject(Notes.class);
//Log.d("TOSTRING", ""+notes.getDate());
d.getId();
// after getting data from Firebase we are
// storing that data in our array list
allNotes.add(notes);
Log.d(TAG, "added");
}
// after that we are passing our array list to our adapter class.
adapter = new NotesLVAdapter(NotesLV.this, allNotes);
// after passing this array list to our adapter
// class we are setting our adapter to our list view.
listView.setAdapter(adapter);
Log.d(TAG, "adapter was set");
}
else
{
Log.d(TAG,"No data found in query");
// if the snapshot is empty we are displaying a toast message.
Toast.makeText(NotesLV.this, "No data found in Database", Toast.LENGTH_SHORT).show();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// we are displaying a toast message
// when we get any error from Firebase.
Toast.makeText(NotesLV.this, "Fail to load data..", Toast.LENGTH_SHORT).show();
}
});
}
@Override
protected void onResume() {
super.onResume();
adapter.notifyDataSetChanged();
loadNotesToListView();
}
}
我的适配器: 公共类 NotesLVAdapter 扩展 ArrayAdapter {
private FirebaseFirestore db;
private String mail;
public NotesLVAdapter(Context context, ArrayList<Notes> notes) {
super(context, 0, notes);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView==null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.activity_notes_d,parent, false);
}
Notes notes = getItem(position);
db= FirebaseFirestore.getInstance();
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
mail= user.getEmail().toString();
TextView tvNTitle = (TextView) listItemView.findViewById(R.id.tvNoteName);
tvNTitle.setText(notes.getNoteName());
TextView tvNDate = (TextView) listItemView.findViewById(R.id.tvDate);
tvNDate.setText(notes.getSavedDate());
ImageView imageBoss=(ImageView)listItemView.findViewById(R.id.imgBoss);
String pic="";
listItemView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
Log.d("ALMA","clicked");
Intent intent = new Intent(view.getContext(), CreateNote.class);
intent.putExtra("title",tvNTitle.getText().toString());
intent.putExtra("content",notes.getContent().toString());
intent.putExtra("reminder",notes.needReminder());
intent.putExtra("saved date",notes.getSavedDate().toString());
intent.putExtra("date",notes.getDate().toString());
view.getContext().startActivity(intent);
}
});
listItemView.setOnLongClickListener(new AdapterView.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
PopupMenu popupMenu=new PopupMenu(view.getContext(), view);
popupMenu.setGravity(Gravity.END);
popupMenu.getMenuInflater().inflate(R.menu.notes_popup_menu,popupMenu.getMenu());
popupMenu.show();
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
int id=menuItem.getItemId();
if(id==R.id.menu_edit) {
Intent intent = new Intent(view.getContext(), CreateNote.class);
intent.putExtra("title",tvNTitle.getText().toString());
intent.putExtra("content",notes.getContent().toString());
intent.putExtra("reminder",notes.needReminder());
intent.putExtra("date",notes.getDate().toString());
view.getContext().startActivity(intent);
}
if(id==R.id.menu_delete){
// below line is for getting the collection
// where we are storing our courses.
db.collection("Folder").document(mail).collection("Notes").document(notes.getNoteName()+", "+notes.getSavedDate()).delete()
// after deleting call on complete listener
// method to delete this data.
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
// inside on complete method we are checking
// if the task is success or not.
if (task.isSuccessful()) {
// this method is called when the task is success
// after deleting we are starting our MainActivity.
Log.d("ALMA","deleted");
} else {
// if the delete operation is failed
// we are displaying a toast message.
Log.d("ALMA","didnt delete");
}
}
});
}
return true;
}
});
return true;
}
});
return listItemView;
}
}