android - 从列表视图中删除一行后刷新片段
问题描述
我设法创建了一个自定义列表视图,并为每一行添加了一个删除按钮。删除按钮运行良好,当我单击该按钮时,该行将从数据库中删除。问题是,当我删除一行时,它会保留在列表中,直到我通过移动到另一个片段来刷新片段并返回,然后它从列表中消失。我想要的是,当我删除一行时,它会立即从列表视图中消失。我尝试了一些解决方案来刷新整个片段或删除完成后的列表视图,但它们不起作用!
这是我为适配器类中的删除按钮编写的代码
public View getView(final int position, View convertView, ViewGroup parent) {
View theView = super.getView(position, convertView, parent); // It took me three days to fix :) I posed a question for it in StackOverFlow
Button deleteButt = (Button) theView.findViewById(R.id.deleteButton);
deleteButt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// This code to delete a row from the class Orders was taken from Back4app help https://www.back4app.com/docs/android/parse-objects/crud
ParseQuery<ParseObject> query = ParseQuery.getQuery("Orders");
query.whereEqualTo("objectId", mainObjectsArray.get(position)[3]); // mainObjectsArray.get(position)[3] We fetched the object id and stored it in mainObjectArray. position is the position of the item in the list which is the same position of the the same item in the mainObjectArray. Smart isn't it?! :)
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(final List<ParseObject> object, ParseException e) {
if (e == null) {
// The dialog
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setMessage("Are you sure you want to delete this order?")
.setNegativeButton("No", null)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Delete based on the position
object.get(0).deleteInBackground(new DeleteCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
Toast.makeText(getActivity(), "Deleted!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(
getApplicationContext(),
e.getMessage().toString(),
Toast.LENGTH_LONG
).show();
}
}
});
}
});
alert.create().show();
} else {
Toast.makeText(
getApplicationContext(),
e.getMessage().toString(),
Toast.LENGTH_LONG
).show();
}
};
});
}
});
return theView;
}
后
Toast.makeText(getActivity(), "Deleted!", Toast.LENGTH_SHORT).show();
我应该编写一个代码来刷新片段,以便从数据库中获取新数据。将寻求任何帮助!多谢。
解决方案
一旦订单被删除,只需从适配器列表中删除订单并通知适配器。
推荐阅读
- php - $mysqli->escape_string - 可以在不指定确切字段名称的情况下使用吗
- java - 在庞大的数据集中计算唯一的 URL(150+ 十亿)
- vue.js - vuejs dynamic input collection with wrong values
- ubuntu - whoami returns user but usermod complains it doesn't exist
- r - Forecasting of a dynamac's package object
- matlab - Built-in MATLAB command to find unknown upper limit that makes an integral equal to zero?
- vim - Can VIM automatically close NERDTree when deleting a buffer?
- powershell - Powershell Invoke-Restmethod 由于 SSL/TLS 安全通道(Smartsheets API)而中止
- r - 计算 data.frame 上的字符位置
- android - 在 Firebase 中将 SMS OTP 身份验证与电子邮件链接验证身份验证相关联