程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> android-StartIntent()在自定義Adapter不運行

android-StartIntent()在自定義Adapter不運行

編輯:編程綜合問答
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(){}

為蝦米額?

最佳回答:


代碼是在View.OnClickListener中的,你需要調用在Activity類中的startActivity(),不是在 View.OnClickListener中。

所以,應該用這句:

context.startActivity(intent, ACTIVITY_EDIT);

或者這句:

((Activity) context).startActivity(intent, ACTIVITY_EDIT);
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved