How to get the GridView position while its checkbox item is checked?
I have a custom GridView with checkbox
, imageView
and textView
. In the gridViewAdapter.getView()
, i have used rows and holder to initialise the values for checkbox, imageView and textview.
Now when I click on checkbox, I need to fetch the position of that gridView item so I can move the image from it's position to another position in a gridView.
I tried like this:
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.imageTitle = (TextView) row.findViewById(R.id.textView);
holder.image = (ImageView) row.findViewById(R.id.imageView);
holder.cb=(CheckBox)row.findViewById(R.id.checkBox);
holder.cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//here with this checkbox which just got clicked,
// i want to access its gridview position or holder index
// so i can get access to the image
}
});
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
ImageItem item = (ImageItem)dataArraylist.get(position);
holder.imageTitle.setText(item.getTitle());
holder.image.setImageBitmap(item.getImage());
// holder.cb.setChecked(item.isSelected());
return row;
}
public static class ViewHolder {
public TextView imageTitle;
public ImageView image;
public CheckBox cb;
}
UPDATED: ok the parameter "position" of getView function works. public View getView(int position, View convertView, ViewGroup parent){}. But now when I select checkbox in the first view , the position of checkbox is returned correctly. but when i scroll down to next view, the positions of checkbox is random. how to resolve this?
public View getView(int position, View convertView, final ViewGroup parent) {
final int pos=position;
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.image = (ImageView) convertView.findViewById(R.id.imageView);
holder.cb=(CheckBox)convertView.findViewById(R.id.checkBox);
if(flag) {
holder.cb.setVisibility(View.VISIBLE);
holder.cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("MYLOG", "position: "+pos);
ImageItem tempObj;
ImageItem item = clickedArraylist.get(pos);
if(item.isSelected())
item.setSelected(false);
else
item.setSelected(true);
notifyDataSetChanged();
}
});
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
holder.cb.setOnCheckedChangeListener(null);
}
ImageItem item = clickedArraylist.get(position);
holder.image.setImageBitmap(item.getImage());
holder.cb.setChecked(item.isSelected());
return convertView;
}
public static class ViewHolder {
public ImageView image;
public CheckBox cb;
}
The solution is pretty simple. Here's my code which I have implemented in one of my apps.
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (dataTaken.contains(position)) {
checkBox.setChecked(false);
// checkBox.setClickable(false);
final Snackbar snackbar = Snackbar.make(coordinatorLayout, "Another topic already being taken", Snackbar.LENGTH_SHORT);
snackbar.setAction("Ok", new View.OnClickListener() {
@Override
public void onClick(View view) {
snackbar.dismiss();
}
})
.show();
} else {
if (isChecked) {
number.add(position);
} else if (!isChecked) {
for (int m = 0; m < number.size(); m++) {
if (number.get(m).equals(position)) {
number.remove(m);
}
}
}
}
}
});
Once you are done with that you can access the list using this fuction from the adapter class
public ArrayList<Integer> item() {
return number;
}
Now from your Activity class call
variable = adapter.item();
Done!!
A very simple solution would be to set a tag
to the view
and get it when it is clicked, something like below:
holder.cb.setTag(pos);
holder.cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = (int) v.getTag();
ImageItem tempObj;
ImageItem item = clickedArraylist.get(position);
...
});
And do whatever you want with the position then. Please let me know if you have trouble implementing it.
OMG this worked: first answer of the below link ListView in ArrayAdapter order get's mixed up when scrolling
just need to close the if(convertView==null) right after inflating the view and remove the else part of it
链接地址: http://www.djcxy.com/p/91312.html