StartIntent()无法在自定义适配器中工作

时间:2012-11-15 16:04:30

标签: android android-intent

我为列表视图创建了一个自定义适配器,这样我就可以在我的复选框和列表中创建监听器事件。以下是我的代码:

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;
    }
} 

我得到的错误指针(startActivity(intent,ACTIVITY_EDIT);)是:

  

方法startActivity(Intent,int)未定义类型new View.OnClickListener(){}

有人可以提供相同的指示吗?

3 个答案:

答案 0 :(得分:2)

由于您的代码位于View.OnClickListener中,因此使用了错误的上下文。您需要在Activity类上调用startActivity()而不是在View.OnClickListener类上调用。

要做到这一点,你应该能够做到:

context.startActivity(intent, ACTIVITY_EDIT);

((Activity) context).startActivity(intent, ACTIVITY_EDIT);

答案 1 :(得分:0)

改为使用CustomAdapter.this.getContext().startActivity()

为什么:

要从内部匿名类引用外部实例,请使用OuterClassName.this。在您的情况下CustomAdapter.this

并且,要开始活动,您需要Context,您可以通过调用getContext()的{​​{1}}方法获取该内容。

答案 2 :(得分:0)

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

context.startActivity(mintent);