【问题标题】:How to go through a json and autofill imageview in function of the json如何通过 json 并在 json 的功能中自动填充 imageview
【发布时间】:2019-06-13 12:55:45
【问题描述】:

您好,我来找您是因为我不知道如何填写 imageView,这取决于我从 Json 获得的信息,有时我会加载 8 张图片,而其他时候我必须加载 17 张图像。问题是我不知道如何正确加载与 Json 一样多的imageViews。我正在使用 Gson 和 Picasso 库。

activity_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>

.MainActivity

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);
}
 }
}

【问题讨论】:

    标签: android json loops imageview picasso


    【解决方案1】:

    动态列表需要使用 Recycler View。

    Here 是 Recycler View 教程的链接

    【讨论】:

    • 但是你不能做一些循环来创建你需要的imageViews吗?
    • 是的,你可以,但如果你创建了这么多图像,android 会抛出内存异常。回收站视图很好地处理动态列表
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-08
    • 1970-01-01
    • 2021-09-22
    • 2013-11-21
    • 2021-12-30
    • 2019-01-02
    相关资源
    最近更新 更多