Android Studio, ImageView не там, где должно быть - PullRequest
0 голосов
/ 24 сентября 2019

Я работаю над проектом, в котором с помощью http-запроса я даю в ответ json, где, когда я его получаю, он дает мне некоторую информацию, в этой информации у меня также есть некоторый путь url img, который мне нужно использовать для создания различныхimg динамически, для их построения я использую цикл for, но вместо этого в порядке «Вид и текст -> Вид и текст -> Вид и текст -> и т. д.» - «Текст -> Текст -> Текст»-> Просмотр -> Просмотр -> Просмотр -> ".Я надеюсь, что я объяснил себе ясно.

Вот код:

@Override
        public void onResponse(JSONObject response) {
            try {
                JSONArray jsonArray = response.getJSONArray("products");


                for (int i = 0; i < jsonArray.length(); i++){
                    JSONObject products = jsonArray.getJSONObject(i);

                    String title = products.getString("title");
                    String tagline = products.getString("tagline");
                    String checkout = products.getString("product_url");

                    JSONArray urlImg = products.getJSONArray("images");
                    JSONObject imgurl = urlImg.getJSONObject(1);
                    String smallImg = imgurl.getString("large");

                    Log.d("LINK IMG", smallImg);

                    LinearLayout layout = (LinearLayout) findViewById(R.id.mainLinearLayout);
                    ImageView imgView = new ImageView(MainActivity.this);
                    imgView.setAdjustViewBounds(true);
                    imgView.setId(R.id.coverview1);
                    layout.addView(imgView);

                    tvTest.append(title + ", " + tagline + "\n" + checkout + "\n\n\n");
                    Glide.with(MainActivity.this).load(smallImg).into(imgView);


                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

и XML:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">


<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/mainLinearLayout">

    <Button
        android:id="@+id/btnTest"
        android:layout_width="409dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Test API"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/coverview1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:adjustViewBounds="true"
            android:maxWidth="20dp"
            android:scaleType="fitCenter" />

        <TextView
            android:id="@+id/tvTest"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:autoLink="web"
            android:padding="10dp"
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent" />
    </RelativeLayout>
</LinearLayout>

1 Ответ

0 голосов
/ 24 сентября 2019

Таким образом, использовать модификацию намного проще https://link.medium.com/zMv1Q6pwf0

...