Android 如何更改GridVIew的图像

Android 如何更改GridVIew的图像,android,image,gridview,Android,Image,Gridview,我有一个GridView,其中包含一些图像。我想通过单击来更改图像。我给出了从对话框中选择图像的选项。但问题是gridview并没有更新最新的图像。我发现了类似的问题。但即使是解决方案也不适用于我的情况。我的代码如下 private GridView gridView1,gridView2,gridView3; private View sepView1,sepView2; private static WallpaperInfo wall = new WallpaperInfo(); pub

我有一个GridView,其中包含一些图像。我想通过单击来更改图像。我给出了从对话框中选择图像的选项。但问题是gridview并没有更新最新的图像。我发现了类似的问题。但即使是解决方案也不适用于我的情况。我的代码如下

private GridView gridView1,gridView2,gridView3;
private View sepView1,sepView2;
private static WallpaperInfo wall = new WallpaperInfo();

public Integer[] butterflyIds = {
        R.drawable.create_cardz, 
        R.drawable.done_icon,
        R.drawable.email, 
        R.drawable.facebook,
        R.drawable.error_icon, 
        R.drawable.mms_lock,
        R.drawable.ic_launcher,
        R.drawable.icon_plus,
        R.drawable.like, 

};


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    Log.i("BillingService", "Starting");
    setContentView(R.layout.wallpapersetting);

    gridView1 = (GridView) findViewById(R.id.gridview1);
    gridView2 = (GridView) findViewById(R.id.gridview2);
    gridView3 = (GridView) findViewById(R.id.gridview3);

    sepView1 = (View)findViewById(R.id.seperator1);
    sepView2 = (View)findViewById(R.id.seperator2);

    gridView1.setAdapter(new ImageAdapter(this, butterflyIds));
    final ImageAdapter im = new ImageAdapter(this,butterflyIds);
    //gridView2.setAdapter(new ImageAdapter(this, butterflyIds));
    //gridView3.setAdapter(new ImageAdapter(this,background));


    gridView1.setOnItemClickListener(new OnItemClickListener() 
    {

        @Override
        public void onItemClick(AdapterView<?> parentView, View vi, int position,long id) 
        {
            createDialog(parentView,vi,position,butterflyIds,im,gridView1);

        }

    });


}

 private  void createDialog(final AdapterView<?>  parentView,View v,int pos,Integer[]     ID, final ImageAdapter im, final GridView gv)
{
    final int parentPos= pos;
    final Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.dialog_layout);
    GridView dialogGridView =  (GridView) dialog.findViewById(R.id.dialog_grid);
    dialogGridView.setAdapter(new DialogImageAdapter(this,ID));
    dialogGridView.setOnItemClickListener(new OnItemClickListener() 
    {

        @Override
        public void onItemClick(AdapterView<?> dialogParentView, View vi, int position,long id) 
        {
            //parentView.addView(vi, position);
            setImage(dialogParentView.getId(),parentPos,im);
            dialog.dismiss();
            im.notifyDataSetChanged();
            gv.setAdapter(im);
            gv.invalidateViews();

        }

    });
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(getWindow().getAttributes());
    lp.width = WindowManager.LayoutParams.FILL_PARENT;
    getWindow().setAttributes(lp);
    dialog.show();  
}

protected void setImage(int viewId,int parentPos, ImageAdapter im) 
{
    im.setFlowerIds(parentPos, viewId);
}

为什么每次有人点击一个项目时,你都要创建一个新的
GridView


无论哪种方式,您都希望确保在适配器上调用
notifyDataSetValidated()
,以强制它重新绘制所有视图。

在适配器方法
setFlowerIds
中添加方法
notifyDataSetChanged()这可能会解决您的问题

public  void setFlowerIds(int pos,int ID )
{
    mThumbIds[pos]= ID;
    notifyDataSetChanged(); // add this method
}
编辑:这就是我如何刷新我的
BaseAdapter
的方法,看看这是否适用于您。还有一个问题

public synchronized void refresAdapter(列出新项){
_items.clear();//我的适配器列表
_items.addAll(newItems);
notifyDataSetChanged();
}

为什么要在onCreate方法中创建两个适配器实例?为什么不创建一个实例并将该实例设置为gridview?第二个gridview用于在dialogBox中显示图像。问题是,当我选择从对话框的图像更新时,单击的图像变得不可见“\u items”将与适配器中的“mThumbIds”数组(我只是使用ArrayList)和“newItems”相同将是从“我的活动”传递的新列表,其中包含要更新适配器的新数据。您可能会收到一个错误,该错误将告诉您在UI线程上运行刷新。如果发生这种情况,请在我添加到答案的链接中查看我的答案
public  void setFlowerIds(int pos,int ID )
{
    mThumbIds[pos]= ID;
    notifyDataSetChanged(); // add this method
}
public synchronized void refresAdapter(List<Item> newItems) {
    _items.clear(); //my adapter list
    _items.addAll(newItems);
    notifyDataSetChanged();
}