2016-06-28 32 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接受它..它帮助 –

相关问题