当用户握住RecyclerView
并将其从数据库中删除时,我正在尝试更新片段上的itemview
。
我看到了许多答案,并尝试了一些实现,但均未成功。我已经尝试过notify()
方法。我不确定如何处理这个问题,也不确定我尝试的方法是否可行。该对象确实删除,并且在重新加载片段时,该项目已消失。有人可以帮忙吗?
这是适配器:
public void onBindViewHolder(MyViewHolder holder, @SuppressLint("RecyclerView") final int position) {
final OfflineModule model = assList.get(position);
holder.textView_name.setText(model.getCode());
holder.textView_bio.setText(model.getDesc());
holder.txtIDMod.setText(model.getId());
holder.itemView.setOnLongClickListener((View v) ->{
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setMessage("Delete " + model.getCode() + "?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
RealmHelper helper;
Realm realm;
// initialize realm
Realm.init(mContext);
//SETUP REEALM
RealmConfiguration defaultConfig = new RealmConfiguration.Builder()
.schemaVersion(0)
.build();
realm=Realm.getInstance(defaultConfig);
helper = new RealmHelper(realm);
List<FAssessment> assessmentList =helper.retrieveAF(model.getId());
assert assessmentList != null;
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
for(FAssessment a : assessmentList) {
a.deleteFromRealm();
}
}
});
OfflineModule result = realm.where(OfflineModule.class)
.equalTo("code", model.getCode()).findFirst();
assert result != null;
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
result.deleteFromRealm();
}
});
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
builder.create();
builder.show();
return true;
});
}
活动:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tab_update,
container,
false);
ButterKnife.bind(this, view);
RelativeLayout = contentuserimportMod;
RelativeLayout1 = delResMod;
moduleID = Objects.requireNonNull(getActivity()).getIntent().getStringExtra("id");
moduleCode =Objects.requireNonNull(getActivity()).getIntent().getStringExtra("code");
moduleName =Objects.requireNonNull(getActivity()).getIntent().getStringExtra("desc");
Realm.init(Objects.requireNonNull(getContext()));
RealmConfiguration defaultConfig = new RealmConfiguration.Builder()
.schemaVersion(0)
.build();
realm=Realm.getInstance(defaultConfig);
helper = new RealmHelper(realm);
init();
getAssessList();
return view;
}
private void init(){
linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
AssList.setLayoutManager(linearLayoutManager);
textView1.setText("Assessments - "+moduleName);
List<AssessmentType> list = helper.retrieveAT();
List<String> listString = new ArrayList<>();
for(AssessmentType a : list) {
listString.add(a.getDesc());
}
adapter = new ArrayAdapter<>(Objects.requireNonNull(getContext()), android.R.layout.simple_spinner_dropdown_item, listString);
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);
spinnerOffline.setAdapter(adapter);
}
private void getAssessList(){
RealmHelper helper=new RealmHelper(realm);
FAssessmentList =helper.retrieveAF(moduleID);
AssessTabUpdateAdapter adapter = new AssessTabUpdateAdapter(getContext(), FAssessmentList);
AssList.setAdapter(adapter);
adapter.notifyDataSetChanged();
AssList.setAdapter(adapter);
}
}
我希望删除立即显示,或者只是在删除发生时像在片段上重新加载一样调用,以便从视图中删除已删除的项目。