我正在尝试定义一个Custom SimpleCursorAdapter类,它将用于填充ListView的行。我使用v4.widget.SimpleCursorAdapter库来向后兼容。我遇到了以下两部分代码的问题。
在构造函数中,我正在使用
super(context,layout,c,from,to);
与此同时,这已被弃用,我不知道如何修改它。
另外,
alarm_activated = (ToggleButton)row.findViewById(R.id.alarm_activated);
"排"无法解决,我不知道如何引用有问题的行。 (这个语法在ArrayAdapter中工作,我希望它也适用于SimpleCursorAdapter ......)。
也许,我的代码的其他部分(在下文中找到)是不正确的。
class AlarmRowAdapter extends SimpleCursorAdapter {
private Context mContext;
private Context appContext;
private int layout;
private Cursor cr;
private final LayoutInflater inflater;
public AlarmRowAdapter(Context context,int layout, Cursor c,String[] from,int[] to) {
super(context,layout,c,from,to);
this.layout=layout;
this.mContext = context;
this.inflater=LayoutInflater.from(context);
this.cr=c;
}
@Override
public View newView (Context context, Cursor cursor, ViewGroup parent) {
return inflater.inflate(layout, null);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
alarm_activated = (ToggleButton)row.findViewById(R.id.alarm_activated);
if (activationInt == 1) {
alarm_activated.setChecked(true);
alarm_activated.getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.MULTIPLY);
} else {
alarm_activated.setChecked(false);
}
alarm_activated.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
buttonView.getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.MULTIPLY);
} else {
buttonView.getBackground().setColorFilter(Color.LTGRAY, PorterDuff.Mode.MULTIPLY);
}
}
});
}
}
感谢您的帮助。
Ĵ
答案 0 :(得分:1)
根据Mike M.提出的建议(请参阅上面的评论),这是工作代码,以防其他人发现它有用。此外,请注意我在开发初始代码时使用了此link中的示例(感谢Bobbake4获取该代码)。
class AlarmRowAdapter extends SimpleCursorAdapter {
private Context mContext;
private Context appContext;
private int layout;
private Cursor cr;
private final LayoutInflater inflater;
public AlarmRowAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context,layout,c,from,to, flags);
this.layout=layout;
this.mContext = context;
this.inflater=LayoutInflater.from(context);
this.cr=c;
}
@Override
public View newView (Context context, Cursor cursor, ViewGroup parent) {
return inflater.inflate(layout, null);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
alarm_activated = (ToggleButton)view.findViewById(R.id.alarm_activated);
if (activationInt == 1) {
alarm_activated.setChecked(true);
alarm_activated.getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.MULTIPLY);
} else {
alarm_activated.setChecked(false);
}
alarm_activated.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
buttonView.getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.MULTIPLY);
} else {
buttonView.getBackground().setColorFilter(Color.LTGRAY, PorterDuff.Mode.MULTIPLY);
}
}
});
}
}
Ĵ
答案 1 :(得分:1)
对于标准构造函数,添加一个标志参数:CursorAdapter doc.
super (context,layout,c, from,to, 0);
您正在尝试使用row.findViewById
但变量行不存在。由于参数为view
,因此您应使用view.findViewById