Android : ImageView with checkbox in grid-view - android

I am trying to delete images with Checkbook.
I have done to delete the selected multiple images which is checked from grid-view on button click event. But the check-boxes are appearing as they are in the grid-view .
How can I remove the check-boxes also with images?
Thanks in advanced.
Here is my check-box code in getView() method in Android
class GridView_Adapter extends BaseAdapter {
private LayoutInflater mInflater;
SparseBooleanArray mSparseBooleanArray;
public GridView_Adapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mSparseBooleanArray = new SparseBooleanArray();
}
public ArrayList<String> getCheckedItems() {
ArrayList<String> mTempArry = new ArrayList<String>();
for(int i=0;i<fileName.size();i++) {
if(mSparseBooleanArray.get(i)) {
mTempArry.add(fileName.get(i));
}}
return mTempArry;
}
public int getCount() {
return fileName.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if (convertView == null)
{
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.add_post_grid_item_layout, null);
holder.image = (ImageView) convertView.findViewById(R.id.image);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
Bitmap myBitmap = BitmapFactory.decodeFile(fileName.get(position));
holder.image.setImageBitmap(myBitmap);
final int pos = position;
holder.checkbox.setId(position);
holder.checkbox.setTag(position);
holder.checkbox.setChecked(mSparseBooleanArray.get(position));
holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
mSparseBooleanArray.put((Integer) buttonView.getTag(), isChecked);
int pos = (Integer) buttonView.getTag();
if (!buttonView.isChecked())
{
picsName.remove((String) fileName.get(pos));
}
else if(buttonView.isChecked())
{
if (!picsName.contains((String) fileName.get(pos)))
{
picsName.add((String) fileName.get(pos));
}
}
notifyDataSetChanged();
}
});
return convertView;
}
class ViewHolder
{
ImageView image;
CheckBox checkbox;
int id;
}
}

Related

listview is not refreshing values after deleting row with checkbox value = true

Good day,
I'm doing a listview, baseadapter with checkbox that is able to delete multiple selected rows.
here is my onCreate :
ArrayList<Memos> list;
list = new ArrayList<Memos>();
list.add(new Memos(1, "s", "s"));
list.add(new Memos(2, "x", "aaa"));
list.add(new Memos(3, "v", "aesf"));
final ListView lv = (ListView) findViewById(R.id.myList);
lv.setAdapter(new MemoListAdapter(list, this));
deletebutton
#Override
public void onClick(View v) {
MemoListAdapter myAdapter = (MemoListAdapter)lv.getAdapter();
myAdapter.remove();
}
then here is my complete baseadapter :
public class MemoListAdapter extends BaseAdapter {
private List<Memos> listComment;
private Context context;
private LayoutInflater inflater = null;
private ArrayList<Memos> deleteMemos;
public MemoListAdapter(List<Memos> listComment, Context context) {
super();
this.listComment = listComment;
this.context = context;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
deleteMemos = new ArrayList<Memos>();
}
#Override
public int getCount() {
return listComment.size();
}
#Override
public Memos getItem(int position) {
return listComment.get(position);
}
#Override
public long getItemId(int arg0) {
return 0;
}
#Override
public int getViewTypeCount() {
return getCount();
}
#Override
public int getItemViewType(int position) {
return position;
}
public class ViewHolder
{
TextView body;
TextView date;
CheckBox checkBox;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_view, null);
holder = new ViewHolder();
holder.body = (TextView) convertView.findViewById(R.id.big_text);
holder.date = (TextView) convertView.findViewById(R.id.small_text);
holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
holder.body.setText(listComment.get(position).getMessageBody());
holder.date.setText(listComment.get(position).getMessageDate());
holder.checkBox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//is chkIos checked?
if(listComment.get(position).isCheckbox()) {
holder.checkBox.setChecked(false);
deleteMemos.remove(listComment.get(position));
}
else {
// Do invisible or gone stuff here
holder.checkBox.setChecked(true);
deleteMemos.add(listComment.get(position));
}
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
public void remove() {
for(Memos memo : deleteMemos) {
listComment.remove(memo);
}
this.notifyDataSetChanged();
}
}
after selecting rows to delete (4 rows)
the list adjusted but the contents and the check value of the checkbox is still the same.
I am wondering what part of the notifyDataSetChanged is wrong.
Thanks in advance!
Your views are reused that's why it happen . Try adding below codes into your getView()
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_view, null);
holder = new ViewHolder();
holder.body = (TextView) convertView.findViewById(R.id.big_text);
holder.date = (TextView) convertView.findViewById(R.id.small_text);
holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(listComment.get(position).isCheckbox()) {
holder.checkBox.setChecked(true);
}else {
holder.checkBox.setChecked(false);
}
holder.body.setText(listComment.get(position).getMessageBody());
holder.date.setText(listComment.get(position).getMessageDate());
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
listComment.get(position).setIsCheckbox(isChecked);
if (isChecked){
deleteMemos.add(listComment.get(position));
}else{
deleteMemos.remove(listComment.get(position));
}
}
});
return convertView;
}

listView checkbox scrolling bug

I have a problem with a listView consisting of check-boxes. When I scroll some items get selected automatically.
This is my code for the getView method in my custom adapter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
convertView = inflater.inflate(R.layout.prerequisite_course, parent, false);
holder = new ViewHolder();
holder.name = (CheckBox) convertView.findViewById(R.id.course_checkbox);
convertView.setTag(holder);
holder.name.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
Course course = (Course) cb.getTag();
if (cb.isChecked())
checkBoxChecked(cb, course);
else
checkBoxNotChecked(cb, course);
}
});
}
else {
holder = (ViewHolder) convertView.getTag();
}
Course course = coursesDataManager.getCourseInPosition(position);
String currentId = COURSES_SUBJECT + " " + course.getId();
holder.name.setText(currentId);
holder.name.setTag(course);
// for the clear button:
if (!holder.name.isChecked()) {
checkBoxNotChecked(holder.name, (Course) holder.name.getTag());
}
return convertView;
}
private void checkBoxChecked(CheckBox cb, Course add) {
cb.setBackgroundColor(Color.rgb(1, 67, 121));
cb.setTextColor(Color.WHITE);
myCoursesManager.addPrerequisite(add);
}
private void checkBoxNotChecked(CheckBox cb, Course remove) {
cb.setBackgroundColor(Color.WHITE);
cb.setTextColor(Color.BLACK);
myCoursesManager.removePrerequisite(remove);
}
Also, criticism of the code would be welcomed as I'm new to android development. Thank you.
You need to maintain boolean array for checkbox,try this way this worked for me
public class CustomAdapter extends BaseAdapter {
private final LayoutInflater inflater;
private final Context context;
private List<ModelPooja> listData;
public CustomAdapter(Context mainActivity, List<ModelPooja> listData) {
context = mainActivity;
this.listData = listData;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return listData.size();
}
#Override
public Object getItem(int position) {
return listData.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_item_poojaselection, null);
holder.tv = (TextView) convertView.findViewById(R.id.list_item_poojaname);
holder.checks = (CheckBox) convertView.findViewById(R.id.list_item_poojacheck);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.checks.setOnCheckedChangeListener(null);
holder.checks.setFocusable(false);
if (listData.get(position).isselected) {
holder.checks.setChecked(true);
} else {
holder.checks.setChecked(false);
}
holder.checks.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton cb, boolean b) {
if (checkMaxLimit()) {
if (listData.get(position).isselected && b) {
holder.checks.setChecked(false);
listData.get(position).isselected = false;
} else {
holder.checks.setChecked(false);
listData.get(position).isselected = false;
Toast.makeText(context, "Max limit reached", Toast.LENGTH_SHORT).show();
}
} else {
if (b) {
listData.get(position).isselected = true;
} else {
listData.get(position).isselected = false;
}
}
}
});
holder.tv.setText(listData.get(position).getPOOJA_LISTING_NAME());
return convertView;
}
public boolean checkMaxLimit() {
int countermax = 0;
for(ModelPooja item : listData){
if(item.isselected){
countermax++;
}
}
return countermax >= 5;
}
public class ViewHolder {
TextView tv;
public CheckBox checks;
}
}
Create a Model Class for saving the checkBox status for the list items.
public class ModelClass
{
int position=null;
String item=null;
boolean selected=false;
public ModelClass(int position,String item, boolean selected) {
super();
this.position=position;
this.item=item;
this.selected = selected;
}
}
Then in your Adapter Class
private ArrayList<ModelClass> arl;
Inside your getView()
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
arl.get(position).selected=isChecked;
}
});
holder.checkBox.setChecked(arl.get(position).selected);
And now no matter how much you scroll your checkBoxes state will be consistent(Trust me, I have used this code in my Project).

