我有一个带有一些任务的RecyclerView。任务有一些TextViews和Buttons。我有一个删除按钮,当我按下它时,我会显示一个确认删除的对话框。如果用户按“删除”,我删除任务并显示带有UNDO按钮的Snackbar。但是当我试图在onBindViewHolder上显示我的Snackbar时,它说“无法解析符号'显示'。无法解决问题。请帮忙。
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {
Item item = items.get(viewHolder.getAdapterPosition());
if (item.isTask()) {
final Task task = (Task)item;
TaskViewHolder taskViewHolder = (TaskViewHolder)viewHolder;
viewBinderHelper.bind((SwipeRevealLayout)taskViewHolder.view, task.getId());
taskViewHolder.title.setText(task.getTitle());
taskViewHolder.time.setText(Utils.getTime(task.getDate()));
taskViewHolder.delete_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Delete this task?");
builder.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int pos = viewHolder.getAdapterPosition();
String key = Utils.getDate(task.getDate());
removeItem(pos);
used_dates.put(key, used_dates.get(key) - 1);
if (!getItem(pos - 1).isTask() && used_dates.get(key) == 0) {
removeItem(pos - 1);
used_dates.put(key, null);
}
}
Snackbar snackbar = Snackbar
.make(layout, "Task is deleted", Snackbar.LENGTH_SHORT)
.setAction("UNDO", new View.OnClickListener() {
@Override
public void onClick(View v) {
currentTaskFragment.addTask(task);
}
});
snackbar.show(); //Cannot resolve symbol 'show'
});
builder.setNegativeButton("Cansel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
viewBinderHelper.closeLayout(task.getId());
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
}
else {
Separator separator = (Separator)item;
SeparatorViewHolder separatorViewHolder = (SeparatorViewHolder)viewHolder;
separatorViewHolder.date.setText(Utils.getDate(separator.getDate()));
}
}
答案 0 :(得分:0)
尝试使用
import android.support.design.widget.Snackbar;
Snackbar
.make(layout, "Task is deleted", Snackbar.LENGTH_SHORT)
.setAction("UNDO", new View.OnClickListener() {
@Override
public void onClick(View v) {
currentTaskFragment.addTask(task);
}
}).show();
或者
您在show
方法之外调用onClick
方法,可以使用onClick
方法移动它。再试一次。