【问题标题】:How to change image of GridVIew如何更改 GridVIew 的图像
【发布时间】:2013-03-20 06:34:31
【问题描述】:

我有一个包含一些图像的 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;
}

 }

【问题讨论】:

  • 为什么要在 onCreate 方法中创建适配器的两个实例?为什么不只创建一个实例并将该实例设置为 gridview?

标签: android image gridview


【解决方案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();
}

【讨论】:

  • "_items" 将与适配器中的 "mThumbIds" 数组相同(我只是使用 ArrayList),而 "newItems" 将是从我的活动传递的带有新数据的新列表更新我的适配器。
  • 您可能会收到一条错误消息,告诉您在 UI 线程上运行刷新。如果发生这种情况,只需在我添加到答案的链接中查看我的答案
【解决方案2】:

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

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

【讨论】:

  • 第二个gridview用于在对话框中显示图像。问题是当我选择从对话框的图像更新时,单击的图像变得不可见
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多