2017-02-15 8 views
0

나는안드로이드에서 국가 항목에 따라 텍스트를 설정하는 방법은 무엇입니까?

... 지금은 글고에서 스피너 항목에 따라 국가 코드를 설정하려면 ...하지만 난 스피너 항목에 따라 설정하는 방법을 알고 해달라고 회에서 국가 이름이 여기에 코드 (IS 무엇입니까 i)는 회 전자에서 국가 이름을 얻고있다 :

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..kindly 도움말에서 국가 코드를 설정합니다. 나는 당신이 String을 표시하려는 경우 당신이 TextView 아닌 EditText을 사용하려고한다고 가정

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

답변

1

:

내 JSON이다. 어쨌든

:와

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

간단한.

비동기로 유지하려면이 값을 청취자 중 하나에 넣어야한다고 가정합니다 (예 : onItemSelected()).

업데이트. 당신이 무엇을하려고하는지 알지 못하기 때문에 어쨌든 Locale 유틸리티 클래스 내의 상수를 찾아 볼 것을 권합니다. json과 비슷한 물건에 열중하지 않고도 모든 언어 ISO 코드와 거기에서 가져온 유용한 상수 및 유틸리티에서 필요한 것을 얻을 수 있습니다.

Locale.COUNTRY.getLanguage(); 

또는

Locale.getISOLanguages(); 

이 당신이 필요로하는 경우 나도 몰라하지만

.

+0

실제로 휴대 전화 번호로 전화를 걸려면 국가 코드 대신 전화 코드로 변경하고 휴대 전화 번호를 추가합니다 – user7316606

+0

http://stackoverflow.com/questions/42243097/how-to- make-linearlayout-design-in-android/42243434 # 42243434이 코드에서 호출 코드를 볼 수 있습니다. – user7316606