2017-02-15 54 views
0

我正在從微調框中獲取國家名稱,現在我想根據edittext中的微調項目設置國家代碼...但我不知道如何根據微調項目設置...如何根據android中的國家項目設置文本?

這是代碼我從微調得到國家名稱):

pmmobile = (EditText) findViewById(R.id.mob); 
private void getCountryData(){ 
     StringRequest stringRequest = new StringRequest(DATA_URL, 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 
         JSONObject j = null; 
         try { 
          Log.d("Test",response); 
          JSONArray result = new JSONArray(response); 
          //Calling method getCountry to get the Country from the JSON Array 
          getCountry(result); 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
        } 
       },new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
      }}); 
     RequestQueue requestQueue = Volley.newRequestQueue(this); 
     //Adding request to the queue 
     requestQueue.add(stringRequest); 
    } 
    private void getCountry(JSONArray jsonArrayCountry){ 
     //Traversing through all the items in the json array 
     List<Country> countries = new ArrayList<>(); 
     try { 
      String country_name, country_code; 
      JSONObject countries_object; 
      for (int i = 0; i < jsonArrayCountry.length(); i++) { 
       countries_object = jsonArrayCountry.getJSONObject(i); 
       country_code = countries_object.getString("id"); 
       country_name = countries_object.getString("Name"); 
       countries.add(new Country(country_code, country_name)); 
      } 
      ArrayAdapter countryAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, countries); 
      pmcountry.setPrompt("Select Country"); 
      pmcountry.setAdapter(countryAdapter); 
      pmcountry.setAdapter(new NothingSelectedSpinnerAdapter(countryAdapter, 
        R.layout.contact_spinner_row_nothing_selected,this)); 
      pmcountry.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
       @Override 
       public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 

       } 
       @Override 
       public void onNothingSelected(AdapterView<?> arg0) { 
       } 
      }); 

     } catch (JSONException e) { 
      Log.e("PMSearchActivity", e.getLocalizedMessage(), e); 
     } 
    } 

我想在pmmobile設置國家代碼..軟件幫助,新的android。

這是我的JSON:

[ 
    { 
    "id": "1", 
    "Name": "Afghanistan", 
    "CountryCode": "AF", 
    "CountryIso": "AFG" 
    }, 
    { 
    "id": "2", 
    "Name": "Albania", 
    "CountryCode": "AL", 
    "CountryIso": "ALB" 
    }, 

回答

1

我想,如果你想顯示String你要使用TextView,不是EditText

總之:

pmmobile.setText(<... string or string res ID ...>); 

這麼簡單。

爲了保持它的異步性,我想你應該把它放在你的聽衆中,例如onItemSelected()

更新。 不知道你在做什麼,我建議你瀏覽Locale工具類中的常量。你可以從那裏獲得所有的語言ISO代碼和你需要的便利的常量和實用程序,而不會因爲json和類似的東西而變得瘋狂。

Locale.COUNTRY.getLanguage(); 

Locale.getISOLanguages(); 

雖然我不知道這是否是你所需要的。

+0

其實我想用手機號碼撥打電話代碼,我會將其改爲撥打代碼而不是國家代碼..然後我將添加手機號碼 – user7316606

+0

http://stackoverflow.com/questions/42243097/how-to- make-linearlayout-design-in-android/42243434#42243434在這個你可以看到調用代碼..thats我想如何顯示 – user7316606