我使用Firebase实时数据库和显示数据库项目的RecyclerView。我希望用户能够通过长按来删除视图和数据库中的项目,并且它适用于数据库,但是当在视图中删除该项目时,它会复制其他项目(仅限于视图,而不是在数据库中)。例如,如果我有3个项目a,b和c,并且我删除b,我将在我的视图中包含项目a,c,a和c。如果之后我删除了其中一个c,例如第二个,我得到a,c,a和a。项目c从我的数据库中消失。我该如何解决? 这是我的ItemDisplayActivity:
public class ItemDisplayActivity extends AppCompatActivity {
private RecyclerView mItemRecyclerView;
private ItemAdapter mItemAdapter;
private ArrayList<Item> mItemList = new ArrayList<Item>();
FirebaseDatabase database;
DatabaseReference myRef;
private static final String TAG = NewItemActivity.class.getSimpleName();
private Context context;
private int realSize = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_display);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
context = this;
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(ItemDisplayActivity.this, NewItemActivity.class));
}
});
mItemRecyclerView = (RecyclerView) findViewById(R.id.activity_main_item_recycler_view);
database = FirebaseDatabase.getInstance();
myRef = database.getReference("items");
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
mItemRecyclerView.setLayoutManager(mLayoutManager);
mItemRecyclerView.setItemAnimator(new DefaultItemAnimator());
mItemAdapter = new ItemAdapter(this, mItemList);
mItemRecyclerView.setAdapter(mItemAdapter);
final String currentUserEmail = FirebaseAuth.getInstance().getCurrentUser().getEmail();
String[] userList2 = getIntent().getStringArrayExtra(KEY);
final String[] allUsers = new String[11];
allUsers[0] = currentUserEmail;
if (userList2 != null) {
for (int i = 0; i < 10; i++) {
if (userList2[i] != null) {
allUsers[i + 1] = userList2[i];
realSize++;
}
}
}
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
Item value = dataSnapshot1.getValue(Item.class);
String valueEmail = "";
if (value.getUserEmail() != null) {
valueEmail = value.getUserEmail();
}
for (int i = 0; i < realSize; i++) {
if (allUsers[i].equals(valueEmail)) {
Item item = new Item();
String name = value.getName();
Integer amount = value.getAmount();
String container = value.getContainer();
String expiryDate = value.getExpiryDate();
String reminderDate = value.getReminderDate();
String owner = value.getOwner();
Double price = value.getPrice();
item.setName(name);
item.setAmount(amount);
item.setContainer(container);
item.setExpiryDate(expiryDate);
item.setReminderDate(reminderDate);
item.setOwner(owner);
item.setPrice(price);
mItemList.add(item);
mItemAdapter.notifyDataSetChanged();
}
}
}
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w("Hello", "Failed to read value.", error.toException());
}
});
/**
* Creates an instance of the item touch listener
* Implements an onClick method
*/
mItemRecyclerView.addOnItemTouchListener(new ItemTouchListener(getApplicationContext(),
mItemRecyclerView, new ItemTouchListener.ClickListener() {
/**
* When clicking on an item, creates a toast message,
* hides the arrival time for that row only and shows the ProgressBar on that row
* for 2 seconds using a thread
* Updates the arrival time for that row only.
*
* @param view
* @param position
*/
@Override
public void onClick(View view, int position) {
Toast.makeText(getApplicationContext(), "Select new amount",
Toast.LENGTH_LONG).show();
final Item item = mItemList.get(position);
// Creating alert Dialog with one Button
AlertDialog.Builder alertDialog = new AlertDialog.Builder(ItemDisplayActivity.this);
//AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
// Setting Dialog Title
alertDialog.setTitle("New Amount");
// Setting Dialog Message
alertDialog.setMessage("Enter New Amount");
final EditText input = new EditText(ItemDisplayActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
input.setLayoutParams(lp);
alertDialog.setView(input);
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.banana);
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("Update",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog
Toast.makeText(getApplicationContext(), "Amount Updated", Toast.LENGTH_SHORT).show();
Integer newAmount = Integer.parseInt(input.getText().toString());
item.setAmount(newAmount);
mItemAdapter.notifyDataSetChanged();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog
dialog.cancel();
}
});
// closed
// Showing Alert Message
alertDialog.show();
}
//Overrides the onLongClick method
@Override
public void onLongClick(View view,final int position) {
Item item = mItemList.get(position);
mItemList.remove(position);
mItemAdapter.notifyItemRemoved(position);
mItemAdapter.notifyItemRangeChanged(position, mItemList.size());
DatabaseReference myRef = FirebaseDatabase.getInstance().getReference();
Query deleteQuery = myRef.child("items").orderByChild("name").equalTo(item.getName());
deleteQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot deleteSnapshot : dataSnapshot.getChildren()) {
deleteSnapshot.getRef().removeValue();
Log.d(TAG, "it tried");
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, "onCancelled", databaseError.toException());
}
});
mItemAdapter.notifyDataSetChanged();
}
}));
}
我的ItemAdapter代码:
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> {
private ArrayList<Item> items;
private Context context;
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView text_name, text_amount, text_container, text_date, text_reminder_date,
text_owner, text_price;
public ViewHolder(View itemView) {
super(itemView);
text_name = (TextView) itemView.findViewById(R.id.layout_middle_name);
text_amount = (TextView) itemView.findViewById(R.id.layout_middle_amount);
text_container = (TextView) itemView.findViewById(R.id.layout_middle_container);
text_date = (TextView) itemView.findViewById(R.id.layout_middle_expiry_date);
text_reminder_date = (TextView) itemView.findViewById(R.id.layout_middle_reminder_date);
text_owner = (TextView) itemView.findViewById(R.id.layout_right_owner);
text_price = (TextView) itemView.findViewById(R.id.layout_right_price);
}
}
public ItemAdapter(Context context, ArrayList<Item> items) {
this.items = items;
this.context = context;
}
@NonNull
@Override
public ItemAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_view, parent, false);
return new ItemAdapter.ViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull ItemAdapter.ViewHolder holder, int position) {
Item item = items.get(position);
holder.text_name.setText(item.getName());
holder.text_amount.setText(item.getAmount() + "");
holder.text_container.setText(item.getContainer());
holder.text_date.setText(item.getExpiryDate());
holder.text_reminder_date.setText(item.getReminderDate());
holder.text_owner.setText(item.getOwner());
holder.text_price.setText(item.getPrice() + "$");
}
@Override
public int getItemCount() {
return items.size();
}
}
答案 0 :(得分:0)
我通过添加mItemList.clear()解决了我的问题;在onDataChange方法中:
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
mItemList.clear(); //This way the item list is updated
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
Item value = dataSnapshot1.getValue(Item.class);
答案 1 :(得分:0)
@Override
public void onLongClick(View view,final int position)
Item item = mItemList.get(
mItemList.clear();
mItemList.remove(position);
mItemAdapter.notifyItemRemoved(position);
mItemAdapter.notifyItemRangeChanged(position, mItemList.size());
尝试执行此操作确实对我有帮助。