我正在从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
- &gt; ArrayList
我使用name
在Android Monitor
控制台中获取所有Sysout
。
要查看这些names
我正在使用以下代码:
ListAdapter adapter = new SimpleAdapter(MainActivity.this, dataList,
R.layout.list_item, new String[]{"name"}, new int[]{R.id.name});
lv.setAdapter(adapter);
此处lv
- &gt; 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>
但是在模拟器中,我只得到前一个值。
我必须在console
的{{1}}中显示所有值。
任何人都可以纠正我在这里犯的错误。
答案 0 :(得分:3)
我已检查过您的代码并使用您的代码段制作示例项目。它运作正常。您的代码没有问题。
我使用了Recycleview而不是列表视图(它是高级的)。请参考以下代码。这将对您有所帮助。
<强> MainActivity.java 强>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> arrayList = new ArrayList<>();
//Change it to read from json
arrayList.add("accounts");
arrayList.add("analyticsReports");
arrayList.add("answers");
arrayList.add("assets");
arrayList.add("assetStatuses");
arrayList.add("channelTypes");
arrayList.add("configurations");
SampleAdapter adapter = new SampleAdapter(arrayList, MainActivity.this);
RecyclerView rv;
rv = (RecyclerView) findViewById(R.id.list_view);
rv.setHasFixedSize(true);
rv.setLayoutManager(new LinearLayoutManager(MainActivity.this));
rv.setAdapter(adapter);
}
}
<强> SampleAdapter.java 强>
class SampleAdapter extends RecyclerView.Adapter<SampleAdapter.ViewHolder> {
private Activity mContext;
private ArrayList<String> arrayList = new ArrayList<>();
SampleAdapter(ArrayList<String> arrayList, Activity context) {
mContext = context;
this.arrayList = arrayList;
}
@Override
public SampleAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(SampleAdapter.ViewHolder holder, final int position) {
holder.tvAward.setText(arrayList.get(position));
}
@Override
public int getItemCount() {
return arrayList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView tvAward;
ViewHolder(View itemView) {
super(itemView);
tvAward = (TextView) itemView.findViewById(R.id.name);
}
}
}
答案 1 :(得分:1)
我得到了Selvin建议的答案。
我刚刚从@{List<Object> Special = new List<Object>() { "3/12/2017", "3/18/2017" };}
删除了ScrollView代码,我得到了以下输出。
非常感谢。
答案 2 :(得分:0)
尝试修改for循环以将名称的值提取到
List<String> dataList = new ArrayList();
for (int i = 0; i < parentArr.length(); i++) {
JSONObject finalObj = parentArr.getJSONObject(i);
String nameKey = finalObj.getString("name");
System.out.println("name : "+nameKey);
dataList.add(nameKey);
}
我认为没有必要在每个hashMap中添加每个nameKey。只需一个包含所有nameKey的字符串列表就足以输入适配器。