无法弄清楚如何从回收站视图和领域数据库中删除特定项目。我想从回收站视图中删除项目,所以当我单击按钮删除时,它会删除该项目,但无法弄清楚如何从数据库中删除,因为当然当我重新启动应用程序时,会出现相同的日期,因为它不是从数据库中删除。
列表项.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:id="@+id/parentLayout">
<TextView
android:id="@+id/tv_task"
android:text="TASK1"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_alignParentLeft="true"/>
<Button
android:id="@+id/btn_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete"
android:layout_alignParentRight="true"/>
</RelativeLayout>
主要活动:
public class MainActivity extends AppCompatActivity {
private ArrayList<String> mTasks = new ArrayList<>();
EditText et_newtask;
Button btn_add;
Realm realm;
RecyclerView recyclerView;
RecyclerViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//SETUP RV
recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//SETUP REALM
realm = Realm.getDefaultInstance();
RealmHelper helper = new RealmHelper(realm);
mTasks = helper.retrive();
//BIND
adapter = new RecyclerViewAdapter( mTasks,this);
recyclerView.setAdapter(adapter);
initTextViewTasks();
}
private void initTextViewTasks(){
et_newtask = (EditText) findViewById(R.id.et_newTask);
btn_add = (Button) findViewById(R.id.btn_add);
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//GET DATA
Tasks t = new Tasks();
String value = et_newtask.getText().toString();
t.setTasks(value);
//SAVE DATA to DB
RealmHelper helper = new RealmHelper(realm);
helper.save(t);
et_newtask.setText("");
//refresh DB
mTasks = helper.retrive();
adapter = new RecyclerViewAdapter(mTasks,MainActivity.this);
recyclerView.setAdapter(adapter);
}
});
}
}
任务类:
public class Tasks extends RealmObject {
private String tasks;
public String getTasks() {
return tasks;
}
public void setTasks(String tasks) {
this.tasks = tasks;
}
}
适配器:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
private ArrayList<String> mTv_tasks = new ArrayList<>();
private ArrayList<Tasks> dataList = new ArrayList<>();
private Context mContext;
OnClickInterface onClickInterface;
Realm realm;
public RecyclerViewAdapter(ArrayList<String> mTv_tasks,OnClickInterface onClickInterface, Context mContext) {
this.mTv_tasks = mTv_tasks;
this.mContext = mContext;
this.onClickInterface = onClickInterface;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_listitem, viewGroup,false);
ViewHolder viewHolder = new ViewHolder(view,onClickInterface);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int position) {
viewHolder.tv_task.setText(mTv_tasks.get(position));
viewHolder.parentLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, mTv_tasks.get(position), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return mTv_tasks.size();
}
当我单击特定的 btn_delete 时,我想删除回收器视图的特定对象(行)。