在加载图片数据时,老是在加载第一张图片,请高手指点迷津:
下面是我的代码:
@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);        setContentView(R.layout.goods_gallery);
  
        m_gallery = (GridView) findViewById(R.id.img_text_gallery);
        
        GridView_property();        Intent intent = getIntent();
        String arg = intent.getStringExtra("type");
        if (arg.equals("hotsale")) {
            key = KEYHOTSALE;
            title = "热卖商品";
        }        mData = new ArrayList<HashMap<String, Object>>();
        adapter = new GoodsGalleryAdapter(this);
        m_gallery.setAdapter(adapter);        m_gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                if (mData != null && mData.size() > 0) {                    Intent it = new Intent(Global.ACTION_MALLGROUP_NAVIGATE);
                    it.putExtra("launch", ProductDetailActivity.class);
                    it.putExtra("product",
                            (String) mData.get(arg2).get(PRODUCTID));
                    it.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    sendBroadcast(it);                }
            }
        });        m_gallery.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.clearFocus();
                return false;
            }
        });        // new a thread{loadData}
        refresh();    }    /**
     * 动态设置gridview的属性  
     */
    public void GridView_property() {
 
        m_gallery.setNumColumns(12);           }    private Handler handler = new Handler() {        public void handleMessage(android.os.Message msg) {            Log.d(TAG, "Handle message " + msg.what);
            if (null != waitDlg)
                waitDlg.dismiss();
            if (MSGLOAD == msg.what) {
                m_gallery_product = (ArrayList<MallProductBean>) msg.obj;
                updateUI();
            }
        }
    };      private void initData() {
        if (null == mData)
            mData = new ArrayList<HashMap<String, Object>>();
//        mData.clear();//        if (m_gallery_product == null)
//            return;
        for (int i = 0; i < m_gallery_product.size(); i++) {
//            Log.v(TAG, "getView0 cache contains " + i);
            MallProductBean m = m_gallery_product.get(i);
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put(PRODUCTNAME, m.getName());
            map.put(PRODUCTPIC, FunctionsUtil.appendImageSize(m.getPicurl(), 195, 260));
            map.put(PRODUCTPRICENEW, m.getCurrentprice());
            map.put(PRODUCTPRICEOLD, m.getRetailprice());
            map.put(TOTALVOLUMS, m.getAmt());
            map.put(PRODUCTID, m.getId());
            mData.add(map);
        }
          }    public void updateUI() {        initData();
        adapter.notifyDataSetChanged();
    }    public void refresh() {
        if (waitDlg != null) {
            waitDlg.dismiss();
        }
        waitDlg = ProgressDialog.show(getParent(), null, "请等候...");
        new Thread() {
            public void run() {
                // loadData();
                VOProductList tag = MallDao.mallIndexRecommend(key);
                if (tag != null) {
                    ArrayList<MallProductBean> ga = tag.getProductinfo();
                    handler.sendMessage(handler.obtainMessage(MSGLOAD, ga));
                }
            }
        }.start();
    }    private final class ViewHolder {        public ImageView m_iv_goods_item;
        public TextView m_tv_product_name;
        public TextView m_tv_goods_new_price;    }    /** custom adapter */
    private class GoodsGalleryAdapter extends BaseAdapter {        private LayoutInflater mInflater;
        private AsyncImageLoader mIconloader;        private Context mContext;        public GoodsGalleryAdapter(Context context) {
            this.mContext = context;            this.mInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mIconloader = new AsyncImageLoader(new ImageCallback() {
                // 异步加载图片方法
                @Override
                public void imageLoaded(Drawable imageDrawable, String imageUrl, Object to) {
                    ImageView v = (ImageView) m_gallery.findViewWithTag(imageUrl);
                    if (v != null) {
                        v.setImageDrawable(imageDrawable);
                        adapter.notifyDataSetChanged();
                    }                }
            });
        }        @Override
        public int getCount() {
            return mData == null ? 0 : mData.size();        }        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub            Log.v(TAG, "getView1 cache contains " + position);
            Log.v(TAG, "getView2 cache contains " + convertView);  
            
            ViewHolder holder;
            if (convertView == null) {                convertView = mInflater.inflate(R.layout.goods_gallery_item, null);
                
                holder = new ViewHolder();
                
                holder.m_iv_goods_item = (ImageView) convertView
                        .findViewById(R.id.iv_goods_list_item);
                holder.m_tv_product_name = (TextView) convertView
                        .findViewById(R.id.tv_goodsname_item);
                holder.m_tv_goods_new_price = (TextView) convertView
                        .findViewById(R.id.tv_goodsprice_new);
  
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }            if (holder != null) {
                Log.v(TAG, "getView3 contains " + mData.size());
                if (mData != null && mData.size() > 0) {
                    // if (mData.size() > 4) {
                    holder.m_tv_product_name.setText((String) mData.get(
                            position).get(PRODUCTNAME));
                    holder.m_tv_goods_new_price.setText("¥"
                            + String.valueOf((Double) mData.get(position).get(
                                    PRODUCTPRICENEW)));
                   
                    String url = (String) mData.get(position).get(PRODUCTPIC);
                    if (url != null && url.length() > 0) {
                        Log.v(TAG, "getView4 cache contains " + url);
                        holder.m_iv_goods_item.setTag(url);
                        Drawable cacheImage = mIconloader.loadDrawable(url,
                                null);
                        if (cacheImage == null) {
                            try {
                                holder.m_iv_goods_item
                                        .setImageResource(R.drawable.icon);
                            } catch (OutOfMemoryError e) {
                                e.printStackTrace();
                            }
                        } else {
                            holder.m_iv_goods_item.setImageDrawable(cacheImage);
                            // adapter.notifyDataSetChanged();
                        }
                    }                }            }            return convertView;
        }    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return false;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }
}

