gpt4 book ai didi

android - Spinner onItemSelected 仅返回数组的最后一项

转载 作者:行者123 更新时间:2023-11-29 23:19:06 26 4
gpt4 key购买 nike

我正在尝试在应用程序中实现多个微调器,其中数据是在 Volley 调用后填充的。它应该以这样的方式工作;基于第一个微调器值 "id" 的选择,第二个微调器应该从另一个 api 调用中填充它的值,将所选的 "id" 作为参数。我现在可以在微调器中显示 "name"(s) 了。但不确定如何为第二次 api 调用获取所选项目的 id。现在,当我在第一个微调器中选择任何项目时,它只返回数组中最后一个项目的 id

Json 数组响应:

[
{
"id":1,
"name":"Roger Federer",
"country":"Switzerland",
"city":"Basel"

},
{
"id":2,
"name":"Rafael Nadal",
"country":"Spain",
"city":"Madrid"
},
{
"id":3,
"name":"Novak Djokovic",
"country":"Serbia",
"city":"Monaco"
},
{
"id":4,
"name":"Andy Murray",
"country":"United Kingdom",
"city":"London"
},
{
"id":5,
"name":"Maria Sharapova",
"country":"Russia",
"city":"Moscow"
},
{
"id":8,
"name":"Ana Ivanovic",
"country":"Serbia",
"city":"Belgrade"
}
]

一级(模型)

public class FirstLevel {


public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

private String title;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

private int id;
}

Java代码

private ArrayList<String> names = new ArrayList<String>();

private ArrayList<FirstLevel> FirstLevelDataAdapterClassList;

private int selectedFirstLevel;
...
public void FIRST_LEVEL_WEB_CALL(final ViewHolder viewHolder) {

//showSimpleProgressDialog(context, "Loading...", "Fetching Json", false);
JsonArrayRequest jsArrRequest = new JsonArrayRequest
(Request.Method.GET, HTTP_SERVER_URL, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
FIRST_PARSE_DATA_AFTER_WEBCALL(response,viewHolder);
Log.i("FL", "FL");
//removeSimpleProgressDialog();

}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub

}
}) {

};

requestQueue2 = Volley.newRequestQueue(context);
requestQueue2.add(jsArrRequest);

}
public void FIRST_PARSE_DATA_AFTER_WEBCALL(JSONArray array, final ViewHolder viewHolder) {

FirstLevelDataAdapterClassList = new ArrayList<>();

FirstLevel GetFirstLvDataModel = new FirstLevel();

for (int i = 0; i < array.length(); i++) {

JSONObject json = null;
try {
json = array.getJSONObject(i);
GetFirstLvDataModel.setId(json.getString("id"));
GetFirstLvDataModel.setName(json.getString("name"));
FirstLevelDataAdapterClassList.add(GetFirstLvDataModel);
names.add(FirstLevelDataAdapterClassList.get(i).getTitle().toString());

} catch (JSONException e) {

e.printStackTrace();
}

ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(context, simple_spinner_item, names);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
viewHolder.spinFirst.setAdapter(spinnerArrayAdapter);

viewHolder.spinFirst.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {


selectedFirstLevel=FirstLevelDataAdapterClassList.get(position).getId();

Log.i("FLFL",selectedFirstLevel);
SECOND_LEVEL_WEB_CALL(viewHolder,selectedFirstLevel);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});
}

}

最佳答案

你的这条线

selectedFirstLevel= //How to get the 'id' of the selected item here?

会是

selectedFirstLevel = FirstLevelDataAdapterClassList.get(position).getId();

试试这个。谢谢

关于android - Spinner onItemSelected 仅返回数组的最后一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54743581/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com