单击图像应在新 Activity 中打开图像

Clicking on an image should open the image in a new Activity

我有一个 ListView 图片。我使用 Retrofit URL 拍摄了这些图像。现在我正在尝试为我的适配器设置一个 OnClick 以将每张照片传递给新的 activity,但我在使用 intent 时遇到了一些问题,我不知道我必须使用哪个 intent。

当我 运行 应用程序时,它显示一个空白页面,调试器说出我的意图,我的 ImageViewNULL。在新的activity中,我只使用了一个ImageView

这是我的适配器:

public class CustomListPhotoView extends ArrayAdapter<String>{

    private List<String> img;
    private Activity context;

    public CustomListPhotoView(Activity context,  List<String> img) {

        super(context, R.layout.photo_listview,img);
        this.context = context;
        this.img = img;
    }


    @NonNull
    @Override
    public View getView(final int position, @NonNull View convertView, @NonNull ViewGroup parent)
    {
        View r = convertView;
        ViewHolder viewHolderPhoto = null;
        if(r == null) {

            LayoutInflater layoutInflater = context.getLayoutInflater();
            r = layoutInflater.inflate(R.layout.photo_listview, null, true);
            viewHolderPhoto = new ViewHolder(r);
            r.setTag(viewHolderPhoto);
        } else {

            viewHolderPhoto = (ViewHolder) r.getTag();
        }

        Picasso.with(context).load(img.get(position))
                .resize(150,200)
                .centerCrop()
                .into(viewHolderPhoto.imageView);

       viewHolderPhoto.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, DisplayPhoto.class);
                intent.putExtra("image_url", img.get(position));
                context.startActivity(intent);

            }
        });
        return r;
    }
    class ViewHolder {

        ImageView imageView;
        ViewHolder(View v) {
            imageView =  v.findViewById(R.id.imageView);
        }
    }
}

这是我的改造 class:

public class SightsPhotosActivity extends AppCompatActivity {

    private ListView listView;
    private List<String> photos = new ArrayList<>();
    private Intent intent;
    private String sight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photos);
        intent = getIntent();
        sight = intent.getStringExtra("sightName");
        listView = findViewById(R.id.sights_photos);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });

        Retrofit retrofit = RequestService.initializeRequest().build();
        SightClient client = retrofit.create(SightClient.class);
        Call<List<Photo>> call = client.repoForPhotos(sight);
        call.enqueue(new Callback<List<Photo>>() {
            @Override
            public void onResponse(Call<List<Photo>> call, Response<List<Photo>> response) {

                CustomListPhotoView customListPhotoView ;
                List<Photo> repos = response.body();
                for(Photo photo: repos) {
                    photos.add(photo.getImage());
                }
                customListPhotoView = new CustomListPhotoView(SightsPhotosActivity.this,photos);
                listView.setAdapter(customListPhotoView);
            }

            @Override
            public void onFailure(Call<List<Photo>> call, Throwable t) {
                Toast.makeText(SightsPhotosActivity.this,"Something sent wrong ,please re-try later...",Toast.LENGTH_SHORT).show();
            }
        });
    }
}

这是我的新 Activity

public class DisplayPhoto extends AppCompatActivity {
    ImageView Display;
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_photo);
        intent = getIntent();
        String Url = intent.getStringExtra("image_url");
        Display = findViewById(R.id.DisplayimageView);

        Picasso.with(this).load(Url).centerCrop().into(Display);
    }
}

我真的很困惑。任何建议都会很棒。

也许尝试通过以下方法获取此字符串:

String imgURL;
if (savedInstanceState == null) {
    Bundle extras = getIntent().getExtras();
    if(extras == null) {
        imgURL = null;
    } else {
        imgURL = extras.getString("image_url");
    }
} else {
    imgURL = (String) savedInstanceState.getSerializable("image_url");
}