我似乎无法弄清楚,因为我只是这个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的数组。因此,您可以执行以下操作:

声明公共变量:

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());

    }


希望能帮助到你!!!

10-05 18:10