Android 在微调器中获取所选cityName的相同cityId
我解析了json数据(cityName和cityId),只想在微调器中显示城市名称,但在选定的微调器项上,选定的城市名称以及城市id必须使用intent继续进行下一个活动。。。我无法在微调器中获得与所选cityName相同的cityId 请举例说明任何建议。。。谢谢您,只需签出教程并尝试一下,它将帮助您:Android 在微调器中获取所选cityName的相同cityId,android,Android,我解析了json数据(cityName和cityId),只想在微调器中显示城市名称,但在选定的微调器项上,选定的城市名称以及城市id必须使用intent继续进行下一个活动。。。我无法在微调器中获得与所选cityName相同的cityId 请举例说明任何建议。。。谢谢您,只需签出教程并尝试一下,它将帮助您: public class MainActivity extends Activity { JSONObject jsonobject; JSONArray jsonarray;
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
}
});
}
}
}
公共类MainActivity扩展活动{
JSONObject JSONObject;
JSONArray JSONArray;
进程对话框;
ArrayList世界列表;
ArrayList世界;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//下载JSON文件异步任务
新建下载JSON().execute();
}
//下载JSON文件异步任务
私有类下载JSON扩展异步任务{
@凌驾
受保护的Void doInBackground(Void…参数){
//定位世界人口类别
world=新数组列表();
//创建一个数组以填充微调器
worldlist=newarraylist();
//JSON文件URL地址
jsonobject=JSONfunctions
.getJSONfromURL(“http://www.androidbegin.com/tutorial/jsonparsetutorial.txt");
试一试{
//找到节点列表名称
jsonarray=jsonobject.getJSONArray(“世界人口”);
for(int i=0;i
到目前为止,您尝试了什么?我想将城市名称和城市id发送到下一个活动,但当我在spinner中选择解析的城市名称时,我没有获得正确的城市id。我很高兴帮助您快乐地编写代码Hanks@AdityaVyas Lakhan我想这肯定会对我有所帮助,我会尝试您的答案,获取我的代码并尽快通知您。。谢谢兄弟