在RecyclerView中删除和撤消会导致多个条目

时间:2019-04-26 15:27:37

标签: android android-recyclerview

我的import pandas as pd from sklearn.preprocessing import LabelEncoder, OneHotEncoder, StandardScaler df = pd.read_csv("export.csv", sep=",") labelencoder = LabelEncoder() def encoder(x = len(df.columns)): for i in range(x): df.iloc[:, i] = labelencoder.fit_transform(df.iloc[:, i]) encoder() 中有数据,我已将这些数据加载到SQLite Database中。使用ItemTouchHelper,您可以删除一个项目,并且出现RecyclerView且撤消Snackbar

我遇到的问题是,当我多次删除和撤消操作并重新加载应用程序时,我在删除和还原的位置有多个条目。

您可以在此处看到: https://youtu.be/pHSo5EZeSjU

ItemTouchHelper

.action

RecyclerView适配器

    // Delete and undo pocket items
ItemTouchHelper.SimpleCallback recyclerviewSwipe = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
    @Override
    public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
        return false;
    }

    @Override
    public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
        final Pocket pocketList = PocketItems.get(viewHolder.getAdapterPosition());
        final int deletetIndex = viewHolder.getAdapterPosition();

        pocketItemAdapter.removeItem(viewHolder.getAdapterPosition());


        if(pocketItemAdapter.getItemCount() != 0){
            textView_placeholder_recyclerview.setVisibility(View.GONE);
            imageView_placeholder_recyclerview.setVisibility(View.GONE);
        }else{
            textView_placeholder_recyclerview.setVisibility(View.VISIBLE);
            imageView_placeholder_recyclerview.setVisibility(View.VISIBLE);
        }

        Snackbar snackbar = Snackbar.make(coordinatorLayoutPocketMain,R.string.successfully_deleted,3000)
                .setAction(R.string.undo, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        pocketItemAdapter.restoreItem(pocketList,deletetIndex);
                        database.createpocketEntry(pocketList.getTIMESTAMP(),pocketList.getAMOUNT(),pocketList.getDESCRIPTION(),pocketList.getCATEGORY());
                    }
                });
        snackbar.getView().setBackgroundColor(ContextCompat.getColor(PocketMain.this,R.color.colorBright));

        snackbar.show();
    }

};
new ItemTouchHelper(recyclerviewSwipe).attachToRecyclerView(recyclerViewPocketItems);

SQLite打开助手:

void removeItem(int position){
    Database database = new Database(context);
    database.deletepocketEntry(Integer.parseInt(pocketItems.get(position).getROWID()));
    pocketItems.remove(position);
    notifyItemRemoved(position);


}

void restoreItem(Pocket item, int position){
    pocketItems.add(position,item);
    notifyItemInserted(position);
    Log.d("RESTORED","restoreItem()");
}

当我登录删除时,恢复一切正常:

  Integer deletepocketEntry(Integer rowID){
        SQLiteDatabase database = this.getWritableDatabase();
        Log.d("RESTORED","deletePocketEntry");
        return database.delete(KEY_DATABASE_TABLE,"rowid = ?", new String[]{Integer.toString(rowID)});
 }

long createpocketEntry(String timestamp,String amount,String description, String category){
    SQLiteDatabase database = this.getWritableDatabase();
    ContentValues pocketValues = new ContentValues();
    pocketValues.put(KEY_TIMESTAMP,timestamp);
    pocketValues.put(KEY_AMOUNT,amount);
    pocketValues.put(KEY_DESCRIPTION,description);
    pocketValues.put(KEY_CATEGORY,category);
    Log.d("RESTORED","createPocketEntry");
    return database.insert(KEY_DATABASE_TABLE,null,pocketValues);
}

------------------ EDIT -------------------- < / p>

您可以在这里https://www.youtube.com/watch?v=FjWIpXgH5ig看到它 所以基本上我改变了一些东西。 应该返回1,但不是,我认为这是问题所在。

2019-04-26 16:17:57.034 15457-15457/de.corestudio.coinmaster D/RESTORED: deletePocketEntry
2019-04-26 16:17:58.504 15457-15457/de.corestudio.coinmaster D/RESTORED: restoreItem()
2019-04-26 16:17:58.504 15457-15457/de.corestudio.coinmaster D/RESTORED: createPocketEntry
2019-04-26 16:18:00.825 15457-15457/de.corestudio.coinmaster D/RESTORED: deletePocketEntry
2019-04-26 16:18:01.878 15457-15457/de.corestudio.coinmaster D/RESTORED: restoreItem()
2019-04-26 16:18:01.878 15457-15457/de.corestudio.coinmaster D/RESTORED: createPocketEntry

而del​​etePocketEntry是:

int removeItem(int position){
    Database database = new Database(context);
    int boo = database.deletepocketEntry(Integer.parseInt(pocketItems.get(position).getROWID()));
    pocketItems.remove(position);
    notifyItemRemoved(position);
    return boo;
}

所以每次红色小吃栏出现都是因为它,因为它返回0而不是1:

Integer deletepocketEntry(Integer rowID){
    SQLiteDatabase database = this.getWritableDatabase();
    return database.delete(KEY_DATABASE_TABLE,"rowid = ?", new String[]{Integer.toString(rowID)});
}

0 个答案:

没有答案