GridView getview 重复调用

gaven_zhong 2012-01-11 03:12:26
在加载图片数据时,老是在加载第一张图片,请高手指点迷津:
下面是我的代码:
@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);
}
}


}
...全文
796 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
gaven_zhong 2012-06-28
  • 打赏
  • 举报
回复
算是解决了
我贴上得代码没有问题应该,因为后来修改了本地缓存,就没有这些问题了
dongjie40407 2012-05-11
  • 打赏
  • 举报
回复
这里有人么?listview中使用了cursoradapter ,在滑动屏幕的时候 向上滑动 和向下滑动 有区别么? 为什么我在向上滑动过程中会出现内存溢出的现象啊?求高手帮忙啊?
还有就是 在滑动过程中内存数据是不是会递增?最好把原理给讲下~ 谢谢哈!
Cursor = dbHelper.getData(sql, null); 这里的查询语句 和cursoradapter 有关么?和内存溢出有关么?
fokman 2012-05-09
  • 打赏
  • 举报
回复
请问楼主解决了么? 碰到同样的问题
futurebp 2012-03-30
  • 打赏
  • 举报
回复
布局文件的问题
老科达 2012-02-01
  • 打赏
  • 举报
回复
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();
}
}

这里你有使用了异步吗?
HappyDelano 2012-01-31
  • 打赏
  • 举报
回复
看看在布局文件里img_text_gallery的GridView的高度属性设置的是不是wrap_content,如果是改掉试试。
Q23185954 2012-01-31
  • 打赏
  • 举报
回复
请问解决了么?我也出现这个问题了
gaven_zhong 2012-01-11
  • 打赏
  • 举报
回复
这个后台打印:
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

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