[英]android get two jsonArray and inflate them
I have two json array (cast,crew) and I am using this code to inflate first one to my HorizontalScrollView. 我有两个json数组(cast,crew),并且我正在使用此代码将第一个膨胀到我的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);
}
}
When I am using this code for the second one, it's not working and only the first one works. 当我在第二个代码中使用此代码时,它不起作用,只有第一个代码有效。 I think I need the second method start "only" after the first one finish. 我想我需要在第一个方法完成后“仅”开始第二个方法。 How do I execute second one only after first one finish? 如何仅在第一个完成后执行第二个?
Try nesting with an if condition 尝试使用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);
}
}
}
}
}
谢谢大家,我只是将主题放在单独的try / coach中,并且可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.