gpt4 book ai didi

android - 如何正确删除带有滑动的 CardView? (使用 SQLite 数据库)

转载 作者:行者123 更新时间:2023-12-03 18:14:49 27 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,该应用程序显示一系列卡片,其中包含存储在本地 SQLite 数据库中的一些信息(在主 fragment 中)。

喜欢 this 中的 Matteo问题,我已经实现了一个自定义回收器适配器( GitHub link ),以及一个用于滑动的自定义监听器( SwipeableRecyclerView ),但面临一些不同的问题。当我刷我的 carview 项目时,它会回到它的起始位置,并且可能根本没有删除以前刷过的项目(从与我的回收器适配器一起使用的数据库和回收器 View 中)。

我的数据库助手的代码如下:

public class GeekNotesDatabaseHelper {
static final String DB_NAME = "geeknotes_db";
static final String TABLE = "notes_table";
static final int VERSION = 1;
static final String UID = "_id";
static final String TITLE = "title";
static final String CATEGORY = "category";

private DBHelper dbHelper;
private SQLiteDatabase database;

public GeekNotesDatabaseHelper(Context context) {
dbHelper = new DBHelper(context);
database = dbHelper.getWritableDatabase();
}

public void insertData(String title, String category) {
ContentValues cv = new ContentValues();
cv.put(TITLE, title);
cv.put(CATEGORY, category);
database.insert(TABLE, null, cv);
}

public Cursor getAllData() {
String buildSQL = "SELECT * FROM " + TABLE + " ORDER BY " + UID + " DESC";
return database.rawQuery(buildSQL, null);
}

public void deleteByID(int position) {
database.delete(TABLE, UID + " = " + position, null);
}

private class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, DB_NAME, null, VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE + " (" + UID + " INTEGER " +
"PRIMARY KEY AUTOINCREMENT, " + TITLE + " TEXT, " + CATEGORY + " TEXT);");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE);
onCreate(db);
}
}
}

适配器类:
public class GeekNotesAdapter extends CursorRecyclerAdapter<GeekNotesAdapter.ViewHolder> {
private final Fragment fragment;
private OnItemClickListener itemClickListener;

public GeekNotesAdapter(Cursor cursor, Fragment fragment) {
super(cursor);
this.fragment = fragment;
}

@Override
public void onBindViewHolderCursor(ViewHolder holder, Cursor cursor) {
holder.title.setText(
cursor.getString(cursor.getColumnIndexOrThrow(GeekNotesDatabaseHelper.TITLE)).replaceAll("\\<[^>]*>", ""));

holder.category.setText(
cursor.getString((cursor.getColumnIndexOrThrow(GeekNotesDatabaseHelper.CATEGORY))));

holder.id = cursor.getInt(cursor.getColumnIndexOrThrow(GeekNotesDatabaseHelper.UID));
}

@Override
public GeekNotesAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
final LayoutInflater mInflater = LayoutInflater.from(viewGroup.getContext());
final View sView = mInflater.inflate(R.layout.note_item, viewGroup, false);
return new ViewHolder(sView);
}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

TextView title;
TextView category;
int id;

public ViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.name);
category = (TextView) view.findViewById(R.id.category);
itemView.setOnClickListener(this);
}

@Override
public void onClick(View v) {
if (itemClickListener != null) {
itemClickListener.onItemClick(v, id);
}
}
}

public interface OnItemClickListener {
public void onItemClick(View view , int position);
}

public void setOnItemClickListener(final OnItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
}

还有我的 Fragment 与 cardview-items 的代码:
public class GeekNotesFragment extends Fragment {

GeekNotesDatabaseHelper dbHelper;
GeekNotesAdapter adapter;

@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.main_fragment, container, false);

dbHelper = new GeekNotesDatabaseHelper(getActivity());

Bundle bundle = getArguments();
if (bundle != null) {
// String itemTitle = bundle.getString("tag_item_title");
// String itemCategory = bundle.getString("tag_item_category");
Item item = (Item) bundle.getSerializable("note_item");
dbHelper.insertData(item.getName(), item.getCategory());
}

final RecyclerView recyclerList = (RecyclerView) v.findViewById(R.id.itemsInCards);
recyclerList.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
recyclerList.setLayoutManager(llm);

setTargetFragment(GeekNotesFragment.this, 0);
adapter = new GeekNotesAdapter(dbHelper.getAllData(), getTargetFragment());

recyclerList.setAdapter(adapter);

SwipeableRecyclerViewTouchListener swipeTouchListener =
new SwipeableRecyclerViewTouchListener(recyclerList, new SwipeableRecyclerViewTouchListener.SwipeListener() {
@Override
public boolean canSwipe(int position) {
return true;
}

@Override
public void onDismissedBySwipeLeft(RecyclerView recyclerView, int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {
dbHelper.deleteByID(position);
adapter.notifyItemRemoved(position);
}
adapter.notifyDataSetChanged();
}

@Override
public void onDismissedBySwipeRight(RecyclerView recyclerView, int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {
dbHelper.deleteByID(position);
adapter.notifyItemRemoved(position);
}
adapter.notifyDataSetChanged();
}
});
recyclerList.addOnItemTouchListener(swipeTouchListener);

FloatingActionButton fab = (FloatingActionButton) v.findViewById(R.id.fab);
fab.attachToRecyclerView(recyclerList);
// fab.setBaselineAlignBottom(true);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container, new NewNoteFragment());
ft.addToBackStack(null);
ft.commit();
}
});

return v;
}
}

将项目添加到我的数据库/recyclerview 很好。但是删除——不幸的是,没有。我是开发 Android 应用程序的新手,所以,请告诉我这里可能出了什么问题,哪个问题不允许我正确删除卡片项目以及如何解决?

提前致谢。

最佳答案

嗯,这是一个很老的帖子。但我想回答,因为它可以帮助别人。

答案-尝试使用回收站 View 的方法,例如-

  • public RecyclerView.ViewHolder findViewHolderForAdapterPosition (int position)
  • public RecyclerView.ViewHolder findViewHolderForLayoutPosition (int position)

  • 获取该位置的 View 持有者,然后访问该 View 的值,以在数据库中删除。

    关于android - 如何正确删除带有滑动的 CardView? (使用 SQLite 数据库),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29047722/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com