Spinner 中未显示不同的 jsonobject 字符串

Different string of jsonobject not displaying in Spinner

我有以下回复,我正在尝试将每个 jsonobject 的日期显示到我的微调器,但我的微调器是空的,有人能告诉我我犯了什么错误吗?

以下是我的代码片段

[

    {
        "friend_id": "1",
        "user_spoc_dob": "1993-11-11",
        "status": true
    },
    {
        "friend_id": "1",
        "user_mother_dob": "1974-12-12",
        "status": false
    },
    {
        "friend_id": "1",
        "user_father_dob": "1972-11-19",
        "status": false
    },
    {
        "friend_id": "1",
        "user_dob": "1994-02-20",
        "status": true
    }
]

MainActivity.java

 protected ArrayList<HashMap<String, String>> doInBackground(
                String... args) {
            ServiceHandler sh = new ServiceHandler();
            // Making a request to url and getting response
            statedata = new ArrayList<HashMap<String, String>>();
            String jsonStr = sh.makeServiceCall(STATE_URL, ServiceHandler.GET);
            Log.d("Response: ", "> " + jsonStr);
            if (jsonStr != null) {
                try {
                   jsonObj = new JSONArray(jsonStr);
                    for (int i = 0; i < jsonObj.length(); i++) {
                        JSONObject c = jsonObj.getJSONObject(i);
                        String wifebday=c.getString("user_spoc_dob");
                        String mothersbday=c.getString("user_mother_dob");
                        String fatherbday=c.getString("user_father_dob");
                        String ownbbday=c.getString("user_dob");
                        System.out.println("Bday" + wifebday + mothersbday + fatherbday + ownbbday);
                        HashMap<String, String> map = new HashMap<String, String>();
                        //map.put(USER_STATUSS, c.getString(USER_STATUSS));

                        map.put(wifebday,c.getString(wifebday));
                        map.put(mothersbday,c.getString(mothersbday));
                        map.put(fatherbday,c.getString(fatherbday));
                        map.put(ownbbday,c.getString(ownbbday));
                        statedata.add(map);
                    }
                    System.out.println("WifeBday"+statedata.size());
                   /* JSONObject test=jsonObj.getJSONObject(0);
                    String wifebday=test.getString("user_spoc_dob");
                    System.out.println("WifeBday"+wifebday);
                    JSONObject mothers=jsonObj.getJSONObject(1);
                    String mothersbday=test.getString("user_mother_dob");
                    System.out.println("MotherBday"+mothersbday);
                    JSONObject father=jsonObj.getJSONObject(2);
                    String fatherbday=test.getString("user_father_dob");
                    System.out.println("FatherBday"+fatherbday);
                    JSONObject ownb=jsonObj.getJSONObject(3);
                    String ownbbday=test.getString("user_dob");
                    System.out.println("UserBaday"+ownbbday);*/
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }
            return statedata;
        }

        protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
            super.onPostExecute(result);
            pDialog.dismiss();
            arrallstates = new String[statedata.size()];
            for (int index = 0; index < statedata.size(); index++) {
                HashMap<String, String> map = statedata.get(index);
               // arrallstates[index] = map.get(USER_STATUSS);
            }
            // pass arrConuntry array to ArrayAdapter<String> constroctor :
            adapterallstates = new ArrayAdapter<String>(
                    TestSpinner.this,
                    android.R.layout.simple_spinner_dropdown_item, arrallstates);
            System.out.println("adpttest"+adapterallstates);
            spiner.setAdapter(adapterallstates);
        }
    }

虽然您只需要一个日期列表,但我建议您使用 String 列表而不是 Map 列表。

这样试试:

