0

内部に評価バーがあるListViewを実装しようとしています。リスナーがあり、評価に応じてアイテム内のTextViewを変更したいと考えています。onRatingChanged

問題は、星に触れて更新すると、別のTextView の値が更新されることです。私のアダプターはCursorAdapterを拡張します。私が持っていればgetView()解決すると思いますが、 getView() を使用していないため、 CursorAdapterを処理する方法がわかりません。

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

1 に答える 1

1

問題は、星に触れて更新すると、別の TextView の値が更新されることです。私のアダプターは CursorAdapter を拡張します。getView() があれば解決すると思いますが、getView() を使用していないため、CursorAdapter の処理方法がわかりません。

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;
    }

}
于 2013-07-26T06:35:00.707 に答える