How to get Value from URL and set name in spinner

2019-07-18 13:44发布

问题:

I am new in android dev i am fetch data from URL and want to set name in spinner and send their id to server

MY JSON

{
"data": [{
    "name": "RELIANCE CDMA",
    "ID": "1"
}, {
    "name": "IDEA",
    "ID": "5"
}, {
    "name": "AIRTEL",
    "ID": "7"
}, {
    "name": "MTNL DELHI",
    "ID": "8"
}, {
    "name": "MTNL MUMBAI",
    "ID": "8"
}, {
    "name": "VODAFONE",
    "ID": "12"
}, {
    "name": "LOOP",
    "ID": "13"
}, {
    "name": "BSNL",
    "ID": "14"
}, {
    "name": "AIRCEL",
    "ID": "19"
}, {
    "name": "TATA INDICOM",
    "ID": "23"
}, {
    "name": "VIRGIN CDMA",
    "ID": "27"
}, {
    "name": "RELIANCE GSM",
    "ID": "32"
}, {
    "name": "TATA DOCOMO",
    "ID": "35"
}, {
    "name": "VIRGIN GSM",
    "ID": "36"
}, {
    "name": "MTS",
    "ID": "38"
}, {
    "name": "UNINOR",
    "ID": "41"
}, {
    "name": "VIDEOCON",
    "ID": "43"
}, {
    "name": "T24",
    "ID": "52"
}]
}

and i am using this code in android

CODE

 private class DownloadJSON extends AsyncTask<Void, Void, Void> {
    protected Void doInBackground(Void... params) {
        json_data = new ArrayList<Json_Data>();
        datalist = new ArrayList<String>();
        jsonobject = JSONfunctions
                .getJSONfromURL("http://www.myurl.com/index.php");    // example only
        Log.d("Response: ", "> " + jsonobject);
        try {
            jsonarray = jsonobject.getJSONArray("data");
            for (int i = 0; i < jsonarray.length(); i++) {
                jsonobject = jsonarray.getJSONObject(i);
                Json_Data worldpop = new Json_Data();
                worldpop.setName(jsonobject.optString("name"));
                worldpop.setId(jsonobject.optString("ID"));
                json_data.add(worldpop);
                datalist.add(jsonobject.optString("name"));
            }
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }
    protected void onPostExecute(Void args) {

        Spinner mySpinner = (Spinner)getView().findViewById(R.id.operator_spinner);
        mySpinner
                .setAdapter(new ArrayAdapter<String>(getActivity(),
                        android.R.layout.simple_spinner_dropdown_item,
                        datalist));
        mySpinner
                .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    public void onItemSelected(AdapterView<?> arg0,
                                               View arg1, int position, long arg3) {
                        String opt_code = datalist.get(position);
                    }

                    public void onNothingSelected(AdapterView<?> arg0) {
                        // TODO Auto-generated method stub
                    }
                });
    }


}

but this code only show name in spinner but not select any id form my json

回答1:

It should be:

private class DownloadJSON extends AsyncTask<Void, Void, Void> {
    protected Void doInBackground(Void... params) {
        json_data = new ArrayList<Json_Data>();
        datalist = new ArrayList<Pair<String, String>>();
        jsonobject = JSONfunctions
                .getJSONfromURL("http://www.myurl.com/index.php");    // example only
        Log.d("Response: ", "> " + jsonobject);
        try {
            jsonarray = jsonobject.getJSONArray("data");
            for (int i = 0; i < jsonarray.length(); i++) {
                jsonobject = jsonarray.getJSONObject(i);
                Json_Data worldpop = new Json_Data();
                worldpop.setName(jsonobject.optString("name"));
                worldpop.setId(jsonobject.optString("ID"));
                json_data.add(worldpop);
                datalist.add(new Pair<String, String>(jsonobject.optString("ID"), jsonobject.optString("name"));
            }
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(Void args) {
        Spinner mySpinner = (Spinner) getView().findViewById(R.id.operator_spinner);
        mySpinner
                .setAdapter(new ArrayAdapter<String>(getActivity(),
                        android.R.layout.simple_spinner_dropdown_item,
                        datalist));
        mySpinner
                .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    public void onItemSelected(AdapterView<?> arg0,
                                               View arg1, int position, long arg3) {
                        String opt_code = datalist.get(position).first;
                    }

                    public void onNothingSelected(AdapterView<?> arg0) {
                        // TODO Auto-generated method stub
                    }
                });
    }
}

But it seems to me that you don't understand what's going on in that code, so highly suggest to sort out in that.

And one more advice - don'y use AsyncTask, and take a look on Retrofit.