Как пройти через json и автоматически заполнить изображение в функции json - PullRequest
0 голосов
/ 19 января 2019

Здравствуйте, я прихожу к вам, потому что я не знаю, как я могу заполнить imageView в зависимости от того, что я получаю от Json , бывают моменты, когда я получаю 8 изображений для загрузки, а в другое время у меня естьзагрузить 17 изображений.Проблема в том, что я не знаю, как сделать это правильно, чтобы загрузить столько imageViews, сколько Json .Я использую библиотеки 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);
}
 }
}

1 Ответ

0 голосов
/ 19 января 2019

Вам нужно использовать Recycler View для динамических списков.

Здесь - ссылка на учебник для Recycler View

...