如何从回收器视图和领域数据库中删除特定项目



无法弄清楚如何从回收器视图和领域数据库中删除特定项目。我想出了从回收器视图中删除项目的方法,所以当我单击按钮删除时,它会删除该项目,但无法弄清楚如何从日期库中删除,当然,当我重新启动应用程序时,相同的日期出现,因为它没有从数据库中删除。

列表项.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时,我想删除回收器视图的特定对象(行(。

我假设您知道如何从 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 中的侦听器以从回收器视图中删除该项目

public void onClick(View v) { 
     if(v.equals(btn_delete)){ 
        this.onClickInterface.onDeleteClicked(id)
        (...) remove from recycler view
     }
}

4(在您的活动中实现OnCickInterface

public class MainActivity extends AppCompatActivity implements OnClickInterface {
    @Override
    public void onDeleteClicked(int id){
       //delete from realm
    }
}

就是这样,真的希望这对您的问题有所帮助

相关内容

  • 没有找到相关文章

最新更新