如何将引用可绘制图像的 json 文件加载到水平滚动视图中?

How to load json file that refers to drawable images into a horizontal scroll view?

我是 android 工作室的新手,我的大部分工作都是参考 Stack Overflow 回答的问题和缩短问题的主题完成的。

我的JSON是这样的:

[ 
       { "name":"station1",
         "url":"http://example1.com",
         "image":"R.drawable.radio1"
        },
        { "name":"station2",
          "url":"example2.com",
          "image":"R.drawable.radio2"
        }
]

等等, 我的 XML 是

<HorizontalScrollView
    android:id="@+id/horizontalScrollView1"
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    tools:layout_editor_absoluteY="0dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <RelativeLayout
            android:id="@+id/relative1"
            android:layout_width="100dp"
            android:layout_height="100dp"></RelativeLayout>


    </LinearLayout>
</HorizontalScrollView>

每次我向 JSON 文件添加另一个 "name and image" 时,我都需要在动态创建的滚动视图中加载图像和名称请有人能帮我提供代码(下面的文字加载的每张图片)。

首先,您需要更改 ListView 中的 Horizo​​ntalScrollView 吗 https://developer.android.com/guide/topics/ui/layout/listview.html , 如:

<LinearLayout
            android:id="@+id/fullscreen_content_controls"
            style="?metaButtonBarStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            tools:ignore="UselessParent">

            <ListView
                android:id="@+id/lstView"
                android:layout_margin="5dp"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:divider="@android:color/transparent"
                android:dividerHeight="10.0sp">
            </ListView>
        </LinearLayout>

当您在 MainClass 中添加此代码以使用 ListView 时:

ListView listView = findViewById(R.id.lstView);
listView.setAdapter(new CustomAdapter(this, lst));

之后,在CustomAdapter.java,需要:

public class CustomAdapter extends BaseAdapter {

    public CustomAdapter(MainClass mainActivity, List<?> iLst) {
        lst = iLst;
        context=mainActivity;
        inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View rowView;
        rowView = inflater.inflate(R.layout.listview_custom, null); <-- RAPPRESENTE A SINGLE ROW LAYOUT

        ImageView img = (ImageView) rowView.findViewById(R.id.customImg);
        TextView text =(TextView) rowView.findViewById(R.id.customText);

        return rowView;
    }
}

最后,您为单行添加 XML 布局,它会自动为每一行复制:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/border_radius"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="1"
        android:orientation="horizontal">
        <LinearLayout
            android:layout_width="0sp"
            android:layout_weight=".2"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center">
            <ImageView
                android:id="@+id/customImg"
                android:layout_width="50sp"
                android:layout_height="45sp"
                android:drawableTint="@color/White"
                android:background="@drawable/upload"
                android:tint="@color/White"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="0sp"
            android:layout_weight=".6"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:id="@+id/customText"
                android:paddingTop="5dp"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="20dp"
                android:ellipsize="none"
                android:scrollHorizontally="false"
                android:maxLines="100"
                android:textColor="@color/White"
                android:text="Title" />

        </LinearLayout>
    </LinearLayout>
</LinearLayout>

首先我建议像这样创建一个 class BEAN:

public class MyBean{
String name;
String url;
String image;
}

现在,正如 GrIsHu 在他们的回答中所描述的那样 here

您需要使用以下代码从资产文件中读取 Json 文件:

public String loadJSONFromAsset() {
    String json = null;
    try {
        InputStream is = getActivity().getAssets().open("yourfilename.json");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        json = new String(buffer, "UTF-8");
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return json;
}

之后,将Json字符串转换为Json数组,如下所示:

JSONArray ja = new JSONArray(loadJSONFromAsset());

现在您可以填充

List<MyBeen.class> lst 

像这样:

for (int i = 0; i < ja.length(); i++) {
                lst.add(gSon.fromJson(ja.get(i).toString(), MyBeen.class));
        }