简体   繁体   中英

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. I have implemented it using the Custom Adapter and showing the images in the getView() method like :

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? Maybe you should get it from the view that is passed to the getView() method? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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