我有两个json数组(cast,crew),我正在使用此代码将第一个json充气到我的HorizontalScrollView中。
JSONArray casts = castImageResponse.getJSONArray("cast");
String image = null;
String name = null;
if (casts.length() > 0) {
for (int i = 0; i <= casts.length(); i++) {
JSONObject cast = casts.getJSONObject(i);
image = cast.getString("profile_path");
name = cast.getString("name");
View view = mInflater.inflate(R.layout.index_actors_gallery,mGallery, false);
ImageView img = view.findViewById(R.id.id_index_actors_image);
Picasso.get().load("https://image.tmdb.org/t/p/h632"+image).into(img);
TextView txt = view.findViewById(R.id.id_index_actors_name);
txt.setText(name);
mGallery.addView(view);
}
}
当我在第二个代码中使用此代码时,它不起作用,只有第一个代码有效。 我想我需要在第一个方法完成后“仅”开始第二个方法。 仅在第一个完成后如何执行第二个?
答案 0 :(得分:0)
尝试使用if条件嵌套
JSONArray casts = castImageResponse.getJSONArray("cast");
String image = null;
String name = null;
if (casts.length() > 0) {
for (int i = 0; i <= casts.length(); i++) {
JSONObject cast = casts.getJSONObject(i);
image = cast.getString("profile_path");
name = cast.getString("name");
View view = mInflater.inflate(R.layout.index_actors_gallery,mGallery, false);
ImageView img = view.findViewById(R.id.id_index_actors_image);
Picasso.get().load("https://image.tmdb.org/t/p/h632"+image).into(img);
TextView txt = view.findViewById(R.id.id_index_actors_name);
txt.setText(name);
mGallery.addView(view);
if(i==casts.length()){ //for starting only after the first one finishes
JSONArray crews = castImageResponse.getJSONArray("crew");
String image = null;
String name = null;
if (crews.length() > 0) {
for (int j = 0; j <= crews.length(); j++) {
//code for cast repeated in crews
JSONObject crews = casts.getJSONObject(i);
image = crews.getString("profile_path");
name = crews.getString("name");
View view = mInflater.inflate(R.layout.index_actors_gallery,mGallery, false);
ImageView img = view.findViewById(R.id.id_index_actors_image);
Picasso.get().load("https://image.tmdb.org/t/p/h632"+image).into(img);
TextView txt = view.findViewById(R.id.id_index_actors_name);
txt.setText(name);
mGallery.addView(view);
}
}
}
}
}
答案 1 :(得分:0)
谢谢大家,我只是将主题放在单独的try / coach中,并且可以正常工作。