我正在使用此代码为每个新项目创建新的侦听器

首先是xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_row_selector"
android:padding="8dp"
android:id="@+id/showonmap"
android:alpha="0.9">

<!-- Thumbnail Image -->

<!-- notofocation  Title -->
<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="15dp"
    android:textStyle="bold" />

<!--   -->
<TextView
    android:id="@+id/rating"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/title"
    android:layout_marginTop="1dip"
    android:textSize="@dimen/rating" />

<!-- Genre -->
<TextView
    android:id="@+id/genre"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/rating"
    android:layout_marginTop="5dp"
    android:textColor="@color/genre"
    android:textSize="@dimen/genre" />

<!-- date -->
<TextView
    android:id="@+id/releaseYear"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:textColor="@color/year"
    android:textSize="@dimen/year" />




我只是解析JSON数组响应并使新项取决于数组长度

之后,我将侦听器添加到


..具有id showonmap的RelativeLayout


然后单击它打开新活动
Onclick的代码

showonmaps.setOnClickListener(new View.OnClickListener() {


        public void onClick(View view) {


            Intent intent = new Intent(context, show_notification.class);
            intent.putExtra("lat", m.getGenre().get(0));
            intent.putExtra("lang", m.getGenre().get(1));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


            context.startActivity(intent);





        }
    });


我在这里使用

  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  context.startActivity(intent);


以及何时使用

Intent intent = new Intent(context,
                            show_notification.class);
                    context.startActivity(intent);


该应用程序停止了,所以我必须返回并使用

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  context.startActivity(intent);


但是当我使用它并打开新活动并按返回按钮时,它退出应用程序的任何想法或解决方案?

最佳答案

我通过将活动和上下文传递给班级来解决它

该代码将是

 Intent intent = new Intent(context, show_notification.class);
activity.startActivity(yourIntent);


上下文和活动就是您在构造类时通过的内容

这个问题帮助我找到了选择

here

关于java - android adapter Onclick打开新 Activity ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36980970/

10-08 23:41