samedi 1 septembre 2018

How to delete ckeckbox selected item from recyclerview

Delete multiple items from Recyclerview from sqlite database and also from adapter. i have recyclerView data list which is get from sqlite database and now i want to delete recyclerView list by mulltiplt select it with chackbox. select item not deleted when checked item.

here is my sqlite database helper class delete method:

 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;
    }

here is my Adapter for checkbox :

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();
    }
}




Aucun commentaire:

Enregistrer un commentaire