0

我在使用 OnItemClickListener 的通知代码中有 3 个错误我需要应用,当单击一个项目时它显示通知这里是代码:

    list.setOnItemClickListener(new OnItemClickListener(){

        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // TODO Auto-generated method stub


            Bundle programNum = getIntent().getExtras();
            final String progNum = programNum.getString("ProgNum");
            final String dayNum = programNum.getString("DayNum");

            final List<TouringPrograms> startTime = datasource.getTouringProgramsStartTime(progNum, dayNum);
            final List<TouringPrograms> endTime = datasource.getTouringProgramsEndTime(progNum, dayNum);

            Intent intent = new Intent(this, ProgramsList2.class);
            PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
            String body = (String) ((TextView)parent.getChildAt(position)).getText();
            String title = "Egypt On The Go";
            String time = body + "\n start at:" + startTime.get(position)+ "\n end at:" + endTime.get(position);
            Notification n = new Notification(R.drawable.egypt, time, System.currentTimeMillis());
            n.setLatestEventInfo(this, title, time, pi);
            n.defaults = Notification.DEFAULT_ALL;
            nm.notify(uniqueID, n);
            //String time1 = "" + System.currentTimeMillis();
            //Toast.makeText(this, time1, Toast.LENGTH_SHORT).show();
            //finish();

        }});

3错误:

1.Intent 意图 = new Intent(this, ProgramsList2.class);

2.PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);

3.n.setLatestEventInfo(this, title, time, pi);

请问有什么帮助吗?

4

1 回答 1

2

错误 1

Intent intent = new Intent(this, ProgramsList2.class);

this是一个类型的项目OnItemClickListener。您应该在调用final Context intentContext = (Context) this; 之前setOnItemClickListener通过声明来传递它。然后,使用:

Intent intent = new Intent(intentContext, ProgramsList2.class);

永远记得跟踪您的Context物品;它们对于这样的事情很重要(Intents、资源、资产等)。

错误 2

PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);

和上面的问题完全一样。this不是类型Context。使用与上述相同的修复程序。

错误 3

n.setLatestEventInfo(this, title, time, pi);

在这里,与上述两个问题相同。当您this应该使用Context. 与上述两个相同的修复。

概括

this使用匿名类时跟踪您的 s 。对于将来,发布编译错误(代码行以及错误本身的文本)对那些提供答案的人来说非常有帮助。

于 2012-06-23T23:03:29.553 回答