2016-06-28 1 views
-1

파싱 된 json 데이터 (cityName 및 cityId)는 스피너에 도시 이름 만 표시하고 선택한 스피너 항목에는 선택한 도시 이름과 도시 ID를 입력해야합니다 의도를 사용한 활동 ... 나는 같은 도시를 얻을 수 없다. 선택한 cityName의 이름은 스피너에있다.스피너에 선택된 cityName의 동일한 도시 ID를 얻으려면

예와 어떤 제안 ... 감사

+0

를? – Selvin

+0

도시 이름과 도시 ID를 다음 활동으로 보내려고하지만 스피너에서 구문 분석 된 도시 이름을 선택할 때 올바른 도시 ID를 얻지 못했습니다 – Shubham

+0

행복하게 코딩 할 수있어서 기쁩니다 –

답변

0

그냥 체크 아웃 this 튜토리얼을 기쁘게하고 당신을 도울 것입니다 그런 식으로 시도 : 지금까지 시도했던 어떤

public class MainActivity extends Activity { 
    JSONObject jsonobject; 
    JSONArray jsonarray; 
    ProgressDialog mProgressDialog; 
    ArrayList<String> worldlist; 
    ArrayList<WorldPopulation> world; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // Download JSON file AsyncTask 
     new DownloadJSON().execute(); 

    } 

    // Download JSON file AsyncTask 
    private class DownloadJSON extends AsyncTask<Void, Void, Void> { 

     @Override 
     protected Void doInBackground(Void... params) { 
      // Locate the WorldPopulation Class 
      world = new ArrayList<WorldPopulation>(); 
      // Create an array to populate the spinner 
      worldlist = new ArrayList<String>(); 
      // JSON file URL address 
      jsonobject = JSONfunctions 
        .getJSONfromURL("http://www.androidbegin.com/tutorial/jsonparsetutorial.txt"); 

      try { 
       // Locate the NodeList name 
       jsonarray = jsonobject.getJSONArray("worldpopulation"); 
       for (int i = 0; i < jsonarray.length(); i++) { 
        jsonobject = jsonarray.getJSONObject(i); 

        WorldPopulation worldpop = new WorldPopulation(); 

        worldpop.setRank(jsonobject.optString("rank")); 
        worldpop.setCountry(jsonobject.optString("country")); 
        worldpop.setPopulation(jsonobject.optString("population")); 
        worldpop.setFlag(jsonobject.optString("flag")); 
        world.add(worldpop); 

        // Populate spinner with country names 
        worldlist.add(jsonobject.optString("country")); 

       } 
      } catch (Exception e) { 
       Log.e("Error", e.getMessage()); 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void args) { 
      // Locate the spinner in activity_main.xml 
      Spinner mySpinner = (Spinner) findViewById(R.id.my_spinner); 

      // Spinner adapter 
      mySpinner 
        .setAdapter(new ArrayAdapter<String>(MainActivity.this, 
          android.R.layout.simple_spinner_dropdown_item, 
          worldlist)); 

      // Spinner on item click listener 
      mySpinner 
        .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 

         @Override 
         public void onItemSelected(AdapterView<?> arg0, 
           View arg1, int position, long arg3) { 
          // TODO Auto-generated method stub 
          // Locate the textviews in activity_main.xml 
          TextView txtrank = (TextView) findViewById(R.id.rank); 
          TextView txtcountry = (TextView) findViewById(R.id.country); 
          TextView txtpopulation = (TextView) findViewById(R.id.population); 

          // Set the text followed by the position 
          txtrank.setText("Rank : " 
            + world.get(position).getRank()); 
          txtcountry.setText("Country : " 
            + world.get(position).getCountry()); 
          txtpopulation.setText("Population : " 
            + world.get(position).getPopulation()); 
         } 

         @Override 
         public void onNothingSelected(AdapterView<?> arg0) { 
          // TODO Auto-generated method stub 
         } 
        }); 
     } 
    } 

} 
+0

이것이 분명히 나를 도울 것이라고 생각합니다. 저는 제 코드에 대한 대답을 시도하고 최대한 빨리 알려 드리겠습니다. 고마워요. 오빠. – Shubham

+0

@ShubhamUpadhyay는 그것을 받아들입니다. –

관련 문제