Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 数据已从回收器列表中删除,但未从Sqlite数据库中删除_Android_Sqlite_Android Recyclerview - Fatal编程技术网

Android 数据已从回收器列表中删除,但未从Sqlite数据库中删除

Android 数据已从回收器列表中删除,但未从Sqlite数据库中删除,android,sqlite,android-recyclerview,Android,Sqlite,Android Recyclerview,我正在开发android应用程序I,我正在使用Recycler适配器和Sqlite数据库删除和更新Sqlite数据库 我的问题是,当我在“回收器”视图中单击项目以删除它时,会从列表中删除数据,但当我再次单击“上一步”并打开列表时,删除的数据仍然存在 我已经从设备文件资源管理器中检查了我的数据库,确认数据没有从数据库中删除 更新也会发生同样的事情 这是我的回收器适配器类 public class UserRecyclerAdapterSavedUsers extends RecyclerView.

我正在开发android应用程序I,我正在使用Recycler适配器和Sqlite数据库删除和更新Sqlite数据库

我的问题是,当我在“回收器”视图中单击项目以删除它时,会从列表中删除数据,但当我再次单击“上一步”并打开列表时,删除的数据仍然存在

我已经从设备文件资源管理器中检查了我的数据库,确认数据没有从数据库中删除

更新也会发生同样的事情

这是我的回收器适配器类

public class UserRecyclerAdapterSavedUsers extends RecyclerView.Adapter<UserRecyclerAdapterSavedUsers.UserViewHolder> {

private List<User> listUsers;
Context mContext;
RecyclerView mRecyclerView;
ItemClickListenerLongPressed itemClickListenerLongPressed;
UserRecyclerAdapterSavedUsers userRecyclerAdapterSavedUsers;
View itemView;

public UserRecyclerAdapterSavedUsers(List<User> listUsers,RecyclerView recyclerView) {
    this.listUsers = listUsers;
    mRecyclerView=recyclerView;
}

@Override
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    mContext= parent.getContext();
    itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_user_recycler_second, parent, false);

    return new UserViewHolder(itemView);
}
/**
 * ViewHolder class
 */
public class UserViewHolder extends RecyclerView.ViewHolder {

    //public AppCompatTextView ID;
    public AppCompatTextView textViewID;
    public AppCompatTextView textViewName;
    public AppCompatTextView textViewPassword;
    public AppCompatTextView textViewRole;
    LinearLayout layout;

    public UserViewHolder(View view) {
        super(view);

        textViewID = (AppCompatTextView) view.findViewById(R.id.textViewID);
        textViewName = (AppCompatTextView) view.findViewById(R.id.textViewName);
        textViewPassword = (AppCompatTextView) view.findViewById(R.id.textViewPassword);
        textViewRole = (AppCompatTextView) view.findViewById(R.id.textViewRole);
        layout = (LinearLayout) view.findViewById(R.id.list_view);
    }
}
@Override
public void onBindViewHolder(UserViewHolder holder, final int position) {

    holder.textViewID.setText(listUsers.get(position).getUserid());
    holder.textViewName.setText(listUsers.get(position).getName());
    holder.textViewPassword.setText(listUsers.get(position).getPassword());
    holder.textViewRole.setText(listUsers.get(position).getRole());
    holder.layout.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            displayingAlertDialog(position);
            return false;
        }
    });

}

public void setItemClickListenerLongPressed(ItemClickListenerLongPressed itemClickListenerLongPressed) {
    this.itemClickListenerLongPressed = itemClickListenerLongPressed;
}

@Override
public int getItemCount() {
    Log.v(UsersRecyclerAdapter.class.getSimpleName(),""+listUsers.size());
    return listUsers.size();
}

private void displayingAlertDialog(final int position) {
    final User user= new User();
    //displaying alert dialog box
    AlertDialog.Builder builder = new AlertDialog.Builder(itemView.getContext());
    builder.setTitle("Choose Option");
    builder.setMessage("Update or Delete?");
    builder.setPositiveButton("Update", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

            //go to update activity
            gotupdateuserActivity(user.getUserid());
            // dialog.cancel();

        }
    });
    builder.setNeutralButton("Delete", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            //go to Remove Item
            DatabaseHelper dbHelper = new DatabaseHelper(mContext);
            dbHelper.deletePersonRecord(user.getUserid(), mContext);
            listUsers.remove( position);
            notifyItemRemoved(position);
            mRecyclerView.removeViewAt(position);
            notifyItemRangeChanged(position, listUsers.size());
            notifyDataSetChanged();
            dialog.cancel();

        }
    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    AlertDialog alert11 = builder.create();
    alert11.show();
}
public void remove(int position) {
    listUsers.remove(position);
    notifyItemRemoved(position);
}

private void gotupdateuserActivity(String userid) {
    Intent goToUpdate = new Intent(mContext, UpdateUserRec.class);
    goToUpdate.putExtra("USER_ID", userid);
    Toast.makeText(mContext, "USER REC", Toast.LENGTH_SHORT).show();
    mContext.startActivity(goToUpdate);
}
这是我的回收清单的图片

当我点击一个项目时,它会给我打开一个这样的对话框

当我点击“删除”时,这会显示

之后,当我按下返回键并再次打开删除的数据时,我再次打开 Logcat没有错误

这是我的更新活动

