How to go through a json and autofill imageview in function of the json












-1















Hello I come to you because I don't know how I can fill imageView depending on what I get from Json, there are moments when I get 8 images to load and other times I have to load 17 images. The problem is that I don't know how to do it correctly to load as many imageViews as the Json has. I'm using the Gson and Picasso libraries.



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









share|improve this question



























    -1















    Hello I come to you because I don't know how I can fill imageView depending on what I get from Json, there are moments when I get 8 images to load and other times I have to load 17 images. The problem is that I don't know how to do it correctly to load as many imageViews as the Json has. I'm using the Gson and Picasso libraries.



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









    share|improve this question

























      -1












      -1








      -1








      Hello I come to you because I don't know how I can fill imageView depending on what I get from Json, there are moments when I get 8 images to load and other times I have to load 17 images. The problem is that I don't know how to do it correctly to load as many imageViews as the Json has. I'm using the Gson and Picasso libraries.



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









      share|improve this question














      Hello I come to you because I don't know how I can fill imageView depending on what I get from Json, there are moments when I get 8 images to load and other times I have to load 17 images. The problem is that I don't know how to do it correctly to load as many imageViews as the Json has. I'm using the Gson and Picasso libraries.



      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 19 at 14:02









      FumatamaxFumatamax

      63




      63
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You need to use Recycler View for dynamic lists.



          Here is the link to tutorial for Recycler View






          share|improve this answer
























          • but can't you make some loop that creates as many imageViews as you need?

            – Fumatamax
            Jan 19 at 15:33











          • Yes, you can but if you create so many images android will throw memory exception. Recycler view deal with dynamic list pretty well

            – makkhokher
            Jan 21 at 10:18











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54267870%2fhow-to-go-through-a-json-and-autofill-imageview-in-function-of-the-json%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You need to use Recycler View for dynamic lists.



          Here is the link to tutorial for Recycler View






          share|improve this answer
























          • but can't you make some loop that creates as many imageViews as you need?

            – Fumatamax
            Jan 19 at 15:33











          • Yes, you can but if you create so many images android will throw memory exception. Recycler view deal with dynamic list pretty well

            – makkhokher
            Jan 21 at 10:18
















          0














          You need to use Recycler View for dynamic lists.



          Here is the link to tutorial for Recycler View






          share|improve this answer
























          • but can't you make some loop that creates as many imageViews as you need?

            – Fumatamax
            Jan 19 at 15:33











          • Yes, you can but if you create so many images android will throw memory exception. Recycler view deal with dynamic list pretty well

            – makkhokher
            Jan 21 at 10:18














          0












          0








          0







          You need to use Recycler View for dynamic lists.



          Here is the link to tutorial for Recycler View






          share|improve this answer













          You need to use Recycler View for dynamic lists.



          Here is the link to tutorial for Recycler View







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 19 at 14:11









          makkhokhermakkhokher

          8911




          8911













          • but can't you make some loop that creates as many imageViews as you need?

            – Fumatamax
            Jan 19 at 15:33











          • Yes, you can but if you create so many images android will throw memory exception. Recycler view deal with dynamic list pretty well

            – makkhokher
            Jan 21 at 10:18



















          • but can't you make some loop that creates as many imageViews as you need?

            – Fumatamax
            Jan 19 at 15:33











          • Yes, you can but if you create so many images android will throw memory exception. Recycler view deal with dynamic list pretty well

            – makkhokher
            Jan 21 at 10:18

















          but can't you make some loop that creates as many imageViews as you need?

          – Fumatamax
          Jan 19 at 15:33





          but can't you make some loop that creates as many imageViews as you need?

          – Fumatamax
          Jan 19 at 15:33













          Yes, you can but if you create so many images android will throw memory exception. Recycler view deal with dynamic list pretty well

          – makkhokher
          Jan 21 at 10:18





          Yes, you can but if you create so many images android will throw memory exception. Recycler view deal with dynamic list pretty well

          – makkhokher
          Jan 21 at 10:18


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54267870%2fhow-to-go-through-a-json-and-autofill-imageview-in-function-of-the-json%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Homophylophilia

          Updating UILabel text programmatically using a function

          Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage