0

So, I used Android - configure Spinner to use array to try and display just one field of a custom java class. My Java class is as follows (it is a part of my Google App Engine backend)

public class CropVariety {

private String varietyId;
private String varietyName;

public String getVarietyId() { return varietyId; }
public String getVarietyName() {
    return varietyName;
}

public void setVarietyId(String data) {varietyId = data; }
public void setVarietyName(String data) {
    varietyName = data;
}

public String toString()
{
    return varietyName;
}
}

and then in the activity Async task, I have the following in Post Execute

super.onPostExecute(result);
            varietyList = result;
            spinner = (Spinner) findViewById(R.id.variety_spinner);
            spinner.setSelection(0);
            dataAdapter = new ArrayAdapter(context,android.R.layout.simple_spinner_item, varietyList);
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            dataAdapter.notifyDataSetChanged();

            spinner.setAdapter(dataAdapter);
            spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

The OnItemSelectedListener is implemented as follows:

public void onItemSelected(AdapterView parent, View view, int pos, long id) {

        // String selectedItem = parent.getItemAtPosition(pos).toString();

        CropVariety selectedItem = (CropVariety) parent.getItemAtPosition(pos);

        //check which spinner triggered the listener
        switch (parent.getId()) {
            case R.id.variety_spinner:
                selectedVarietyName = selectedItem.getVarietyName();
                selectedVarietyId = selectedItem.getVarietyId();
                break;
        }

So while selectedVarietyName and selectedVarietyId get the right values, each item on the drop down list looks as follows:

{"varietyId":"sefs","varietyName":"asfd"}

I followed the link and don't know why the variety name isn't being displayed in the spinner. Thank you

Community
  • 1
  • 1

1 Answers1

0

you are downloading json Object from that link .. you have to parse th json in your doInBackground .. and store the result in a list to use it later in the postExecute here is an exemple:

class ModifierParam extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(ActivityMed.this);
            if (action.equals("rdvmed")) {
                pDialog.setMessage(getString(R.string.enrParam));
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(true);
                pDialog.show();
            }
        }


        protected String doInBackground(String... args) {
            JSONObject json; 
            List<NameValuePair> params = new ArrayList<NameValuePair>();
//here are the password and email for login
            params.add(new BasicNameValuePair("Email", email));
            params.add(new BasicNameValuePair("Mdp", mdp)); 
                json = jsonParser.makeHttpRequest(url_parametres,
                        "POST", params);
            }
            try { 
                if(json != null && !json.isNull("yourArrayName")){ 
                list.clear(); 
                        if (json.has("yourArrayName")) {
                            JSONArray ja = json.getJSONArray("yourArrayName");
                            for (int i = 0; i < ja.length(); i++) {
                                JSONObject c = ja.getJSONObject(i);
                                String varietyNames= String.valueOf(c.getInt("varietyName"));
                                String varietyId= c.getString("varietyId"));

                                //store data in the list
                                list.add(new YourObjectName(varietyName, varietyId));
                            }
                        }

                    }
                }

            }} catch (JSONException e) {
                e.printStackTrace();
            } catch (ParseException e) {
                e.printStackTrace();
            }

            return null;
        }

        protected void onPostExecute(String file_url) {
    pDialog.dismiss();
            //here you create your adapter from that list 
            // and then Spinner.setAdapter(the Adapter);
        }
    }

you need jsonParser i think you have it... if you dont here it is : http://www.androidhive.info/2012/01/android-json-parsing-tutorial/ now your data will be displayed correctly

Charaf Eddine Mechalikh
  • 1,248
  • 2
  • 10
  • 20