2012-03-21 54 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