带有自定义java对象的Android Spinner显示键值配对而不是值

时间:2015-04-20 01:30:57

标签: android google-app-engine android-arrayadapter android-spinner google-cloud-endpoints

因此,我使用Android - configure Spinner to use array来尝试显示自定义java类的一个字段。我的Java类如下(它是我的Google App Engine后端的一部分)

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;
}
}

然后在活动Async任务中,我在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());

OnItemSelectedListener实现如下:

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;
        }

因此,当selectedVarietyName和selectedVarietyId获得正确的值时,下拉列表中的每个项目如下所示:

{ “varietyId”: “SEFS”, “varietyName”: “ASFD”}

我跟着链接,不知道为什么品种名称没有在微调器中显示。谢谢

1 个答案:

答案 0 :(得分:0)

你正在从该链接下载json对象..你必须解析你的doInBackground中的json ..并将结果存储在列表中以便稍后在postExecute中使用它是一个例子:

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

你需要jsonParser我认为你拥有它...如果你不在这里它是: http://www.androidhive.info/2012/01/android-json-parsing-tutorial/ 现在您的数据将正确显示