简体   繁体   English

如何在Android的自定义列表视图中设置图片?

[英]How to set image in custom listview in Android?

I am developing an application in which I have to show ratings depending on the values that I am receiving after parsing the XML response in listview. 我正在开发一个应用程序,其中必须根据在列表视图中解析XML响应后收到的值来显示等级。 I have implemented it using the Custom Adapter and showing the images in the getView() method like : 我已经使用“自定义适配器”实现了它,并在getView()方法中显示了图像,例如:

String rating = Constants.menuRatingList.get(position);

if (rating.equals("1")) {
  rateImg1.setImageResource(R.drawable.stary);                      
}

The problem is when I scroll the down to the last item and again move upwards, it is redrawing the list row. 问题是当我向下滚动到最后一个项目并再次向上移动时,它正在重新绘制列表行。

Someone please suggest me approach to stop the redrawing the list row and set the image value permanently. 有人建议我停止重新绘制列表行并永久设置图像值。

How do you create the rateImg1 variable? 如何创建rateImg1变量? Maybe you should get it from the view that is passed to the getView() method? 也许您应该从传递给getView()方法的视图中获取它? Something like this: 像这样:

public View getView(int position, View view, ViewGroup parent) {
if (view == null) {
view = LayoutInflater.from(ctx).inflate(R.layout.my_layout, parent, false);
}
((ImageView) view.findViewById(R.id.my_image)).setImageResource(R.drawable.some_drawable);
return view;
}

Also you can implement getViewTypeCount() and getItemViewType() and check them in the above method not to redraw the same image if it is already set. 您也可以实现getViewTypeCount()和getItemViewType()并在上述方法中检查它们,以确保即使已设置相同的图像也不会重绘。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM