gpt4 book ai didi

java - 将数据从 JSON ArrayList 加载到微调器中

转载 作者:行者123 更新时间:2023-11-29 04:53:23 25 4
gpt4 key购买 nike

我似乎无法弄明白,因为我只是这个 android 东西的初学者。

我的应用通过 JSON 从我的 WebAPI 获取客户名称。

现在我正尝试将它加载到微调器中,但我如何才能将我的 JSON arraylist 加载到其中?

我尝试在其中加载 custTable,但它现在在微调器中显示“com.jetron.jetronbuitendienst.CustomerDetailsTable.....”。

我的代码:

  package com.jetron.jetronbuitendienst;

import android.annotation.TargetApi;
import android.os.AsyncTask;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class Gegevens extends Main {

String Naam;
Spinner spCustomers;
private ArrayList<CustomerDetailsTable> Klanten;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gegevens);
new AsyncLoadCustDetails().execute();
spCustomers = (Spinner) findViewById(R.id.spKlanten);

}

/**
* Set up the {@link android.app.ActionBar}, if the API is available.
*/

protected class AsyncLoadCustDetails extends
AsyncTask<Void, JSONObject, ArrayList<CustomerDetailsTable>> {
ArrayList<CustomerDetailsTable> custTable = null;

@Override
protected ArrayList<CustomerDetailsTable> doInBackground(Void... params) {
// TODO Auto-generated method stub

RestAPI api = new RestAPI();
try {

JSONObject jsonObj = api.GetCustomerDetails();

JSONParser parser = new JSONParser();

custTable = parser.parseCustomerDetails(jsonObj);

Log.d("Customers: ", jsonObj.toString());

} catch (Exception e) {
// TODO Auto-generated catch block
Log.d("AsyncLoadCustDetails", e.getMessage());

}

return custTable;
}

@Override
protected void onPostExecute(ArrayList<CustomerDetailsTable> result) {
// TODO Auto-generated method stub

// Application of the Array to the Spinner
ArrayAdapter<CustomerDetailsTable> spinnerArrayAdapter = new ArrayAdapter<CustomerDetailsTable>(getApplicationContext(), android.R.layout.simple_spinner_item, custTable);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
spCustomers.setAdapter(spinnerArrayAdapter);
}

}
}

这是 jsonObj:D/Customers::{"Successful":true,"Value":[{"Naam":"Google"},{"Naam":"Apple"},{"Naam":"AVN"},{"Naam":"Bemd"}]}

最佳答案

好吧,既然你得到了这个:

D/Customers:: {"Successful":true,"Value":[{"Naam":"Google"},{"Naam":"Apple"},{"Naam":"AVN"},{"Naam":"Bemd"}]}

这意味着您有一个名为 Value 的数组。所以你可以做的是:

声明公共(public)变量:

private JSONObject jsonChildNode;
private JSONArray jsonMainNode;
private String name;

然后将您的代码修改为:

try {
JSONObject jsonObj = api.GetCustomerDetails();
JSONParser parser = new JSONParser();
custTable = parser.parseCustomerDetails(jsonObj);
Log.d("Customers: ", jsonObj.toString());
jsonMainNode = jsonObj.optJSONArray("Value");
for (int i = 0; i < jsonMainNode.length(); i++) {
jsonChildNode = jsonMainNode.getJSONObject(i);
name = jsonChildNode.optString("Naam");
}
//and then you can add the name to a List<String> which will contain all the values of each item in the Value JSON Array.
} catch (Exception e) {
// TODO Auto-generated catch block
Log.d("AsyncLoadCustDetails", e.getMessage());

}

希望对您有所帮助!!!

关于java - 将数据从 JSON ArrayList 加载到微调器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34612460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com