0

我正在制作应用程序,其中我在列表视图上显示速率栏并显示其他 2 个小部件。我在这个适配器中遇到了一些问题,当我对任何列表项进行评分并向下滚动列表并返回顶部时,被评分的项目很明显,该特定项目上没有评分显示

假设我给第一个项目 3 星并向下滚动并返回它,它变成了 0

这是我的代码:::

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;

    }
}
4

1 回答 1

2

getItemId()在适配器类中的方法应该返回positionnot 0

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

更新:

我最近添加了带有 CheckBox Scrolling Issue 的 ListView。同样应该适用于此,只需尝试使用其中position的 ratingbar并将当前等级设置为它。onRatingChanged()ratingBar.getTag();

于 2012-06-05T05:10:12.193 回答