jeudi 19 mars 2020

Implement checkbox and do delete operation in gridvidew android studio?

I have a grid view adapter, I am getting images from Arraylist into it. I have a menu button in my activity that enables and disables view of checkboxes. Now I want to delete the selected images but selected images get unselected when scrolled down the grid view. I tried taking help from display check box while pressing a button in android but it is extending adapter made programmatically, I am using base adapter.

what am I doing wrong?

public class GalleryAdapter extends BaseAdapter {

    private Context ctx;
    private ArrayList<Uri> mArrayUri;
    private ArrayList<CheckBox> checkBoxes;
    private ViewHolder viewHolder;
    boolean checked = false;

    GalleryAdapter(Context ctx, ArrayList<Uri> mArrayUri) {
         this.ctx = ctx;
         this.mArrayUri = mArrayUri;
         checkBoxes = new ArrayList<>();
     }

    @Override
    public int getCount() {
         return mArrayUri.size() + 1;
    }

    @Override
    public Object getItem(int position) {
         return mArrayUri.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
    public class ViewHolder{
         CheckBox check;
    }


    public boolean toggleChecks() {
        if (viewHolder.check.getVisibility() == View.INVISIBLE) {
            for (int i=0; i<checkBoxes.size(); i++)
                checkBoxes.get(i).setVisibility(View.VISIBLE);
            return true;
        }
         else {
             for (int i=0; i<checkBoxes.size(); i++)
                 checkBoxes.get(i).setVisibility(View.INVISIBLE);
        }

        notifyDataSetChanged();
        return false;
   }

@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if(convertView == null){
        convertView = inflater.inflate(R.layout.pics, parent, false);
        viewHolder = new ViewHolder();
        viewHolder.check = convertView.findViewById(R.id.chk);
        viewHolder.check.setVisibility(View.INVISIBLE);
        convertView.setTag(viewHolder);
        convertView.setTag(R.id.chk, viewHolder.check);
    }
    else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
    viewHolder.check.setTag(position);
    checkBoxes.add(viewHolder.check);
    viewHolder.check.setChecked(checked);
    ImageView imageView = convertView.findViewById(R.id.picture);

    if(position == mArrayUri.size())
    {
        Glide.with(ctx).load(R.mipmap.add).placeholder(R.mipmap.progress_image).into(imageView);
        //       imageView.setImageResource(R.mipmap.add);
        return  convertView;
    }
    Glide.with(ctx).load(mArrayUri.get(position)).placeholder(R.mipmap.progress_image).into(imageView);
    //   imageView.setImageURI(mArrayUri.get(position));
    return convertView;
}

}




Aucun commentaire:

Enregistrer un commentaire