Android 如何在json函数中遍历json和自动填充imageview
您好,我来找您是因为我不知道如何填充Android 如何在json函数中遍历json和自动填充imageview,android,json,loops,imageview,picasso,Android,Json,Loops,Imageview,Picasso,您好,我来找您是因为我不知道如何填充imageView,这取决于我从Json获得的内容,有时我需要加载8个图像,有时我需要加载17个图像。问题是我不知道如何正确地加载imageview,就像Json一样多。我用的是格森和毕加索的图书馆 活动\u main.xml <LinearLayout android:id="@+id/linearLayat" android:layout_width="match_parent"
imageView
,这取决于我从Json获得的内容,有时我需要加载8个图像,有时我需要加载17个图像。问题是我不知道如何正确地加载imageview
,就像Json一样多。我用的是格森和毕加索的图书馆
活动\u main.xml
<LinearLayout
android:id="@+id/linearLayat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/txtFeatured"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Images" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/imgF01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@android:drawable/btn_star_big_on" />
<ImageView
android:id="@+id/imgF02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@android:drawable/btn_star_big_on" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/imgF03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@android:drawable/btn_star_big_on" />
<ImageView
android:id="@+id/imgF04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@android:drawable/btn_star_big_on" />
</LinearLayout>
</LinearLayout>
对于动态列表,您需要使用回收器视图
是指向“回收者视图”教程的链接,但您不能创建一些循环,以创建所需数量的
imageview
?是的,您可以,但如果您创建这么多图像,android将抛出内存异常。回收器视图可以很好地处理动态列表
public class MainActivity extends AppCompatActivity {
public ImageView img_0, img_1, img_2, img_3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img_0 = findViewById(R.id.imgF01);
img_1 = findViewById(R.id.imgF02);
img_2 = findViewById(R.id.imgF03);
img_3 = findViewById(R.id.imgF04);
Response response = client.newCall(request).execute();
if(response.isSuccessful()){
String response_jSon = response.body().string();
Gson gson = new Gson();
obtenerDatos obtenerDatos = gson.fromJson(response_jSon, obtenerDatos.class);
Picasso.get().load(obtenerDatos.items.get(0).item.images.information).into(img_0);
Picasso.get().load(obtenerDatos.items.get(1).item.images.information).into(img_1);
Picasso.get().load(obtenerDatos.items.get(2).item.images.information).into(img_2);
Picasso.get().load(obtenerDatos.items.get(3).item.images.information).into(img_3);
}
}
}