Reputation: 975
I am showing multiple images in gridview with check-box attached to it..My requirement is whenever i will check any check-box that particular view should be removed from the gridview and my gridview should be updated with remaining set of images.
For example If i check on 2nd element of the gridView that particular element should be completely removed from the gridview and remaining images should be rearranged in the gridview. i.e 3rd element should come to 2nd position and 4th element should come to 3rd postion.
Again If i check on 3rd element of the updated gridView that particular element should be completely removed from the gridview and remaining images should be rearranged in the gridview. ExistingDetailedActivity.java
public class ExistingDetailedActivity extends Activity {
public String images,audiopath,name;
TextView ringtonename;
public GridView gridView;
public String [] imgpath;
CustomBaseExistAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_existing_detailed);
Intent i = getIntent();
if(i.hasExtra("BNDL")){
Bundle bdl = getIntent().getBundleExtra("BNDL");
if(bdl.get("IMAGEPATH") != null){
images = bdl.getString("IMAGEPATH");
}
if(bdl.get("AUDIOPATH") != null){
audiopath = bdl.getString("AUDIOPATH");
}
if(bdl.get("RINGTONENAME") != null){
name = bdl.getString("RINGTONENAME");
}
}
imgpath=images.split("\\*") ;
ringtonename=(TextView) findViewById(R.id.textView2);
ringtonename.setText(name);
gridView=(GridView) findViewById(R.id.gridview1);
//CustomExistingListAdapter adapter = new CustomExistingListAdapter(this,imgpath);
//CustomBaseExistAdapter adapter = new CustomBaseExistAdapter(this,imgpath);
adapter = new CustomBaseExistAdapter(this,imgpath);
gridView.setAdapter(adapter);
//adapter.notifyDataSetChanged();
}
}
CustomBaseExistAdapter.java
public class CustomBaseExistAdapter extends BaseAdapter{
private final Activity context;
private final String[] imagepath;
private String[] imagepath1=null;
private String[] imagepath2=null;
private String[] imagepath3=null;
public CustomBaseExistAdapter(Activity context,
String[] imagepath) {
this.context = context;
this.imagepath = imagepath;
List<String> nonBlank = new ArrayList<String>();
for(String s: imagepath) {
if (!s.trim().isEmpty()) {
nonBlank.add(s);
}
}
//nonBlank will have all the elements which contain some characters.
imagepath1 = (String[]) nonBlank.toArray( new String[nonBlank.size()] );
imagepath2 = (String[]) nonBlank.toArray( new String[nonBlank.size()] );
//notifyDataSetChanged();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return imagepath.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
convertView = null;
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.custom_image_with_checkbox, null);
CheckBox cb=(CheckBox) convertView.findViewById(R.id.checkBox1);
final ImageView imageView = (ImageView) convertView.findViewById(R.id.imgThumb);
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
int id = buttonView.getId();
if(isChecked){
List<String> list = new ArrayList<String>(Arrays.asList(imagepath1));
list.remove(imagepath1[position]);
imagepath3 = list.toArray(new String[0]);
//////////////////////////////////
List<String> nonBlank1 = new ArrayList<String>();
for(String s: imagepath3) {
if (!s.trim().isEmpty()) {
nonBlank1.add(s);
}
}
imagepath2=null;
//nonBlank will have all the elements which contain some characters.
imagepath2 = (String[]) nonBlank1.toArray( new String[nonBlank1.size()] );
//imageView.setImageBitmap(BitmapFactory.decodeFile(null));
notifyDataSetChanged();
}
}
});
imageView.setImageBitmap(BitmapFactory.decodeFile(imagepath2[position]));
}
return convertView;
}
Upvotes: 3
Views: 513
Reputation: 338
There are many things wrong with your logic. Usually, for grid update you should use, on the UI Main thread the following:
adapter.notifyDataChanged();
and somethimes also:
grid.invalidateViews();
But your problem is that on checkbox changed you are not updating the data in your data source, which seems to be imagePath[], as you pass it the contructor and also use it in the getCount() method to retrieve the count.
So, you should remove the element from this datasource if you want the grid to update.
If you want to keep all the imagePath[] data, have another variable for backup:
private String[] imagepathBackUp=null;
public CustomBaseExistAdapter(Activity context,
String[] imagepath) {
this.context = context;
this.imagepath = imagepath;
this.imagepathBackUp =imagepath;
List<String> nonBlank = new ArrayList<String>();
for(String s: imagepath) {
if (!s.trim().isEmpty()) {
nonBlank.add(s);
}
}
And then remove it from imagePath like this:
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
int id = buttonView.getId();
if(isChecked){
List<String> list = new ArrayList<String>(Arrays.asList(imagepath1));
list.remove(imagepath1[position]);
imagepath3 = list.toArray(new String[0]);
//////////////////////////////////
List<String> nonBlank1 = new ArrayList<String>();
for(String s: imagepath3) {
if (!s.trim().isEmpty()) {
nonBlank1.add(s);
}
}
imagepath2=null;
//nonBlank will have all the elements which contain some characters.
imagepath2 = (String[]) nonBlank1.toArray( new String[nonBlank1.size()] );
//imageView.setImageBitmap(BitmapFactory.decodeFile(null));
/// REMOVE item from itemPath[], something like this:
String newList[] = new String[itemPath.length - 1];
int count = 0;
for (int i = 0; i < itemPath.length; i++) {
if (itemPath.length - 1 > 0) {
if (itemPath[i] == imagepath1[position]) { // itemPath[1] as the range starts from 0, so 1 would be ITEM2
// SKIP IF MATCHES THE ITEM YO WANT TO REMOVE
} else {
newList[count] = itemPath[i];
count++;
}
}
}
itemPath= newList;
notifyDataSetChanged();
}
Check out this link as well, it is exactly what you need:
Android - Remove GridView item from inside getView()?
EDIT:
Instead of using an array to remove, it might be better to use a list, as I see you previuosly did:
List<String> newlist = new ArrayList<String>(Arrays.asList(itemPath));
newlist.remove(imagepath1[position]);
itemPath = newlist.toArray(new String[newlist.size()]);
Also, check that imagepath1[position] will not throw an exception, so that imagepath1.lenght > position.
Upvotes: 2