Checkbox showing multiple time in grid view holder Pattern

i am showing and hiding checkbox on imageview click in gridview but when i click on one imageView checkbox appear on multiple image view android and as i scroll visible checkbox goes missing when i scroll
here is my Adapter
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ImageAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return arrPath.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(
R.layout.galleryitems, parent,false);
holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);
convertView.setTag(R.id.itemCheckBox, holder.checkbox);
convertView.setTag(R.id.thumbImage,holder.imageview);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
holder.checkbox = (CheckBox)convertView.getTag();
holder.imageview = (ImageView)convertView.getTag();
}
holder.checkbox.setId(position);
holder.imageview.setId(position);
holder.checkbox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
CheckBox cb = (CheckBox) v;
int id = cb.getId();
if (thumbnailsselection[id]) {
cb.setChecked(false);
thumbnailsselection[id] = false;
} else {
cb.setChecked(true);
thumbnailsselection[id] = true;
}
}
});
holder.imageview.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
ImageView iv = (ImageView )v;
int id = iv.getId();
if (holder.checkbox.getVisibility() == View.GONE) {
holder.checkbox.getTag();
holder.imageview.getTag();
holder.imageview.setAlpha(0.22f);
holder.checkbox.setVisibility(View.VISIBLE);
holder.checkbox.setChecked(true);
} else if (holder.checkbox.getVisibility() == View.VISIBLE) {
holder.imageview.setAlpha(0.8f);
holder.checkbox.setVisibility(View.GONE);
}
}
});
holder.checkbox.setTag(position);
holder.imageview.setImageBitmap(thumbnails[position]);
holder.checkbox.setChecked(thumbnailsselection[position]);
holder.id = position;
return convertView;
}
}
class ViewHolder {
ImageView imageview;
CheckBox checkbox;
int id;
}
}
try this code it worked for me,
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<Items> list
public ImageAdapter(ArrayList<Items> list) {
this.list=list;
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return list.size();
}
public Items getItem(int position) {
return list.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(
R.layout.galleryitems, parent,false);
holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
//Take image view in your XML
holder.checkbox = (ImageView) convertView.findViewById(R.id.itemCheckBox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(list.get(position).isImageSelected()){
// set your check image
holder.checkbox.setImageResource(checkResId);
}else{
// set your Uncheck image
holder.checkbox.setImageResource(unCheckResId);
}
holder.checkbox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(list.get(position).isImageSelected()){
// set your Uncheck image
holder.checkbox.setImageResource(unCheckResId);
list.get(position).setIsImageSelected(false);
} else {
// set your check image
holder.checkbox.setImageResource(checkResId);
list.get(position).setIsImageSelected(false);
}
notifyDataSetChanged();
}
});
return convertView;
}
}
class ViewHolder {
ImageView imageview;
// Use ImageView for select and deselect
ImageView checkbox;
int id;
}
}
public class Items {
String thumbImageUrl;
boolean isImageSelected;
public String getThumbImageUrl() {
return thumbImageUrl;
}
public void setThumbImageUrl(String thumbImageUrl) {
this.thumbImageUrl = thumbImageUrl;
}
public boolean isImageSelected() {
return isImageSelected;
}
public void setIsImageSelected(boolean isImageSelected) {
this.isImageSelected = isImageSelected;
}
}
that's not how you use ViewHolder dude.
ViewHolder will also be reused like convertView as you set them as the tag.
It was supposed to link the UI, so that you don't need to call findViewById again.
But is your responsibility to maintain the data( the position of ImageView you selected in your case)
see this as an example.

How to use single selection checkbox in android?

