我有一个ListView
带有图片。我使用Retrofit通过URL拍摄了这些图像。现在,我正在尝试为适配器设置OnClick
,以将每张照片传递给新活动,但是我在使用意图时遇到了麻烦,我不知道必须使用哪种意图。
当我运行该应用程序时,它显示空白页面,调试器说出我的意图,我的ImageView
是NULL
。在新活动中,我仅使用一个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);
}
}
}
这是我的改造类:
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();
}
});
}
}
这是我的新活动
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);
}
}
我真的很困惑。任何建议都会很棒。
答案 0 :(得分:1)
也许尝试通过此方法获取此字符串:
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");
}