【发布时间】:2017-06-28 07:53:24
【问题描述】:
我正在从 JSONArray 解析键 name 的值,并且必须填充到 Android 活动的 list view 中。
我有 JSON 树为
{
"items": [
{
"name": "accounts",
"links": [
{
"rel": "canonical",
"href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/accounts"
}
]
},
{
"name": "analyticsReports",
"links": [
{
"rel": "canonical",
"href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/analyticsReports"
}
]
},
{
"name": "answers",
"links": [
{
"rel": "canonical",
"href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/answers"
}
]
},
{
"name": "assets",
"links": [
{
"rel": "canonical",
"href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/assets"
}
]
},
{
"name": "assetStatuses",
"links": [
{
"rel": "canonical",
"href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/assetStatuses"
}
]
},
{
"name": "channelTypes",
"links": [
{
"rel": "canonical",
"href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/channelTypes"
}
]
},
{
"name": "configurations",
"links": [
{
"rel": "canonical",
"href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/configurations"
}
]
}
]
}
我正在使用for loop提取name的值:
for (int i = 0; i < parentArr.length(); i++) {
JSONObject finalObj = parentArr.getJSONObject(i); //Getting each single object from JSON array Items
String nameKey = finalObj.getString("name"); //Getting value of key(name) from each JSON Object
System.out.println("name : "+nameKey);
HashMap<String, String> data = new HashMap<>(); // tmp hash map for single data
data.put("name",nameKey); //Adding each value of name to Hashmap key => value
dataList.add(data); //adding data to the datalist
}
这里dataList -> ArrayList
我正在使用Sysout 在Android Monitor 控制台中获取所有name。
要查看这些names,我使用以下代码:
ListAdapter adapter = new SimpleAdapter(MainActivity.this, dataList,
R.layout.list_item, new String[]{"name"}, new int[]{R.id.name});
lv.setAdapter(adapter);
这里lv -> ListView
这是我的activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.spectrum_developer.resturldemo.MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list_view"
tools:layout_editor_absoluteX="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp">
</ListView>
</ScrollView>
</RelativeLayout>
还有list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:textColor="@color/colorPrimaryDark"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
但在模拟器中,我只获得了前一个值。
我必须展示我在android studio 的console 中得到的所有值。
谁能纠正我在这里犯的错误。
【问题讨论】:
-
您可以上下滚动吗?如果是,请调整单独的布局高度以包裹内容。
-
发布您的活动和适配器代码
-
@Sanoop 是
wrap_content本身。我也添加了activity。请调查一下。 -
@EKN 适配器代码已发布,活动代码已添加。帮帮我。
标签: java android json listview arraylist