public class ImageAdapter extends BaseAdapter
{
private LayoutInflater mInflater;
private Context mContext;
public ImageAdapter(Context context)
{
mContext = context;
}
public int getCount()
{
return count;
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(final int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if (convertView == null)
{
holder = new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.state_info, null);
holder.textview = (TextView) convertView.findViewById(R.id.thumbImage);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.checkbox.setId(position);
holder.textview.setId(position);
holder.checkbox.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
// String h="hello";
CheckBox cb = (CheckBox) v;
int id = cb.getId();
if(cb.isChecked())
{
cb.setChecked(false);
}
Toast.makeText(Four.this, "Selected CheckBox ID" + v.getId(), Toast.LENGTH_SHORT).show();
}
});
holder.textview.setText(items.get(position).getName());
holder.checkbox.setChecked(thumbnailsselection[position]);
holder.id = position;
return convertView;
}
public void clear()
{
CheckBox cb=(CheckBox)findViewById(R.id.itemCheckBox);
// int id=cb.getId();
for(int j=0;j<count;j++)
{
cb.setChecked(false);
}
}
}
class ViewHolder
{
TextView textview , textIndexId;
CheckBox checkbox;
int id;
}
You can do one thing rather use checkbox you can use radon button but just change the style to check box
public class ImageAdapter extends BaseAdapter
{
private LayoutInflater mInflater;
private Context mContext;
public ImageAdapter(Context context)
{
mContext = context;
}
public int getCount()
{
return count;
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(final int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if (convertView == null)
{
convertView = LayoutInflater.from(mContext).inflate(R.layout.state_info, null);
holder = new ViewHolder();
holder.textview = (TextView) convertView.findViewById(R.id.thumbImage);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
convertView.setTag(holder);
//holder.checkbox.setId(position);
holder.textview.setId(position);
holder.checkbox.setId(position);
holder.checkbox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
for (int i = 0; i < count; i++)
{
if (v.getId() == i)
{
thumbnailsselection[i] = true;
Log.v("check", ""+position);
}
else
{
thumbnailsselection[i] = false;
}
}
notifyDataSetChanged();
}
});
if (thumbnailsselection[position])
{
holder.checkbox.setChecked(true);
}
else
{
holder.checkbox.setChecked(false);
}
holder.textview.setText(items.get(position).getName());
holder.checkbox.setChecked(thumbnailsselection[position]);
holder.id = position;
return convertView;
}
}

Checkbox in Listview showing unexpected behaviour

Hi i am using checkbox in listview, but the checkbox get auto unchecked or checked when i scroll my list view.
public class MyContactsAdapter extends BaseAdapter {
private LayoutInflater inflater;
private List<Friend> mAllFriends;
public MyContactsAdapter(Context context,List<Friend> allFriends) {
inflater = LayoutInflater.from(context);
this.mAllFriends =allFriends;
}
#Override
public int getCount() {
if(mAllFriends==null)
{
return 0;
}else{
return mAllFriends.size();
}
}
#Override
public Object getItem(int position) {
return mAllFriends.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.contact_list_item, null);
mHolder = new ViewHolder();
mHolder.mProFileImage = (ImageView) convertView
.findViewById(R.id.profleImage);
mHolder.mPersonName = (TextView) convertView
.findViewById(R.id.person_Name);
mHolder.mEmailId = (TextView) convertView
.findViewById(R.id.person_emailId);
mHolder.mSelectPerson = (CheckBox)convertView.findViewById(R.id.personSelect);
mHolder.mSelectAllLabel = (TextView)convertView.findViewById(R.id.personSelectAllLabel);
convertView.setTag(mHolder);
} else {
mHolder = (ViewHolder) convertView.getTag();
}
if(position==0)
{
mHolder.mProFileImage.setVisibility(View.GONE);
mHolder.mPersonName.setVisibility(View.GONE);
mHolder.mEmailId.setVisibility(View.GONE);
mHolder.mSelectAllLabel.setVisibility(View.VISIBLE);
}else{
mHolder.mProFileImage.setVisibility(View.VISIBLE);
mHolder.mPersonName.setVisibility(View.VISIBLE);
mHolder.mEmailId.setVisibility(View.VISIBLE);
mHolder.mSelectAllLabel.setVisibility(View.GONE);
}
mHolder.mProFileImage.setImageResource(R.drawable.ic_launcher);
mHolder.mPersonName.setText(""+mAllFriends.get(position).getName());
mHolder.mEmailId.setText(""+mAllFriends.get(position).getEmail());
mHolder.mSelectPerson.setTag(position);
mHolder.mSelectPerson.setChecked(mAllFriends.get(position).isSelected());
mHolder.mSelectPerson.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mAllFriends.get((Integer) buttonView.getTag()).setSelected(isChecked);
}
});
return convertView;
}
private static class ViewHolder {
ImageView mProFileImage;
TextView mPersonName;
TextView mEmailId;
CheckBox mSelectPerson;
TextView mSelectAllLabel;
}
}

Categories

Resources