如何更改GridVIew的图像

时间:2013-03-20 06:34:32

标签: android image gridview

我有一个包含一些图像的GridView。我想点击它来改变图像。我可以选择从DialogBox中选择图像。但问题是gridview没有更新最新的图像。我发现了类似的问题How can I change Image on Gridview Runtime?。但即使是解决方案也无法解决我的问题。我的代码如下。

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

我的imageAdapter类就是这个

 package com.tdsoc.lw.utils;

 import android.content.Context;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.BaseAdapter;
 import android.widget.GridView;
 import android.widget.ImageView;

 public class ImageAdapter extends BaseAdapter {
private Context mContext;
private Integer[] mThumbIds;
private ImageView imageView;
// Keep all Images in array

// Constructor
public ImageAdapter(Context c)
{
    mContext = c;
}

public ImageAdapter(Context c, Integer[] mTid)
{
    mContext = c;
    mThumbIds = mTid;
}
@Override
public int getCount() 
{
    return mThumbIds.length;
}

@Override
public Object getItem(int position)
{
    return position;
}

@Override
public long getItemId(int position) 
{
    return mThumbIds[position];
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
     if( convertView == null)
     {
        imageView = new ImageView(mContext);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
     }
     else
     {
         imageView = (ImageView)convertView;
     }
     imageView.setImageResource(mThumbIds[position]);
     //notifyDataSetChanged();
     return imageView;
}
public Integer[] getFlowerIds()
{
    return mThumbIds;
}
public  void setFlowerIds(int pos,int ID )
{
    mThumbIds[pos]= ID;
}

 }

2 个答案:

答案 0 :(得分:1)

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

public  void setFlowerIds(int pos,int ID )
{
    mThumbIds[pos]= ID;
    notifyDataSetChanged(); // add this method
}

编辑:这是我刷新BaseAdapter的方式,看看这是否适合您。另请查看this问题

public synchronized void refresAdapter(List<Item> newItems) {
    _items.clear(); //my adapter list
    _items.addAll(newItems);
    notifyDataSetChanged();
}

答案 1 :(得分:0)

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

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