java - 如何在 ListView 中显示 JSON 结果

标签 java android json listview

我有以下代码和列表,在我的 ListView 上显示错误的结果。

公共(public)类 ZonesActivity 扩展了 AppCompatActivity {

ListView mListView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_zones);

    String myJSONString = null;
    String myJSONObject = null;

    ArrayList<Zones> allZones = new ArrayList<Zones>();

    try {
        InputStream inputStream = getAssets().open("zonesjson.json");
        int sizeOfJSONFile = inputStream.available();

        byte[] bytes = new byte[sizeOfJSONFile];

        inputStream.read(bytes);

        //close the input stream
        inputStream.close();
        myJSONString = new String(bytes, "UTF-8");
        myJSONObject = new JSONObject(myJSONString).toString();


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


    try {
        JSONObject jsonObjMain = new JSONObject(myJSONObject);

        // Creating JSONArray from JSONObject
        JSONArray jsonArray = jsonObjMain.getJSONArray("zones");

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

            // Creating JSONObject from JSONArray
            JSONObject jsonObj = jsonArray.getJSONObject(i);

            // Getting data from individual JSONObject
            String zname = jsonObj.getString("zname");
            String location = jsonObj.getString("location");
            String pastname = jsonObj.getString("pastname");
            int starttime = jsonObj.getInt("starttime");
            int endtime = jsonObj.getInt("endtime");
            String contactdetails = jsonObj.getString("contactdetails");
            String address = jsonObj.getString("address");

            Zones zonesList = new Zones();
            zonesList.setZoneName(zname);
            zonesList.setZoneLocation(location);
            zonesList.setZonePastor(pastname);
            zonesList.setZoneStartTime(starttime);
            zonesList.setZoneEndTime(endtime);
            zonesList.setZoneContactNumber(contactdetails);
            zonesList.setZoneAddress(address);

            allZones.add(zonesList);
        }


        mListView = (ListView)findViewById(R.id.zoneListView1);
        ArrayAdapter arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, allZones);
        mListView.setAdapter(arrayAdapter);

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



}

}

它在我的模拟器上显示以下结果:请协助我得到的错误

enter image description here

最佳答案

您应该创建一个自定义阵列适配器。默认的不知道如何显示实体“Zones”,因此它为每个项目显示zones.toString()。

希望这个例子对您有帮助:

public class ZonesAdapter extends ArrayAdapter<Zones> {

    public ZonesAdapter(Context context, int resource, List<Zones> objects) {
        super(context, resource, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.zones_item_layout, parent, false);
        }
        Zones item = getItem(position);
        TextView title = (TextView) convertView.findViewById(R.id.title);
        title.setText(item.getZoneName());
        // ...
        // Set other fields
        // ...
        return convertView;
    }
}

然后

ZonesAdapter arrayAdapter = new ZonesAdapter(this, R.layout.zones_item_layout, allZones);
mListView.setAdapter(arrayAdapter);

zones_item_layout.xml:

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

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    // Add other views

</LinearLayout>

关于java - 如何在 ListView 中显示 JSON 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35102783/

相关文章:

json - 如何获取ffmpeg命令生成的json的key值?

java - 数组列表错误

android - getWritableDatabase() 会影响事务吗

json - 从 .Net Core 2.1 Web API 中的 JSON DateTime 响应中删除时间组件

android - 是否可以根据国家/地区提供多个 APK 支持?

java - 在 Android Studio 中,将 Java 代码粘贴到 Kotlin 文件中不再转换为 Kotlin

c# - JSON.net:如何在不使用默认构造函数的情况下反序列化?

java - 如何在多模块 Maven 项目中配置 slf4j+logback?

java - 字符数组大小? Stringbuffer 将 HTMl 文件保存在字符串中

java - 如何在Java中减去两个列表/数组的值?