如何从Recyclerview删除ckeckbox选定的项目

时间:2018-09-01 14:52:02

标签: android sqlite checkbox android-recyclerview

从sqlite数据库和适配器的Recyclerview中删除多个项目。 我有从sqlite数据库获取的recyclerView数据列表,现在我想通过mulltiplt删除recyclerView列表,选择chackbox。选择选中的项目时未删除的项目。

这是我的sqlite数据库助手类删除方法:

 public boolean deleteData(int id){
        try
        {
            int result = db.delete(Constants.TB_NAME,Constants.ROW_ID + " =?",new String[]{String.valueOf(id)});
      if(result>0){}
      return true;
        }catch (SQLException e)
        {
            e.printStackTrace();
        }

        return false;
    }

这是我的适配器复选框:

public class MyAdapter extends RecyclerView.Adapter<MyHolder> {
    Context c;
    ArrayList<CrudData> players;
    DBAdapter  DmDatabase;
    public MyAdapter(Context c, ArrayList<CrudData> players) {
        this.c = c;
        this.players = players;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.model,null);
        MyHolder holder=new MyHolder(v);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyHolder holder, final int position) {
        holder.nametxt.setText(players.get(position).getName());
        holder.posTxt.setText(players.get(position).getPosition());


        holder.check_.setChecked(players.get(position).isSelected());

        holder.check_.setTag(players.get(position));

        holder.check_.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                    int ply = players.get(position).getId();
                    players.remove(position);
                    notifyItemRemoved(position);
                    deletePlanet(position);
            }
        });
    }
    public void deletePlanet(int pos)
    {
        CrudData p=players.get(pos);
        int id=p.getId();

        DBAdapter db=new DBAdapter(c);
        db.openDB();
        if(db.deleteData(id))
        {
            players.remove(pos);
        }else
        {
            Toast.makeText(c,"Unable To Delete",Toast.LENGTH_SHORT).show();
        }

        db.closeDB();

        this.notifyItemRemoved(pos);
    }

    @Override
    public int getItemCount() {
        return players.size();
    }
}

0 个答案:

没有答案