2

我制作的 listView 有一个问题,它有一个 ratingbar 和一个 texview 元素。它是一个非常简单的程序,用于显示列表并存储用户在 ratingBar 中输入的星星的值。不幸的是,由于某种原因,当我向上和向下滚动离开屏幕的行时,当回滚时,它们的星星会变回它们的初始值。代码如下。如果有人可以提供帮助,那就太好了。谢谢。

package com.example.test;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;

public class MainActivity extends Activity implements RatingBar.OnRatingBarChangeListener {

private String[] textList = {"Pikachu","Raichu","Raticate","Ratata","Absol","Abra","Kadabra","Pidgey","Mime","Meowth","Mew","Squirtle","Blastoise","Goop","Kilobyte","Armada"};
private ArrayList<String> list = new ArrayList<String>();
ListView listView;

protected void onCreate(Bundle icicle) 
{
    super.onCreate(icicle);
    setContentView(R.layout.activity_main);

    for(int i=0; i<textList.length; i++)
    {
        list.add(i,textList[i]);
    }

    listView = (ListView)findViewById(R.id.listRow);
    listView.setAdapter(new setListView(list));
}

class setListView extends ArrayAdapter<String>
{
    public setListView(ArrayList<String> list)
    {
        super(MainActivity.this, R.layout.custom_layout, R.id.text, list);
    }

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

        if(convertView == null)
        {
            LayoutInflater inflater = getLayoutInflater();
            row = inflater.inflate(R.layout.custom_layout, parent, false);
        }

        RatingBar ratingBar = (RatingBar)row.findViewById(R.id.rating);
        RatingBarInfo rating;
        rating = (RatingBarInfo)ratingBar.getTag();
        if(rating == null)
        {
            rating = new RatingBarInfo(position);
            ratingBar.setTag(rating);
        }
        ratingBar.setRating(rating.getRating());

        TextView text = (TextView)row.findViewById(R.id.text);
        text.setText(list.get(position));

        return(row);
    }
}

class RatingBarInfo
{
    float rating;
    int position;

    public RatingBarInfo(int positionTemp)
    {
        rating = 2.0f;
        position = positionTemp;
    }

    public void setRating(float ratingTemp)
    {
        rating = ratingTemp;
    }

    public float getRating()
    {
        return rating;
    }

    public int getPosition()
    {
        return position;
    }
}

public void onRatingChanged(RatingBar ratingBar, float rating,boolean fromUser) 
{
    RatingBarInfo ratingChange = (RatingBarInfo)ratingBar.getTag();
    ratingChange.setRating(rating);
    ratingBar.setTag(ratingChange);
    ((ArrayAdapter<String>)listView.getAdapter()).notifyDataSetChanged();
}
}
4

1 回答 1

0

您忘记在 RatingBar 上设置 OnRatingBarChangeListener。只需将此行添加到您的 getView() 方法中,它应该可以正常工作。

ratingBar.setOnRatingBarChangeListener(MainActivity.this)
于 2013-01-05T08:29:27.330 回答