2016-06-28 78 views
-1

我解析了json數據(cityName和cityId),只想在微調器中顯示城市名稱,但在所選的微調項目上,所選城市名稱以及城市ID必須繼續下一個使用意圖的活動...我無法在微調器中獲得所選城市名稱的同一cityId。要獲得與spinner中所選城市名稱相同的cityId

請與例如任何建議...謝謝

+0

了什麼你試過這麼遠嗎? – Selvin

+0

我想發送城市名稱和城市id到下一個活動,但是當我選擇解析城市名稱的微調我不geting正確的城市編號 – 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接受它..它幫助 –

相關問題