0

我是 Android 和 Java 编程的新手。我有一个实现自定义光标适配器的类。问题是我需要能够访问侦听器内游标适配器中的一些信息。见下文:

    public class MyCursorAdapter extends CursorAdapter{  
        public MyCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
            super(context, c);
        }

        public void bindView(View view, Context context, Cursor cursor) {
            TextView ratingBarName = (TextView)view.findViewById(R.id.ratingbar_name);
            ratingBarName.setText(cursor.getString(
                cursor.getColumnIndex(MyDbAdapter.KEY_NAME)));

            RatingBar ratingBar = (RatingBar)view.findViewById(R.id.ratingbar);
            ratingBar.setRating(cursor.getFloat(
                cursor.getColumnIndex(MyDbAdapter.KEY_RATING)));


            RatingBar.OnRatingBarChangeListener barListener = 
                new RatingBar.OnRatingBarChangeListener() {
                public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromTouch) {
                    MyDbAdapter db = MyActivity.this.getDbHelper();

                    // NEED ACCESS TO CURSOR HERE SO I CAN DO:
                    // cursor.getColumnIndex(MyDbAdapter.KEY_ROWID);
                    // AND THEN USE THE ROW ID TO SAVE THE RATING IN THE DB
                    // HOW DO I DO THIS?
                }

            }               
            ratingBar.setOnRatingBarChangeListener(barListener);
       }

       public View newView(Context context, Cursor cursor, ViewGroup parent) {
           LayoutInflater inflater = LayoutInflater.from(context);
           View view = inflater.inflate(R.layout.ratingrow, parent, false);
           bindView(view, context, cursor);
           return view;
       }
   }

非常感谢你。

4

2 回答 2

2

像这样使您的光标最终成为:final Cursor cursor

 public void bindView(View view, Context context, final Cursor cursor)
于 2012-03-21T13:39:23.190 回答
1

在您输入侦听器之前设置为 的标签,RatingBar然后KEY_ROWID在侦听器中检索标签并在光标上使用它:

//...
ratingBar.setRating(cursor.getFloat(cursor.getColumnIndex(MyDbAdapter.KEY_RATING)));
ratingBar.setTag(new Long(cursor.getLong(MyDbAdapter.KEY_ROWID)));
RatingBar.OnRatingBarChangeListener barListener = 
                new RatingBar.OnRatingBarChangeListener() {
                public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromTouch) {
                    MyDbAdapter db = MyActivity.this.getDbHelper();                       
                    long theIdYouWant = (Long) ratingBar.getTag();                    
                    //use the id 
                }

            }    

//...
于 2012-03-21T13:44:06.637 回答