解决方案 »

  1.   

    这个后台打印:
    01-11 07:16:32.183: VERBOSE/GirdViewActivity(4583): OnStart
    01-11 07:16:32.245: VERBOSE/GirdViewActivity(4583): OnResume
    01-11 07:16:34.764: DEBUG/GirdViewActivity(4583): Handle message 0
    01-11 07:16:35.414: VERBOSE/GirdViewActivity(4583): getView1 cache contains 0
    01-11 07:16:35.456: VERBOSE/GirdViewActivity(4583): getView2 cache contains null
    01-11 07:16:35.476: VERBOSE/GirdViewActivity(4583): getView3 contains 12
    01-11 07:16:35.476: VERBOSE/GirdViewActivity(4583): getView4 cache contains http://miugopic.miugo.net/PhotoHome/dfh/海味/310467大糖心鲍(500g)_195x260.JPG
    01-11 07:16:35.514: VERBOSE/GirdViewActivity(4583): getView1 cache contains 0
    01-11 07:16:35.514: VERBOSE/GirdViewActivity(4583): getView2 cache contains android.widget.RelativeLayout@44f03f30
    01-11 07:16:35.534: VERBOSE/GirdViewActivity(4583): getView3 contains 12
    01-11 07:16:35.534: VERBOSE/GirdViewActivity(4583): getView4 cache contains http://miugopic.miugo.net/PhotoHome/dfh/海味/310467大糖心鲍(500g)_195x260.JPG
    01-11 07:16:35.555: VERBOSE/GirdViewActivity(4583): getView1 cache contains 0
    01-11 07:16:35.555: VERBOSE/GirdViewActivity(4583): getView2 cache contains android.widget.RelativeLayout@44f03f30
    01-11 07:16:35.555: VERBOSE/GirdViewActivity(4583): getView3 contains 12
    01-11 07:16:35.555: VERBOSE/GirdViewActivity(4583): getView4 cache contains http://miugopic.miugo.net/PhotoHome/dfh/海味/310467大糖心鲍(500g)_195x260.JPG
    01-11 07:16:35.564: VERBOSE/GirdViewActivity(4583): getView1 cache contains 0
    01-11 07:16:35.604: VERBOSE/GirdViewActivity(4583): getView2 cache contains android.widget.RelativeLayout@44f03f30
    01-11 07:16:35.604: VERBOSE/GirdViewActivity(4583): getView3 contains 12
    01-11 07:16:35.604: VERBOSE/GirdViewActivity(4583): getView4 cache contains http://miugopic.miugo.net/PhotoHome/dfh/海味/310467大糖心鲍(500g)_195x260.JPG
    01-11 07:16:35.604: VERBOSE/GirdViewActivity(4583): getView1 cache contains 0
    01-11 07:16:35.604: VERBOSE/GirdViewActivity(4583): getView2 cache contains android.widget.RelativeLayout@44f03f30
    01-11 07:16:35.604: VERBOSE/GirdViewActivity(4583): getView3 contains 12
    01-11 07:16:35.624: VERBOSE/GirdViewActivity(4583): getView4 cache contains http://miugopic.miugo.net/PhotoHome/dfh/海味/310467大糖心鲍(500g)_195x260.JPG
    01-11 07:16:35.676: VERBOSE/GirdViewActivity(4583): getView1 cache contains 0
    01-11 07:16:35.676: VERBOSE/GirdViewActivity(4583): getView2 cache contains android.widget.RelativeLayout@44f03f30
    01-11 07:16:35.676: VERBOSE/GirdViewActivity(4583): getView3 contains 12
    01-11 07:16:35.684: VERBOSE/GirdViewActivity(4583): getView4 cache contains http://miugopic.miugo.net/PhotoHome/dfh/海味/310467大糖心鲍(500g)_195x260.JPG
    01-11 07:16:35.694: VERBOSE/GirdViewActivity(4583): getView1 cache contains 0
    01-11 07:16:35.694: VERBOSE/GirdViewActivity(4583): getView2 cache contains android.widget.RelativeLayout@44f03f30
    01-11 07:16:35.694: VERBOSE/GirdViewActivity(4583): getView3 contains 12
    01-11 07:16:35.694: VERBOSE/GirdViewActivity(4583): getView4 cache contains http://miugopic.miugo.net/PhotoHome/dfh/海味/310467大糖心鲍(500g)_195x260.JPG
    01-11 07:16:35.726: VERBOSE/GirdViewActivity(4583): getView1 cache contains 0
    01-11 07:16:35.726: VERBOSE/GirdViewActivity(4583): getView2 cache contains android.widget.RelativeLayout@44f03f30
    01-11 07:16:35.744: VERBOSE/GirdViewActivity(4583): getView3 contains 12
    01-11 07:16:35.744: VERBOSE/GirdViewActivity(4583): getView4 cache contains http://miugopic.miugo.net/PhotoHome/dfh/海味/310467大糖心鲍(500g)_195x260.JPG
    01-11 07:16:35.774: VERBOSE/GirdViewActivity(4583): getView1 cache contains 0
    01-11 07:16:35.774: VERBOSE/GirdViewActivity(4583): getView2 cache contains android.widget.RelativeLayout@44f03f30
    01-11 07:16:35.774: VERBOSE/GirdViewActivity(4583): getView3 contains 12
    01-11 07:16:35.784: VERBOSE/GirdViewActivity(4583): getView4 cache contains http://miugopic.miugo.net/PhotoHome/dfh/海味/310467大糖心鲍(500g)_195x260.JPG
    01-11 07:16:35.784: VERBOSE/GirdViewActivity(4583): getView1 cache contains 0
    01-11 07:16:35.794: VERBOSE/GirdViewActivity(4583): getView2 cache contains android.widget.RelativeLayout@44f03f30
    01-11 07:16:35.794: VERBOSE/GirdViewActivity(4583): getView3 contains 12
    01-11 07:16:35.804: VERBOSE/GirdViewActivity(4583): getView4 cache contains http://miugopic.miugo.net/PhotoHome/dfh/海味/310467大糖心鲍(500g)_195x260.JPG
    01-11 07:16:35.804: VERBOSE/GirdViewActivity(4583): getView1 cache contains 0
    01-11 07:16:35.804: VERBOSE/GirdViewActivity(4583): getView2 cache contains android.widget.RelativeLayout@44f03f30
    01-11 07:16:35.804: VERBOSE/GirdViewActivity(4583): getView3 contains 12
    01-11 07:16:35.816: VERBOSE/GirdViewActivity(4583): getView4 cache contains http://miugopic.miugo.net/PhotoHome/dfh/海味/310467大糖心鲍(500g)_195x260.JPG
    01-11 07:16:35.824: VERBOSE/GirdViewActivity(4583): getView1 cache contains 0
    01-11 07:16:35.835: VERBOSE/GirdViewActivity(4583): getView2 cache contains android.widget.RelativeLayout@44f03f30
    01-11 07:16:35.835: VERBOSE/GirdViewActivity(4583): getView3 contains 12
    01-11 07:16:35.835: VERBOSE/GirdViewActivity(4583): getView4 cache contains http://miugopic.miugo.net/PhotoHome/dfh/海味/310467大糖心鲍(500g)_195x260.JPG
    01-11 07:16:35.864: VERBOSE/GirdViewActivity(4583): getView1 cache contains 0
    01-11 07:16:35.864: VERBOSE/GirdViewActivity(4583): getView2 cache contains android.widget.RelativeLayout@44f03f30
    01-11 07:16:35.894: VERBOSE/GirdViewActivity(4583): getView3 contains 12
    01-11 07:16:35.894: VERBOSE/GirdViewActivity(4583): getView4 cache contains http://miugopic.miugo.net/PhotoHome/dfh/海味/310467大糖心鲍(500g)_195x260.JPG
    01-11 07:16:35.914: VERBOSE/GirdViewActivity(4583): getView1 cache contains 0
    01-11 07:16:35.914: VERBOSE/GirdViewActivity(4583): getView2 cache contains android.widget.RelativeLayout@44f03f30
    01-11 07:16:35.928: VERBOSE/GirdViewActivity(4583): getView3 contains 12
    01-11 07:16:35.928: VERBOSE/GirdViewActivity(4583): getView4 cache contains http://miugopic.miugo.net/PhotoHome/dfh/海味/310467大糖心鲍(500g)_195x260.JPG
    01-11 07:16:35.934: VERBOSE/GirdViewActivity(4583): getView1 cache contains 0
    01-11 07:16:35.934: VERBOSE/GirdViewActivity(4583): getView2 cache contains android.widget.RelativeLayout@44f03f30
    01-11 07:16:35.934: VERBOSE/GirdViewActivity(4583): getView3 contains 12
    01-11 07:16:35.954: VERBOSE/GirdViewActivity(4583): getView4 cache contains http://miugopic.miugo.net/PhotoHome/dfh/海味/310467大糖心鲍(500g)_195x260.JPG
    01-11 07:16:35.984: VERBOSE/GirdViewActivity(4583): getView1 cache contains 0
    01-11 07:16:35.984: VERBOSE/GirdViewActivity(4583): getView2 cache contains android.widget.RelativeLayout@44f03f30
    01-11 07:16:35.984: VERBOSE/GirdViewActivity(4583): getView3 contains 12
    01-11 07:16:35.984: VERBOSE/GirdViewActivity(4583): getView4 cache contains http://miugopic.miugo.net/PhotoHome/dfh/海味/310467大糖心鲍(500g)_195x260.JPG
    01-11 07:16:36.004: VERBOSE/GirdViewActivity(4583): getView1 cache contains 0
    01-11 07:16:36.004: VERBOSE/GirdViewActivity(4583): getView2 cache contains android.widget.RelativeLayout@44f03f30
    01-11 07:16:36.004: VERBOSE/GirdViewActivity(4583): getView3 contains 12
    01-11 07:16:36.004: VERBOSE/GirdViewActivity(4583): getView4 cache contains http://miugopic.miugo.net/PhotoHome/dfh/海味/310467大糖心鲍(500g)_195x260.JPG
    01-11 07:16:36.034: VERBOSE/GirdViewActivity(4583): getView1 cache contains 0
    01-11 07:16:36.034: VERBOSE/GirdViewActivity(4583): getView2 cache contains android.widget.RelativeLayout@44f03f30
    01-11 07:16:36.034: VERBOSE/GirdViewActivity(4583): getView3 contains 12
    01-11 07:16:36.034: VERBOSE/GirdViewActivity(4583): getView4 cache contains http://miugopic.miugo.net/PhotoHome/dfh/海味/310467大糖心鲍(500g)_195x260.JPG
    01-11 07:16:36.044: VERBOSE/GirdViewActivity(4583): getView1 cache contains 0
    01-11 07:16:36.044: VERBOSE/GirdViewActivity(4583): getView2 cache contains android.widget.RelativeLayout@44f03f30
    01-11 07:16:36.044: VERBOSE/GirdViewActivity(4583): getView3 contains 12
    01-11 07:16:36.064: VERBOSE/GirdViewActivity(4583): getView4 cache contains http://miugopic.miugo.net/PhotoHome/dfh/海味/310467大糖心鲍(500g)_195x260.JPG
      

  2.   

    看看在布局文件里img_text_gallery的GridView的高度属性设置的是不是wrap_content,如果是改掉试试。
      

  3.   

      ImageView v = (ImageView) m_gallery.findViewWithTag(imageUrl);
      if (v != null) {
      v.setImageDrawable(imageDrawable);
      adapter.notifyDataSetChanged();
      }既然你的imageUrl 都是一样的, 找到的永远只是你的第一个tag为imageUrl 的imageView。所以更新只是第一个。还有一点看不懂holder.m_iv_goods_item.setTag(url);
      Drawable cacheImage = mIconloader.loadDrawable(url,
      null);
      if (cacheImage == null) {
      try {
      holder.m_iv_goods_item
      .setImageResource(R.drawable.icon);
      } catch (OutOfMemoryError e) {
      e.printStackTrace();
      }
      } else {
      holder.m_iv_goods_item.setImageDrawable(cacheImage);
      // adapter.notifyDataSetChanged();
      }
      }这里你有使用了异步吗?
      

  4.   

    这里有人么?listview中使用了cursoradapter ,在滑动屏幕的时候 向上滑动 和向下滑动 有区别么? 为什么我在向上滑动过程中会出现内存溢出的现象啊?求高手帮忙啊?
    还有就是 在滑动过程中内存数据是不是会递增?最好把原理给讲下~ 谢谢哈!
    Cursor = dbHelper.getData(sql, null);  这里的查询语句 和cursoradapter 有关么?和内存溢出有关么?