【问题标题】:Android Spinner with custom java object showing key value pairing instead of value带有自定义 java 对象的 Android Spinner 显示键值对而不是值
【发布时间】:2015-06-26 14:13:03
【问题描述】:

所以,我使用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;
}
}

然后在活动异步任务中,我在 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"}

我点击了链接,但不知道为什么微调器中没有显示品种名称。谢谢

【问题讨论】:

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


    【解决方案1】:

    您正在从该链接下载 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/ 现在您的数据将正确显示

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-28
      • 2014-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-17
      • 1970-01-01
      相关资源
      最近更新 更多