我遇到了實施刪除按鈕的問題。它應該從RecyclerView中刪除一個項目,如果你點擊它。我的佈局文件包含兩個TextViews和ImageButton的,像這樣:爲什麼我的刪除按鈕在RecyclerView中不起作用?
<TextView
android:id="@+id/layout_list_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/layout_list_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/layout_list_name"
android:layout_alignLeft="@+id/layout_list_name"
android:layout_below="@+id/layout_list_name" />
<ImageButton
android:id="@+id/layout_list_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:contentDescription="@string/layout_list_delete" />
這是管理的RecyclerView的項目和功能,我的適配器類:
List<RecyclerViewListSchema> list;
public RecyclerViewListAdapter(List<RecyclerViewListSchema> list) {
this.list = list;
}
public static class RecyclerViewViewHolder extends RecyclerView.ViewHolder {
TextView name;
TextView date;
ImageButton delete;
public RecyclerViewViewHolder(View view) {
super(view);
name = (TextView) view.findViewById(R.id.layout_list_name);
date = (TextView) view.findViewById(R.id.layout_list_date);
delete = (ImageButton) view.findViewById(R.id.layout_list_delete);
}
}
@Override
public RecyclerViewViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_list, viewGroup, false);
RecyclerViewViewHolder viewHolder = new RecyclerViewViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerViewViewHolder viewHolder, final int position) {
viewHolder.name.setText(list.get(position).name);
viewHolder.date.setText(list.get(position).date);
viewHolder.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
RecyclerViewListSchema item = list.get(position);
list.remove(position);
notifyItemRemoved(position);
}
});
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public int getItemCount() {
return list.size();
}
更新: 這是確切的位置在我的刪除功能的工作原理刪除項目:
@Override
public void onBindViewHolder(RecyclerViewViewHolder viewHolder, final int position) {
viewHolder.name.setText(list.get(position).name);
viewHolder.date.setText(list.get(position).date);
viewHolder.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
RecyclerViewListSchema item = list.get(position);
list.remove(position);
notifyItemRemoved(position);
}
});
}
我RecyclerView填充一個從SQLite數據庫中選擇它旁邊的按鈕。我嘗試點擊它,但它沒有做任何事情。我的代碼中發生了什麼?我做錯了什麼?有關如何解決此問題的任何建議?
也表明你在哪裏點擊刪除按鈕 – AADProgramming
你更新你的SQLite數據庫的代碼?您應該從數據庫中刪除項目。 – Amir
我已經整理出我的問題。顯然,我上面提供的代碼實際上工作。出於某種原因,當我一直在手機上運行我的應用程序時,該應用程序未更新,因此我必須將其卸載並重新安裝。感謝大家的幫助! – GWX