如何从回收者视图和领域数据库中删除特定项目?
How can I delete specific item from recycler view and from realm database?
无法弄清楚如何从回收站视图和领域数据库中删除特定项目。
我想从回收站视图中删除项目,所以当我单击删除按钮时,它会删除该项目,但也无法弄清楚如何从数据库中删除,因为当然当我重新启动应用程序时,会出现相同的日期,因为它不是从数据库中删除。
listitem.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);
}
});
}
}
任务class:
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 时,我想删除回收器视图的特定对象(行)。
我假设您知道如何从 Realm 中删除项目,但您不知道如何使用适配器内的 onclicklistener 执行此操作。
1) 创建一个接口来进行适配器与 activity
之间的通信
public interface OnClickInterface {
void onDeleteClicked(int id); //user id, or whatever you need to know what item you should delete
}
2) 在适配器构造函数中接收您的侦听器
public class YourAdapter ... {
OnClickInterface onClickInterface;
//initialilize in a constructor
public Adapter(OnClickInterface onClickInterface){
this.onClickInterface = onClickInterface
}
}
3) 在 onClick 中调用侦听器以从 RecyclerView 中删除该项目
public void onClick(View v) {
if(v.equals(btn_delete)){
this.onClickInterface.onDeleteClicked(id)
(...) remove from recycler view
}
}
4) 在您的 Activity
上实现 OnCickInterface
public class MainActivity extends AppCompatActivity implements OnClickInterface {
@Override
public void onDeleteClicked(int id){
//delete from realm
}
}
就是这样,真的希望这对您的问题有所帮助
无法弄清楚如何从回收站视图和领域数据库中删除特定项目。 我想从回收站视图中删除项目,所以当我单击删除按钮时,它会删除该项目,但也无法弄清楚如何从数据库中删除,因为当然当我重新启动应用程序时,会出现相同的日期,因为它不是从数据库中删除。
listitem.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);
}
});
}
}
任务class:
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 时,我想删除回收器视图的特定对象(行)。
我假设您知道如何从 Realm 中删除项目,但您不知道如何使用适配器内的 onclicklistener 执行此操作。
1) 创建一个接口来进行适配器与 activity
之间的通信public interface OnClickInterface {
void onDeleteClicked(int id); //user id, or whatever you need to know what item you should delete
}
2) 在适配器构造函数中接收您的侦听器
public class YourAdapter ... {
OnClickInterface onClickInterface;
//initialilize in a constructor
public Adapter(OnClickInterface onClickInterface){
this.onClickInterface = onClickInterface
}
}
3) 在 onClick 中调用侦听器以从 RecyclerView 中删除该项目
public void onClick(View v) {
if(v.equals(btn_delete)){
this.onClickInterface.onDeleteClicked(id)
(...) remove from recycler view
}
}
4) 在您的 Activity
上实现 OnCickInterfacepublic class MainActivity extends AppCompatActivity implements OnClickInterface {
@Override
public void onDeleteClicked(int id){
//delete from realm
}
}
就是这样,真的希望这对您的问题有所帮助