2012-03-21 10 views
0

AndroidとJavaプログラミングが初めてです。私はカスタムカーソルアダプタを実装するクラスを持っています。問題は、リスナー内のカーソル・アダプタ内の情報の一部にアクセスできる必要があることです。下記をご覧ください。AndroidのRatingBarsとリスナー

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.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

このようなあなたのカーソル決勝を行います。最終カーソルカーソルリスナーに入力して、リスナーにタグを取得し、それを使用する前にRatingBarKEY_ROWIDのためのタグとして設定し

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

ありがとうございます。ちょうど疑問に思っています - これを行う必要はありませんか? – Mewzer

+0

あなたの内部クラス(OnRatingBarChangeListener)cantは最終的な変数ではないので、そうするために "一般的に必要"です。解決済みのマークを付けてください。 –

関連する問題