Android中的分级栏和侦听器
我不熟悉Android和Java编程。我有一个实现自定义游标适配器的类。问题是我需要能够在侦听器中访问游标适配器中的一些信息。见下文:Android中的分级栏和侦听器,android,database,sqlite,android-cursoradapter,Android,Database,Sqlite,Android Cursoradapter,我不熟悉Android和Java编程。我有一个实现自定义游标适配器的类。问题是我需要能够在侦听器中访问游标适配器中的一些信息。见下文: public class MyCursorAdapter extends CursorAdapter{ public MyCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) { super(context,
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;
}
}
非常感谢。将光标设为最终光标,如下所示:final光标
public void bindView(View view, Context context, final Cursor cursor)
将光标设为final,如下所示:final光标
public void bindView(View view, Context context, final Cursor cursor)
在进入侦听器,然后在侦听器中检索标记并在光标上使用之前,将
键设置为分级栏
的标记:
//...
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
}
}
//...
在进入侦听器,然后在侦听器中检索标记并在光标上使用之前,将键设置为分级栏
的标记:
//...
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
}
}
//...
谢谢你的帮助。我只是想知道,在这种情况下,必须这样做是否意味着糟糕的设计,或者通常需要更改为final?您的内部类(OnRatingBarChangeListener)无法获取非final变量,所以是的,它是“通常需要的”。请标记为已解决。谢谢您的帮助。我只是想知道,在这种情况下,必须这样做是否意味着糟糕的设计,或者通常需要更改为final?您的内部类(OnRatingBarChangeListener)无法获取非final变量,所以是的,它是“通常需要的”。请标记为已解决。