在 android 中更新 GridView

Update GridView in android

我在带有复选框的 gridview 中显示多个图像。。我的要求是每当我选中任何复选框时,特定视图应该从 gridview 中删除,我的 gridview 应该使用剩余的图像集进行更新。

例如,如果我检查 gridView 的第二个元素,该特定元素应从 gridview 中完全删除,其余图像应在 gridview 中重新排列。即第 3 个元素应该排在第 2 个位置,第 4 个元素应该排在第 3 个位置。

同样,如果我检查更新后的 gridView 的第 3 个元素,该特定元素应从 gridview 中完全删除,其余图像应在 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;
}

你的逻辑有很多问题。通常,对于网格更新,您应该在 UI 主线程上使用以下内容:

 adapter.notifyDataChanged();

有时还有:

 grid.invalidateViews();

但是你的问题是复选框改变了你没有更新数据源中的数据,它似乎是 imagePath[],因为你将它传递给构造函数并且在 getCount() 方法中使用它来检索计数。

因此,如果您希望网格更新,您应该从此数据源中删除该元素。

如果你想保留所有的imagePath[]数据,再备一个变量:

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);
    }
}

然后像这样从 imagePath 中删除它:

@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();

                    }

也看看这个 link,它正是您需要的:

Android - Remove GridView item from inside getView()?

编辑:

与其使用数组来删除,不如使用列表更好,正如我以前看到的那样:

List<String> newlist = new ArrayList<String>(Arrays.asList(itemPath));          
newlist.remove(imagepath1[position]);           
itemPath = newlist.toArray(new String[newlist.size()]);

此外,检查 imagepath1[position] 不会抛出异常,这样 imagepath1.lenght > position.