在列表視圖中創建的自定義Adapter用來監聽checkbook和列表的狀態,代碼:
public class CustomAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] names;
private final String TAG = "MyPerformanceArrayAdapter";
private static final int TASK_CREATE = 0;
private static final int ACTIVITY_EDIT = 1;
static class ViewHolder {
public TextView text;
public CheckBox reminderCheckBox;
}
public CustomAdapter(Activity context, String[] names) {
super(context, R.layout.reminder_row, names);
this.context = context;
this.names = names;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.reminder_row, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) rowView.findViewById(R.id.reminderRowTextId);
viewHolder.reminderCheckBox = (CheckBox) rowView.findViewById(R.id.CheckBoxId);
rowView.setTag(viewHolder);
}
final int pos = position;
final ViewHolder holder = (ViewHolder) rowView.getTag();
String s = names[position];
holder.text.setText(s);
holder.reminderCheckBox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (holder.reminderCheckBox.isChecked()) {
Toast.makeText(getContext(), "pos-->chkd" + pos, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getContext(), "pos-->un--chkd" + pos, Toast.LENGTH_SHORT).show();
}
}
});
holder.text.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "TEXT CLICKED" + pos , Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context,ReminderModificationActivity.class);
intent.putExtra(TasksDBAdapter.KEY_ROWID, pos);
Log.i(TAG, "row clickd --> " + pos);
startActivity(intent,ACTIVITY_EDIT); <<<<-------ERROR ON THIS LINE
}
});
return rowView;
}
}
然後出現錯誤信息:
The method startActivity(Intent, int) is undefined for the type new View.OnClickListener(){}
為蝦米額?
代碼是在View.OnClickListener
中的,你需要調用在Activity類中的startActivity()
,不是在 View.OnClickListener
中。
所以,應該用這句:
context.startActivity(intent, ACTIVITY_EDIT);
或者這句:
((Activity) context).startActivity(intent, ACTIVITY_EDIT);