@Override
public void onBindViewHolder(final BookViewHolder holder, final int position) {
final Upload uploadCurrent = mUploads.get(position);
holder.iBookName.setText(uploadCurrent.getBookName());
holder.iBookAuthor.setText(uploadCurrent.getBookAuthor());
holder.iBookPublisher.setText(uploadCurrent.getBookPublisher());
/*holder.iBookDesc.setText(uploadCurrent.getBookDesc());*/
Picasso.with(mContext)
.load(uploadCurrent.getBookImage())
.placeholder(R.drawable.blank)
.fit()
.into(holder.iBookImage);
holder.iBookMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PopupMenu popup = new PopupMenu(mContext, holder.iBookMenu);
popup.inflate(R.menu.cardview_menu);
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.add_my_list:
break;
case R.id.delete_my_list:
deleteBook(mUploads.get(position).getBookId());
break;
}
return false;
}
});
popup.show();
}
});
}
private void deleteBook(String id) {
DatabaseReference dR = FirebaseDatabase.getInstance().getReference("Books").child(id);
dR.removeValue();
Toast.makeText(mContext, "Book deleted", Toast.LENGTH_SHORT).show();
}
@Override
public int getItemCount() {
return mUploads.size();
}
}
我创建了一个图书应用。我在弹出菜单中设计了cardview,您可以删除项目。我可以删除项目,但看起来仍然不错。我的问题是删除项目时recyclerview无法刷新。你能帮助我吗?我在这里尝试了很多方法,但无法解决问题。
答案 0 :(得分:1)
您必须在删除项目刷新适配器之后
@Override
public void onBindViewHolder(final BookViewHolder holder, final int position) {
final Upload uploadCurrent = mUploads.get(position);
holder.iBookName.setText(uploadCurrent.getBookName());
holder.iBookAuthor.setText(uploadCurrent.getBookAuthor());
holder.iBookPublisher.setText(uploadCurrent.getBookPublisher());
/*holder.iBookDesc.setText(uploadCurrent.getBookDesc());*/
Picasso.with(mContext)
.load(uploadCurrent.getBookImage())
.placeholder(R.drawable.blank)
.fit()
.into(holder.iBookImage);
holder.iBookMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PopupMenu popup = new PopupMenu(mContext, holder.iBookMenu);
popup.inflate(R.menu.cardview_menu);
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.add_my_list:
break;
case R.id.delete_my_list:
deleteBook(mUploads.get(position).getBookId(),position);
break;
}
return false;
}
});
popup.show();
}
});
}
private void deleteBook(String id,int position) {
DatabaseReference dR = FirebaseDatabase.getInstance().getReference("Books").child(id);
dR.removeValue();
// position of item delete in your adapter model
notifyItemRemoved(position);
Toast.makeText(mContext, "Book deleted", Toast.LENGTH_SHORT).show();
}
答案 1 :(得分:0)
您应该使用
@Override
public void onClick(View v) {
notifyDataSetChanged()
}
在项目删除方法中。
答案 2 :(得分:0)
在对同一问题感到无奈之后,我将// function to prepend 0 to a minute if the minute is under 10
Calendar.prototype.displayMinute = function (minute) {
minute = (minute < 10) ? '0' + minute : minute;
return minute;
};
template: "<input type=\"text\" (keyup)='updateMinuteInput($event)' [value]=\"displayMinute(currentMinute)\" >\n",
修改为使用RecyclerViewAdapter
。
这是我的ListAdapter
的样子:
RecyclerViewAdapter
现在,在您的活动中,您可以通过执行以下操作来初始化public class RecyclerViewAdapter extends ListAdapter<YourObjectType, RecyclerViewAdapter.ViewHolder> {
...
public RecyclerViewAdapter() {
super(DIFF_CALLBACK);
...
}
public static final DiffUtil.ItemCallback<YourObjectType> DIFF_CALLBACK = new DiffUtil.ItemCallback<YourObjectType>() {
@Override
public boolean areItemsTheSame(@NonNull YourObjectType oldItem, @NonNull YourObjectType newItem) {
return oldItem.compareTo(newItem)==0; //I added a compareTo method to myObjectClass so that I can compare them.
}
@Override
public boolean areContentsTheSame(@NonNull YourObjectType oldItem, @NonNull YourObjectType newItem) {
return oldItem.compareTo(newItem)==0; //I added a compareTo method to myObjectClass so that I can compare them.
}
};
@NonNull
@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final RecyclerViewAdapter.ViewHolder holder, final int position) {
...
}
class ViewHolder extends RecyclerView.ViewHolder{
...
ViewHolder(View itemView) {
super(itemView);
...
}
}
}
:
RecyclerView
在列表更新时随时呼叫private void initRecyclerView(){
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.invalidate();
adapter = new RecyclerViewAdapter();
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
。
就我而言,我在adapter.submitList(yourList);
回调中调用了submitList
。
我跟随this YouTube tutorial实现了这一目标。