无法弄清楚如何从回收者视图和领域数据库中删除特定项目。 我想出要从回收商视图中删除项目,所以当我单击按钮删除时,它会删除该项目,但也无法弄清楚如何从日期库中删除,因为当然当我重新启动应用程序时,会出现相同的日期,因为不是从数据库中删除。
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>
MainActivity:
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时,我要删除回收者视图的特定对象(行)。
答案 0 :(得分:0)
我假设您知道如何从Realm中删除项目,但是您不知道如何使用适配器内部的onclicklistener来执行此操作。
1)创建一个接口来进行适配器和活动之间的通信
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
}
}
就这样,真的希望能对您的问题有所帮助