简体   繁体   中英

ratingbar issue in android

i am make application in which i am display rate-bar on listview and also display other 2 widget. i got some problem in this adapter is that when i am rate any list item and scroll list down and get back to top, rated item was clear there are no rating display on that particular item

let say i have give 3 star to 1st item and scroll down and get back to it, and it became 0

Here is my code :::

package com.AppFavorits;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

public class ListViewCustomAdapter extends BaseAdapter {

    public Context context;
    LayoutInflater inflator;
    ArrayList<Comment> appname;
    ArrayList<Comment> Packagenm;
    Drawable[] alIcon;
    static float[] rating;

    public ListViewCustomAdapter(Context context, ArrayList<Comment> appname,
            ArrayList<Comment> Packagenm, Drawable[] alIcon) {
        super();

        this.context = context;
        this.appname = appname;
        this.Packagenm = Packagenm;
        this.alIcon = alIcon;
        inflator = LayoutInflater.from(context);
        rating = new float[appname.size()];

        // Log.i("Parse adapter ", " Date adapter -> " +
        // arr_ScheduledDepartureTime[1]);

    }

    public int getCount() {
        // TODO Auto-generated method stub
        return appname.size();

    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return appname.get(position);
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public static class ViewHolder {
        ImageView imgvFavrowiconappicon;
        TextView txvxFavrowiconappname;
        RatingBar ratingBar1;

    }

    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        // View vi=convertView;
        // View view = null;
        ViewHolder viewholder;

        if (convertView == null) {
            viewholder = new ViewHolder();
            convertView = inflator.inflate(R.layout.outbox_list_item, null);
            viewholder.ratingBar1 = (RatingBar) convertView
                    .findViewById(R.id.ratingBar1);
            RatingBar.OnRatingBarChangeListener l = new RatingBar.OnRatingBarChangeListener() {
                public void onRatingChanged(RatingBar ratingBar, float rating,
                        boolean fromTouch) {
                    /*
                     * Integer myPosition=(Integer)ratingBar.getTag(); RowModel
                     * model=getModel(myPosition);
                     * 
                     * model.rating=rating;
                     * 
                     * LinearLayout parent=(LinearLayout)ratingBar.getParent();
                     * TextView label=(TextView)parent.findViewById(R.id.label);
                     */

                    // label.setText(model.toString());
                    Log.i("TAg", "rate" + rating);
                }
            };

            viewholder.ratingBar1.setOnRatingBarChangeListener(l);

            viewholder.txvxFavrowiconappname = (TextView) convertView
                    .findViewById(R.id.txvxFavrowiconappname);
            viewholder.imgvFavrowiconappicon = (ImageView) convertView
                    .findViewById(R.id.imgvFavrowiconappicon);
            // viewholder.ratingBar1 = (RatingBar)
            // convertView.findViewById(R.id.ratingBar1);
            convertView.setTag(viewholder);

        } else {
            viewholder = (ViewHolder) convertView.getTag();

        }

        viewholder.imgvFavrowiconappicon.setImageDrawable(alIcon[position]);
        viewholder.txvxFavrowiconappname.setText(appname.get(position)
                .toString());

        viewholder.ratingBar1.setTag(new Integer(position));
        viewholder.ratingBar1.setRating(0);
        /*
         * viewholder.ratingBar1.setOnTouchListener(new OnTouchListener() {
         * 
         * @Override public boolean onTouch(View v, MotionEvent event) { if
         * (event.getAction() == MotionEvent.ACTION_UP) { // TODO perform your
         * action here } return true; } });
         */
        return convertView;

    }
}

You getItemId() method in the adapter class should return position not 0 .

public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

UPDATE:

I had recently added ListView with CheckBox Scrolling Issue . Same should apply here, just try to get the position of ratingbar inside onRatingChanged() using ratingBar.getTag(); and set the current rating to it.

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