首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将图像动态添加到网格视图

将图像动态添加到网格视图
EN

Stack Overflow用户
提问于 2011-10-17 14:51:15
回答 2查看 2.9K关注 0票数 0

如何在网格视图中动态添加网格项目?目前,我有一个包含我的图像的适配器。我想从URL获取图像,并将它们动态添加到我的网格视图中。我使用下面的代码从url下载图片。

代码语言:javascript
复制
                try {
            URL myFileUrl =null;  
         myFileUrl= new URL(imageUrl);

          HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
    conn.setDoInput(true);
    conn.connect();
    InputStream is = conn.getInputStream();

    bmImg = BitmapFactory.decodeStream(is);
    //images[i].setImageBitmap(bmImg);
      }       catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }

下面是mu图像适配器类

代码语言:javascript
复制
      public class ImageAdapter extends BaseAdapter{
    Context mContext;
    public static final int ACTIVITY_CREATE = 10;
    public ImageAdapter(Context c){
        mContext = c;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 9;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View v;
        if(convertView==null){
            LayoutInflater li = getLayoutInflater();
            v = li.inflate(R.layout.icon, null);
            TextView tv = (TextView)v.findViewById(R.id.icon_text);
            tv.setText("Profile "+position);
            ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
            iv.setImageResource(R.drawable.ondamoveicon);

        }
        else
        {
            v = convertView;
        }
        return v;
    }
    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
}

现在我的问题是,我想要显示通过适配器中的url下载的图像。我们将如何将这些图像传递给适配器类。

现在我得到了网格视图中所有地方的最后一张图像

有人能帮我吗?谢谢

EN

回答 2

Stack Overflow用户

发布于 2011-10-17 15:05:09

  1. 增加你的适配器类的数量。
  2. 你必须使用一些资源数组,它将存储图像。下载新镜像后,将计数加1,并用新镜像更新资源数组。
  3. 调用adapter.notifyDataSetChanged().

这将刷新gridView。

票数 3
EN

Stack Overflow用户

发布于 2011-10-17 15:05:38

您可以尝试LazyList,在main XML中,您可以将列表视图转换为网格视图。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7790268

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档