将数据设置为微调器,并在微调器中发送所选数据的密钥

时间:2019-02-18 10:18:45

标签: java android retrofit spinner

我正在使用翻新版在Android中使用其余的api从服务器获取语言列表。我必须在微调器中发送所选项目的密钥/ ID。这是Json数据。

[
  {
    "key": "ab",
    "language": "Abkhazian"
  },
  {
    "key": "aa",
    "language": "Afar"
  },
  {
    "key": "zu",
    "language": "Zulu"
  }]

我正在获取微调器中的语言数据...那样...

 Call<List<LanguageListing>> call = RetrofitClient.getInstance().getApi().getLanguages();
        call.enqueue(new Callback<List<LanguageListing>>() {
            @Override
            public void onResponse(Call<List<LanguageListing>> call, Response<List<LanguageListing>> response) {
                List<LanguageListing> languages =response.body();
                for (LanguageListing C:languages){
                    Log.d("Languages" , C.getLanguage() );
                    String[] languageName = new String[languages.size()];
                    for (int i=0 ; i<languages.size() ; i++){
                        languageName[i]= languages.get(i).getLanguage();
                    }
                    lan_spinner.setAdapter(new ArrayAdapter<String>(
                            getApplicationContext(),
                            R.layout.items_view,
                            languageName
                    ));
                }
            }
            @Override
            public void onFailure(Call<List<LanguageListing>> call, Throwable t) {
                Toast.makeText(Profile_Activity.this , t.getMessage() , Toast.LENGTH_SHORT).show();
            }
        });

当我需要更新语言时,我必须将密钥发送到服务器,但是我无法在此处获取密钥...

key = lan_spinner.getSelectedItem().toString();

从这行中,我仅能获得语言的名称。请帮助提供示例代码,谢谢。

这是我需要更新语言的代码。

 public void UpdateLanguage(){

        user_id = DatabaseUtil.getInstance().getUser().getData().getID();
        key = lan_spinner.getSelectedItem().toString();
        mRegProgress.setTitle("Updating Language");
        mRegProgress.setMessage("Please wait...");
        mRegProgress.setCanceledOnTouchOutside(false);
        mRegProgress.show();
        Call<ResponseBody> update_lan = RetrofitClient.getInstance().getApi().updateLanguage(user_id , key);
        update_lan.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                mRegProgress.dismiss();
                String s  = response.body().toString();
                Toast.makeText(Profile_Activity.this , "Language Updated Successfully" , Toast.LENGTH_LONG).show(); }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Toast.makeText(Profile_Activity.this ,  "There is some Error in updating your languages..." , Toast.LENGTH_SHORT).show();
            }
        });
    }

这是我的模型课...

public class LanguageListing {
    @SerializedName("key")
    @Expose
    private String key;
    @SerializedName("language")
    @Expose
    private String language;

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }
}

3 个答案:

答案 0 :(得分:0)

使用spinner.getSelectedItem()从微调器中获取所选项目,并将其存储在变量selectedString中,您已经在代码中获取了该变量。

检查主列表中的字符串。如果有字符串,您也可以获取其ID。

for(LanguageListing e : datalist) {
   if (e.getLanguage().contains(selectedString)) {
       String sel_id = e.getKey();
       break;
   }
}

答案 1 :(得分:0)

LanguageListing在此模型类名称中定义了两个字段,即键名和语言名。 您将setOnItemSelectedListener应用于微调器。

spinn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {


                String keyNameStr = (yourlist.get(i).getKeyName());

            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

请尝试它会为您提供帮助。

答案 2 :(得分:0)

尝试一下

ArrayList<LanguageListing > arrayList;
ArrayAdapter<LanguageListing> adapter;

 call.enqueue(new Callback<ResponseBody>() {
    @Override
    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
        if (response != null) {
            if (response.code() == 200) {
                try {
                    String responseValue = response.body().string();
                    if (responseValue.length() > 0) {
                        JSONArray jsonArray = new JSONArray(responseValue);
                        if (jsonArray != null) {
                            for (int i = 0; i < jsonArray.length(); i++) {                                                  
                                LanguageListing obj = new LanguageListing ();
                                obj.setKey(jsonArray.getJSONObject(i).getString("key"));
                                obj.setLanguage(jsonArray.getJSONObject(i).getString("language"));
                                arrayList.add(obj);
                            }
                            adapter = new ArrayAdapter<LanguageListing>(this, android.R.layout.simple_spinner_item, list);
                            adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
                            spinner.setAdapter(adapter);
                        }
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Override
    public void onFailure(Call<ResponseBody> call, Throwable t) {
    }
});

您可以从selectedItem获取spinner语言的键,如下所示。

@OnItemSelected({R.id.yourSpinner})
public void spinnerItemSelected(Spinner spinner, int position) {
switch (spinner.getId()) {
    case R.id.yourSpinner:
        LanguageListing  langKeyvalue = (LanguageListing ) yourSpinner.getSelectedItem();
        String key = langKeyvalue.getKey(); //here you will get selectedItem key
        String language = langKeyvalue.getLanguage();
        break;
        }
}