0

我有一个包含一些图像的 GridView。我想通过单击来更改图像。我正在提供从对话框中选择图像的选项。但问题是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;
}

 }
4

2 回答 2

1

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

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

编辑:这就是我刷新我的方式BaseAdapter,看看这是否适合你。也看看这个问题

public synchronized void refresAdapter(List<Item> newItems) {
    _items.clear(); //my adapter list
    _items.addAll(newItems);
    notifyDataSetChanged();
}
于 2013-03-20T06:41:24.007 回答
0

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

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

于 2013-03-20T06:38:20.043 回答