Hello I build simple gridView gallery. In my base adapter I'm struggling with checkboxes. Checkboxes change status only when I click on them but it should changes even if I click on image to which their are assigned. There is some code because I need to check max 2 checkboxes in gallery + I remember checkbox statuses.
I try to do something like this :
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.checkBox.setChecked(!holder.checkBox.isChecked());
}
});
But it won't work, sometimes it checks checkbox but alyways first one + I only saw Toast for checked checkbox but there was no animation for that like nothing happen but status was changed.
Hole adapter looks like (without above code) :
public class GridAdapter extends BaseAdapter {
private Context mContext;
ViewHolder holder;
private ArrayList<File> listFiles;
private ArrayList<File> selectedImages = new ArrayList<>();
private int numberCheckboxes=0;
static boolean[] check;
public GridAdapter(Context context, CheckboxImages checkboxImages, String sala) {
this.listFiles=new ArrayList<>();
this.mContext = context;
ArrayList<File> temp=new ArrayList<>(checkboxImages.getFiles());
for (File f:temp) {
if(checkboxImages.getBeacon(temp.indexOf(f)).equals(sala))
this.listFiles.add(f);
}
this.check=new boolean[listFiles.size()];
for(int i=0;i<listFiles.size();i++) {
check[i]=checkboxImages.getChecked(temp.indexOf(listFiles.get(i)));
}
}
public interface ImageCheckboxInterface{
void setValues(ArrayList<HashMap<File,Boolean>> checkedImages);
}
@Override
public int getCount() {
return listFiles.size();
}
@Override
public Object getItem(int position) {
return listFiles.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public boolean[] getChecked(){
return this.check;
}
public void setListFiles(CheckboxImages newFiles, String sala){
ArrayList<File> tempList=new ArrayList<>();
tempList=newFiles.getFiles();
this.listFiles.clear();
this.check=null;
this.check=new boolean[newFiles.countImagesForHall(sala)];
for (int i=0;i<tempList.size();i++) {
if(newFiles.getBeacon(i).equals(sala)) {
this.listFiles.add(tempList.get(i));
this.check[this.listFiles.size()-1] = newFiles.getChecked(i);
}
}
numberCheckboxes=0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView == null)
{
convertView = LayoutInflater.from(mContext).inflate(R.layout.my_grid, parent, false);
holder=new ViewHolder();
holder.imageView=(ImageView)convertView.findViewById(R.id.imageView);
holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
convertView.setTag(holder);
}
else
holder=(ViewHolder)convertView.getTag();
holder.checkBox.setOnCheckedChangeListener(null);
holder.checkBox.setChecked(check[position]);
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
int counter=0;
for(int i=0;i<check.length;i++)
if(check[i]) counter++;
numberCheckboxes=counter;
if (isChecked && numberCheckboxes<=2)
{
numberCheckboxes++;
check[position] = true;
if(numberCheckboxes<=2)
Toast.makeText(mContext, mContext.getResources().getString(R.string.zaznaczono_zdj), Toast.LENGTH_SHORT).show();
}
else if (!isChecked&&position<check.length)
{
numberCheckboxes--;
check[position]=false;
}
if (numberCheckboxes>=3&&isChecked)
{
buttonView.setChecked(false);
numberCheckboxes--;
check[position]=false;
}
}
});
Glide.with(mContext)
.load(this.listFiles.get(position).getAbsolutePath()) //path to picture
.into(holder.imageView);
return convertView;
}
static class ViewHolder{
ImageView imageView;
CheckBox checkBox;
}
}
Aucun commentaire:
Enregistrer un commentaire