我正在实现一个显示从.net服务器获取的图像和消息的应用程序.为此,我使用for循环动态地添加了LinearLayout和TextView和
ImageView.
起初我收到消息,我将它们添加到TextView中,并在ImageView中放置虚拟图像:
起初我收到消息,我将它们添加到TextView中,并在ImageView中放置虚拟图像:
for(int i = 0; i < messages.size(); i++) { FrameLayout f1 = new FrameLayout(this); LinearLayout l1 = new LinearLayout(this); LinearLayout l2 = new LinearLayout(this); im = new ImageView(this); TextView tv = new TextView(this); tv.setText(messages.get(i).getmessage()); im.setimageResource(R.drawable.person); l1.addView(tv); l2.addView(im); f1.addView(l1); f1.addView(l2); ((LinearLayout)findViewById(R.id.LinearlayoutMessage)).addView(f1); }
过了一段时间,我得到原始图像.我的目的是用原始图像替换虚拟图像.为了达到这个目的,我正在使用imageview.getid(),不幸的是我得到了imageview的最后一个id.但我需要获得每个个人的imageview id.
用真实图像改变虚拟图像的最佳方法是什么?
解决方法
看起来您需要将每个imageView ID存储在每条消息的地图中 – > imageView如下:
Map<Message,Long> messageMapping = new HashMap<Message,Long>(); for(int i = 0; i < messages.size(); i++) { FrameLayout f1 = new FrameLayout(this); LinearLayout l1 = new LinearLayout(this); LinearLayout l2 = new LinearLayout(this); im = new ImageView(this); TextView tv = new TextView(this); //add mapping messageMapping.put(messages.get(i),im.getId()); tv.setText(messages.get(i).getmessage()); im.setimageResource(R.drawable.person); l1.addView(tv); l2.addView(im); f1.addView(l1); f1.addView(l2); ((LinearLayout)findViewById(R.id.LinearlayoutMessage)).addView(f1); }
然后,当需要将图像延迟加载到现有的imageViews时,您只需要通过消息映射查找imageView id引用:
(总猜猜你的代码看起来像什么)
for(int i = 0; i < messages.size(); i++){ Image image = queryServerForImage(messages.get(i).getimageId()); ImageView imageView = ((ImageView)findViewById(messageMapping.get(messages.get(i)))); //some conversion & seteup may be needed to get image into proper format imageView.setimageBitmap(image); }
您可以使用异步任务执行类似的操作.您可以使用关联的资源ID跨越消息ImageView创建循环中的每一个,并让每个asyc任务更新网络响应中的ui.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。