2012-03-21 48 views
0

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

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

非常感谢。

回答

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 
       } 

      }  

//... 
2

让你的光标最后是这样的:最终光标光标

public void bindView(View view, Context context, final Cursor cursor) 
+0

谢谢你的帮忙。只是想知道 - 是否必须这样做表明不好的设计或正在改变为最终通常需要在这种情况下? – Mewzer 2012-03-21 13:43:40

+0

你的内部类(OnRatingBarChangeListener)不能得到非最终变量,因此它是“通常需要的”。请标记为已解决。 – 2012-03-21 13:47:21