2 o19830015 o19830015 于 2012.11.27 13:11 提问

StartIntent()在自定义Adapter不运行

在列表视图中创建的自定义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(){}

为虾米额?

2个回答

ce_la_vie
ce_la_vie   2012.11.27 13:36
已采纳

代码是在View.OnClickListener中的,你需要调用在Activity类中的startActivity(),不是在 View.OnClickListener中。

所以,应该用这句:

context.startActivity(intent, ACTIVITY_EDIT);

或者这句:

((Activity) context).startActivity(intent, ACTIVITY_EDIT);
Momo1199
Momo1199   2012.11.27 13:27

试试这个:

Intent mintent = new Intent(context,ReminderModificationActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

context.startActivity(mintent);
Csdn user default icon
上传中...
上传图片
插入图片