ListView中的评级栏混合列表项

时间:2013-07-25 14:46:33

标签: android android-cursoradapter ratingbar

我一直在尝试实施 ListView ,其中包含评级栏。我有onRatingChanged听众,我想根据评分更改项目中的 TextView

问题在于,当我触摸星星并更新它时,它会更新另一个 TextView的值。 我的适配器扩展 CursorAdapter 。如果我有getView()我想我会解决它,但我不知道如何处理 CursorAdapter ,因为我们没有使用getView()。

|------------------|
| TextView         | ---> TextView I want to update
| * * * * *        | ---> Rating Bar
|                  |
|__________________|

1 个答案:

答案 0 :(得分:1)

  

问题在于,当我触摸星星并更新它时,它会更新   另一个TextView的值。我的适配器扩展了CursorAdapter。如果我有   getView()我想我会解决它,但我不知道如何处理   CursorAdapter,因为我们没有使用getView()。

就像我在基于Cursor的适配器的评论中已经说过的那样,您可以使用newView()bindView()方法。以下是一个小例子:

public class CustomAdapter extends CursorAdapter {

    private static final int CURSOR_TEXT_COLUMN = 0;

    public CustomAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);

    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.text.setText(cursor.getString(CURSOR_TEXT_COLUMN));
        holder.progress
                .setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

                    @Override
                    public void onRatingChanged(RatingBar ratingBar,
                            float rating, boolean fromUser) {
                        // basic example on how you may update the
                        // TextView(you could use a tag etc).
                        // Keep in mind that if you scroll this row and come
                        // back the value will reset as you need to save the
                        // new rating in a more persistent way and update
                        // the progress
                        View rowView = (View) ratingBar.getParent();
                        TextView text = (TextView) rowView
                                .findViewById(R.id.the_text);
                        text.setText(String.valueOf(rating));
                    }
                });
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater mInflater = LayoutInflater.from(context);
        View rowView = mInflater.inflate(R.layout.row_layout, parent,
                false);
        ViewHolder holder = new ViewHolder();
        holder.text = (TextView) rowView.findViewById(R.id.the_text);
        holder.progress = (RatingBar) rowView
                .findViewById(R.id.the_progress);
        rowView.setTag(holder);
        return rowView;
    }

    static class ViewHolder {
        TextView text;
        RatingBar progress;
    }

}