我尝试开发一个应用程序(笔记本)但删除并编辑按钮,因为每个帖子都不起作用,如果我点击了删除按钮的第4号帖子,每个帖子,帖子号码0将删除,同样适用于编辑。
如何获取position
中每个帖子的RecyclerView
,以便删除和修改功能
代码:
public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.NoteViewHolder> {
public static final int REQUEST_FOR_ACTIVITY_CODE = 102;
public static int itemPosition;
public static String state = "";
private Context context;
private List<Note> notes = new ArrayList<>();
Note note = new Note();
public NoteAdapter(Context context) {
this.context = context;
}
@NonNull
@Override
public NoteViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new NoteViewHolder(LayoutInflater.from(context).inflate(R.layout.layout_item_note, parent, false));
}
@Override
public void onBindViewHolder(@NonNull final NoteViewHolder holder, int position) {
holder.bindNote(notes.get(position));
holder.editButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
state = "edit";
Intent editintent = new Intent(context, NoteActivity.class);
editintent.putExtra("title", holder.textViewTitle.getText().toString());
editintent.putExtra("content", holder.textViewContent.getText().toString());
// context.startActivity(editintent);
((Activity) context).startActivityForResult(editintent, REQUEST_FOR_ACTIVITY_CODE);
}
});
holder.removeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
removeNote(itemPosition);
}
});
}
@Override
public int getItemCount() {
return notes.size();
}
public class NoteViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView textViewTitle;
public TextView textViewContent;
public ImageButton removeButton;
public ImageButton editButton;
public NoteViewHolder(View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.txt_title);
textViewContent = itemView.findViewById(R.id.txt_note);
removeButton = itemView.findViewById(R.id.btn_remove);
editButton = itemView.findViewById(R.id.btn_edit);
}
public void bindNote(Note note) {
textViewTitle.setText(note.getTitle());
textViewContent.setText(note.getContent());
}
@Override
public void onClick(View v) {
itemPosition = note.getId();
}
}
public void setNote(Note note) {
notes.add(note);
notifyItemInserted(notes.size() - 1);
}
public void setEditNote(Note note) {
notes.set(itemPosition, note);
notifyDataSetChanged();
}
public void removeNote(int position) {
notes.remove(position);
notifyItemRemoved(position);
}
public void clearAll() {
notes = new ArrayList<>();
notifyDataSetChanged();
}
}
答案 0 :(得分:0)
将您的点击监听器设置为NoteViewHolder,而不是NoteAdapter,然后在viewHolder调用中找到您的项目位置getAdapterPosition()
您的方法可以通过这种方式实现:
首先,创建界面NoteEditListener
interface NoteEditListener {
void edit(int position);
}
第二次,从NoteAdapter
实施此界面并覆盖edit
方法
public class NoteAdapter implements NoteEditListener
public void edit(int position){
// todo edit your item in position
}
第三次,将NoteEditListener
添加到viewHolder构造函数并从适配器传递this
public class NoteViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView textViewTitle;
public TextView textViewContent;
public ImageButton removeButton;
public ImageButton editButton;
public NoteViewHolder(View itemView, final NoteEditListener listener) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.txt_title);
textViewContent = itemView.findViewById(R.id.txt_note);
removeButton = itemView.findViewById(R.id.btn_remove);
editButton = itemView.findViewById(R.id.btn_edit);
editButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.edit(getAdapterPosition());
}
}
}