在android中更新GridView

时间:2015-03-12 07:36:09

标签: android android-gridview

我在gridview中显示了多个图像,并附带了复选框。multiple images。我的要求是每当我检查任何复选框时,应该从gridview中删除特定视图,我的gridview应该更新剩余的图像集。

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

再次如果我检查更新的gridView的第3个元素,应该从gridview中完全删除特定元素,并且应该在gridview中重新排列剩余的图像。third ![modification][3] 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;
}

1 个答案:

答案 0 :(得分:2)

你的逻辑有很多问题。通常,对于网格更新,您应该在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();

                    }

查看此链接,这正是您所需要的:

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&gt;位置。