public class UpdateUserRec extends AppCompatActivity {
EditText UserIDUpdate,UserNameUpdate,UserPasswordUpdate,UserRoleUpdate;
Button BtnUserRecUpdate;

DatabaseHelper dbHelper;
String receivedUSERId;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_update_record);
    UserIDUpdate= (EditText) findViewById(R.id.useridupdate);
    UserNameUpdate= (EditText) findViewById(R.id.usernameupdate);
    UserPasswordUpdate= (EditText) findViewById(R.id.userpasswordupdate);
    UserRoleUpdate= (EditText) findViewById(R.id.userroleupdate);
    BtnUserRecUpdate= (Button) findViewById(R.id.userbtnupdate);

    dbHelper = new DatabaseHelper(this);

    try {
        //get intent to get person id
        receivedUSERId= getIntent().getStringExtra("USER_ID");
    } catch (Exception e) {
        e.printStackTrace();
    }
    User user= dbHelper.getUser(receivedUSERId);

    UserIDUpdate.setText(user.getUserid());
    UserNameUpdate.setText(user.getName());
    UserPasswordUpdate.setText(user.getPassword());
    UserRoleUpdate.setText(user.getRole());

    BtnUserRecUpdate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            updateUserFunction();
        }
    });
}

private void updateUserFunction() {
    String useridupdate = UserIDUpdate.getText().toString().trim();
    String usernameupdate = UserNameUpdate.getText().toString().trim();
    String userpasswordupdate = UserRoleUpdate.getText().toString().trim();
    String userroleupdate = UserRoleUpdate.getText().toString().trim();

    if(useridupdate.isEmpty()){
        //error name is empty
        Toast.makeText(this, "Enter User ID", Toast.LENGTH_LONG).show();
    }

    if(usernameupdate.isEmpty()){
        //error name is empty
        Toast.makeText(this, "Enter User Name", Toast.LENGTH_LONG).show();
    }

    if(userpasswordupdate.isEmpty()){
        //error name is empty
        Toast.makeText(this, "Enter the password", Toast.LENGTH_LONG).show();
    }

    if(userroleupdate.isEmpty()){
        //error name is empty
        Toast.makeText(this, "Enter User Role", Toast.LENGTH_LONG).show();
    }

    //create updated person
    User user = new User();

    //call dbhelper update
    dbHelper.updateUser(receivedUSERId, this, user);
    //finally redirect back home
    // NOTE you can implement an sqlite callback then redirect on success delete
    goBackHome();

}

private void goBackHome() {
    startActivity(new Intent(UpdateUserRec.this,UsersListActivity.class));
}

}

您正在从列表中删除数据,该列表在
RecyclerView
中显示数据。您应该记住,数据库和您的列表是不同的实体。数据库是数据的
持久存储
,从中删除项目的列表是
非持久存储
。下次从数据库中获取数据时,该列表将再次填充以前删除的项目

正确的方法是

  • 从数据库中删除该行
  • 从适配器中的列表中删除项
  • 已删除的项目(位置)
  • 您在SQL类中有此方法

    public List<User> getAllUser() { ... }
    

    在删除项目时,使用特定的
    Id
    从适配器调用此方法,然后执行与以前相同的操作。

    尝试使用此方法更改删除功能,看看是否有效

    builder.setNeutralButton("Delete", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //go to Remove Item
                DatabaseHelper dbHelper = new DatabaseHelper(mContext);
                dbHelper.deletePersonRecord(listUsers.get(position).getUserid(), mContext);
                listUsers.remove( position);
                notifyItemRemoved(position);
                mRecyclerView.removeViewAt(position);
                notifyItemRangeChanged(position, listUsers.size());
                notifyDataSetChanged();
                dialog.cancel();
    
            }
        });
    
    编辑:针对您的新问题:关于更新用户

    像这样更改
    setPositiveButton
    功能

    builder.setPositiveButton("Update", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
    
                //go to update activity
                gotupdateuserActivity(listUsers.get(position).getUserid());
                // dialog.cancel();
    
            }
        });
    

    我有在db类中删除用户的方法;我从设备文件管理器中获取数据库文件并进行检查后发现,如果从数据库中删除了一行,则数据不会被删除,那么为什么会再次显示;我有点困惑,问题是行没有从数据库中删除如果您使用的是ORM,那么问题可以通过清除运行时现金来解决。它提供了一个简单的方法来清除它,即daosession.clear()我在db类中有deleteUser的方法作为deletePersonRecord。我建议您在日志中打印查询。并检查在拉取数据库后手动执行我已经添加了我正在使用的查询否我说的是运行时打印它。这样您就可以得到传递给这个函数的实际值了好的,我正在处理Itafter,它会拉取您的数据库并在任何数据库查看器中打开它,然后手动运行该查询。看到结果了吗?@AaliaAli你检查过了吗?是的,我已经检查过了,我很高兴这段代码对我的工作有用Problem@AaliaAli我很高兴我帮助了你现在我想更新数据;当我点击对话框更新按钮时,它会打开一个新的活动;在“更新”按钮中打开的数据始终与我单击任何项目时的数据相同这也是每次打开相同的记录
    public List<User> getAllUser() { ... }
    
    public boolean deleteUser(id){ 
        db.delete(DATABASE_TABLE, KEY_NAME + "=" + id, null)
    }
    
    builder.setNeutralButton("Delete", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //go to Remove Item
                DatabaseHelper dbHelper = new DatabaseHelper(mContext);
                dbHelper.deletePersonRecord(listUsers.get(position).getUserid(), mContext);
                listUsers.remove( position);
                notifyItemRemoved(position);
                mRecyclerView.removeViewAt(position);
                notifyItemRangeChanged(position, listUsers.size());
                notifyDataSetChanged();
                dialog.cancel();
    
            }
        });
    
    builder.setPositiveButton("Update", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
    
                //go to update activity
                gotupdateuserActivity(listUsers.get(position).getUserid());
                // dialog.cancel();
    
            }
        });