Search code examples
androidandroid-listviewandroid-custom-view

List view shuffles custom component view


I am using a custom view component to draw a pie ( a simple pie) . Each list Item has a pie with different values.

Now when I use it in a list view the pie values are shuffled and sometimes not updated. My default pie value is 1. When I first open my list the first item shows proper value rest of the list items show only the defalt value 1 regardless of the actual value.

When I scroll the list the pie get value but that are shuffled . The first item has value of 5th , 2nd has value of 3rd and 3rd has value of something else.

Code of custom view:

    public class PieRating extends View{
    private float Rate;
    private Paint p;
    private int startX;
    private int startY;
    private int radius;
    private ArrayList<Integer> colors;
    private ArrayList<Float> values;
    Bitmap bitmap;
    Context mContext;
    public PieRating(Context context, AttributeSet attrs) {
        super(context, attrs);

        Rate=1;
        mContext = context;

        p = new Paint();
        p.setAntiAlias(true);

        colors = new ArrayList<Integer>();
        values = new ArrayList<Float>();

        startX = 0;
        startY = 0;
        radius = 20;

        colors.add(Color.GRAY);
        colors.add(Color.TRANSPARENT);

        values.add(Rate);
        values.add(5 - Rate);

    }

    public void setRating(float Rate) {
        this.Rate = Rate;
        values.clear();
        values.add(Rate);
        values.add(5 - Rate);

    }

    public float getRating(){
        return Rate;


    }

    @Override
    protected void onDraw(Canvas canvas) {

        float offset = 0;
        float sum = 0;
        for (int a = 0; a < values.size(); a++) {
            sum += values.get(a);
        }

        float angle = (float) (360 / sum);

        RectF rectF = new RectF();
        rectF.set(getStartX(), getStartY(), getStartX() + getRadius(),
                getStartY() + getRadius());

        for (int i = 0; i < values.size(); i++) {

            p.setColor(colors.get(i));

            if (i == 0) {
                canvas.drawArc(rectF, 270, values.get(i) * angle, true, p);
            } 
            else {
                canvas.drawArc(rectF, offset, values.get(i) * angle, true, p);
            }

            offset += (values.get(i) * angle + 270);
        }

        canvas.save();

    }


    public int getStartX() {
        return startX;
    }

    public void setStartX(int startX) {
        this.startX = startX;
    }

    public int getStartY() {
        return startY;
    }

    public void setStartY(int startY) {
        this.startY = startY;
    }

    public int getRadius() {
        return radius;
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }

    public ArrayList<Integer> getColors() {
        return colors;
    }

    public void setColors(ArrayList<Integer> colors) {
        this.colors = colors;
    }   

    public ArrayList<Float> getValues() {
        return values;
    }

    public void setValues(ArrayList<Float> values) {
        this.values = values;
    }
}

And my list

public View getView(int position, View convertView, ViewGroup parent) {


PieRating pr =((PieRating)findViewById(R.id.score_pie_container));
            try
            {
            float rating = Float.parseFloat(JSONHelper.getString(mRestaurants, "restaurants.restaurant[].opinion.general", new int[] { position }));
            pr.setRating(rating);
            }
            catch (Exception e) {
                // TODO: handle exception
            }
}

Tried calling pr.invalidate once I set rating but no good.


Solution

  • silly mistake

    ((PieRating)convertView.findViewById(R.id.score_pie_container))

    I missed the convertView part ..

    Leaving answer just in case someone runs into similar issue