Android 如何从json中获取id,但不在spinner中显示?
我从web服务获得了这个Android 如何从json中获取id,但不在spinner中显示?,android,json,Android,Json,我从web服务获得了这个json: [{"id_mata_kul":"2","mata_kul":"Oracle Database"},{"id_mata_kul":"3","mata_kul":"Pengatar Manajemen dan Bisnis"},{"id_mata_kul":"5","mata_kul":"Leadership"}] 然后我可以在微调器中显示mata_kul对象,但我需要从mata_kul获取id,而不将id显示到微调器中 问题: 我需要根据所选的mata_ku
json
:
[{"id_mata_kul":"2","mata_kul":"Oracle Database"},{"id_mata_kul":"3","mata_kul":"Pengatar Manajemen dan Bisnis"},{"id_mata_kul":"5","mata_kul":"Leadership"}]
然后我可以在微调器中显示mata_kul
对象,但我需要从mata_kul
获取id,而不将id显示到微调器中
问题:
我需要根据所选的mata_kul
保存id
例如:当从spinner中选择Leadership
时,我需要使用intent保存该ID5
。另外,当从spinner中选择Oracle数据库
时,我需要使用intent保存ID2
我读到过和我一样的问题,但我不知道它是如何运作的
有人能帮我解决这个问题吗
编辑:
在doInBackground
中:
for (int i = 0; i < jArray.length(); i++) {
JSONObject jsonObject = jArray.getJSONObject(i);
list2.add(jsonObject.getString("mata_kul")); //collecting all json `mata_kul`
}
在onCreate
中:
listItems2.addAll(list2);
adapter2.notifyDataSetChanged();
adapter2= new ArrayAdapter<>(this, R.layout.spinner_layout, R.id.txt, listItems2);
adapter2=newarrayadapter(这个,R.layout.spinner\u layout,R.id.txt,listItems2);
您可以使用Hashmap解决您的问题。在Hashmap中将id值存储为key,将mata_kul部分存储为value。然后使用Hashmap值在微调器中显示。单击项目时,使用值从hashmap中查找相应的id 覆盖微调器并获取所选项目的位置
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int selectedItemPos = position;
//use selectedItemPos to retrieve id values from json array
}
spinner.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
int selectedItemPos=位置;
//使用selectedItemPos从json数组中检索id值
}
尝试使用这种方法获取id
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
}
});
}
}
mySpinner
.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView arg0,
视图arg1,内部位置,长arg3){
//TODO自动生成的方法存根
//在activity_main.xml中找到文本视图
TextView txtrank=(TextView)findViewById(R.id.rank);
TextView txtcountry=(TextView)findViewById(R.id.country);
TextView txtpopulation=(TextView)findViewById(R.id.population);
//设置后跟位置的文本
setText(“秩:
+get(position.getRank());
txtcountry.setText(“国家:
+world.get(position.getCountry());
setText(“人口:
+get(position.getPopulation());
}
@凌驾
未选择公共无效(AdapterView arg0){
//TODO自动生成的方法存根
}
});
}
}
首先,创建一个模型来存储来自web服务的json值。 然后使用此模型创建自定义适配器。 并处理微调器的项目选定事件:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Model model = (Model) parent.getItemAtPosition(position);
// do something with the model
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}});
spinner.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
Model Model=(Model)parent.getItemAtPosition(position);
//对模型做些什么
}
@凌驾
未选择公共无效(AdapterView父级){
}});
你能展示一下你是如何实现你的Spinner适配器的吗?把你的JSON转换成Java模型,你可以用它做任何事情。你的模型类在哪里参考这个答案我不能这样做,因为当选择Leadership
时,id值是3,但是在我的JSON中,Leadership
id应该是5如果你将数据存储在一个数组中,你可以n使用position轻松检索。我认为这不是我需要的,因为我无法使用position
获取id,因为您没有使用POJO类/模型类,正如您在我的json中看到的那样,我有3个数据,但id是恒定的,这取决于mata_kul
selected我在该站点中没有看到POJO类,:(创建名为WorldPopulation的模型类)