protected ArrayList<String> doInBackground(
            String... args) {
        ServiceHandler sh = new ServiceHandler();
        // Making a request to url and getting response
        statedata = new ArrayList<String>();
        String jsonStr = sh.makeServiceCall(STATE_URL, ServiceHandler.GET);
        Log.d("Response: ", "> " + jsonStr);
        if (jsonStr != null) {
            try {
               jsonObj = new JSONArray(jsonStr);
                for (int i = 0; i < jsonObj.length(); i++) {
                    JSONObject c = jsonObj.getJSONObject(i);
                    String wifebday= (c.has("user_spoc_dob")) ? c.getString("user_spoc_dob") : null;
                    String mothersbday= (c.has("user_mother_dob")) ? c.getString("user_mother_dob") : null;
                    String fatherbday= (c.has("user_father_dob") ? c.getString("user_father_dob") : null;
                    String ownbbday= (c.has("user_dob")) ? c.getString("user_dob") : null;
                    System.out.println("Bday" + wifebday + mothersbday + fatherbday + ownbbday);

                    // if test, to avoid adding null values
                    if(wifebday!=null) statedata.add(wifebday);
                    if(mothersbday!=null) statedata.add(mothersbday); 
                    if(fatherbday!=null) statedata.add(fatherbday); 
                    if(ownbbday!=null) statedata.add(ownbbday); 
                }
                System.out.println("WifeBday"+statedata.size());
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }
        return statedata;
    }

    protected void onPostExecute(ArrayList<String> result) {
        super.onPostExecute(result);
        pDialog.dismiss();
        arrallstates = new String[statedata.size()]; // you can also use "result" array instead
        for (int index = 0; index < statedata.size(); index++) {
           arrallstates[index] = statedata.get(index);// or result.get(index);
        }
        // pass arrConuntry array to ArrayAdapter<String> constroctor :
        adapterallstates = new ArrayAdapter<String>(
                TestSpinner.this,
                android.R.layout.simple_spinner_dropdown_item, arrallstates);
        System.out.println("adpttest"+adapterallstates);
        spiner.setAdapter(adapterallstates);
    }
}

试试这个。

首先创建一个ArrayList

ArrayList<String> dateList =new  ArrayList<String>();

doInBackground,这样做,

if (result != null) {
            try {
                JSONArray array = new JSONArray(result);
                for (int i = 0; i < array.length(); i++) {
                    JSONObject c = array.getJSONObject(i);
                    String date = c.getString("user_spoc_dob");
                    Log.e("", "TAG : - " + id);
                    dateList.add(date);
                }
            } catch (Exception e) {
                Log.e("", "Home Exception : " + e.toString());
            }

获取日期后,写在onPostExecute方法中

 adapterallstates = new ArrayAdapter<String>(
                TestSpinner.this,
                android.R.layout.simple_spinner_dropdown_item, dateList);

        spiner.setAdapter(adapterallstates);

一定能解决你的问题

快乐编码,乐于助人。

a supposed JSON input, after all not everyone has friends with subsequent Date of Birth, ROFL

[
  {
    "friend_id": "1",
    "user_spoc_dob": "1993-11-10",
    "status": true
  },
  {
    "friend_id": "1",
    "user_spoc_dob": "1993-11-11",
    "status": true
  },
  {
    "friend_id": "1",
    "user_spoc_dob": "1993-11-12",
    "status": true
  },
  {
    "friend_id": "1",
    "user_spoc_dob": "1993-11-13",
    "status": true
  },
  {
    "friend_id": "1",
    "user_spoc_dob": "1993-11-14",
    "status": true
  },
  {
    "friend_id": "1",
    "user_spoc_dob": "1993-11-15",
    "status": true
  },
  {
    "friend_id": "1",
    "user_spoc_dob": "1993-11-16",
    "status": true
  },
  {
    "friend_id": "1",
    "user_spoc_dob": "1993-11-17",
    "status": true
  },
  {
    "friend_id": "1",
    "user_spoc_dob": "1993-11-18",
    "status": true
  },
  {
    "friend_id": "1",
    "user_spoc_dob": "1993-11-19",
    "status": true
  }
]

Data.java to get the JSON String

import com.google.gson.annotations.SerializedName;

public class Data {

    @SerializedName("friend_id")
    private String friend_id;

    @SerializedName("user_spoc_dob")
    private String user_spoc_dob;

    @SerializedName("status")
    private boolean status;

    public String getFriend_id() {
        return friend_id;
    }

    public void setFriend_id(String friend_id) {
        this.friend_id = friend_id;
    }

    public String getUser_spoc_dob() {
        return user_spoc_dob;
    }

    public void setUser_spoc_dob(String user_spoc_dob) {
        this.user_spoc_dob = user_spoc_dob;
    }

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return ""+user_spoc_dob;
    }
}

for your activity i have written a test code, you can customize it for desired result you want to see

 ArrayList<Data> datas = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            Data data = new Data();
            data.setFriend_id("1");
            data.setUser_spoc_dob("1993-11-1" + i);
            data.setStatus(true);
            datas.add(data);
        }
         // you can understand that i don't know which link you are calling to generate your `JSON`, so i decided to create my own `JSON` data, in which you will learn what `GSON` library can do for you. Due to which you can say Google has made data interchange quite easy for everyone. 

        /**
         * This code will convert your ArrayList object to a equivalent JSON
         * String, 
         */
        String result = (new Gson()).toJson(datas);
        Log.d("TAG", ""+result);

        /**
         * This code will convert your JSON String to a equivalent ArrayList
         * Object
         */
        ArrayList<Data> datas1 = (new Gson()).fromJson(result, new TypeToken<ArrayList<Data>>() {}.getType());



        two_Spinner = (Spinner) findViewById(R.id.SpinnerOneActivity_two_spinner);
        ArrayAdapter<Data> adapter = new ArrayAdapter<Data>(this, android.R.layout.simple_list_item_1, datas1);
        two_Spinner.setAdapter(adapter);

I don't even know how to tell you this code runs, so decided to put a screenshot of the emulator.

必需品GSON图书馆