gpt4 book ai didi

java - 当用户单击微调器中的一项时,它应该显示另一项数据将从 JSON Web 服务获取

转载 作者:行者123 更新时间:2023-12-02 00:53:28 24 4
gpt4 key购买 nike

我已经编写了从 json Web 服务获取数据并以微调器(下拉菜单)方式显示的代码。现在我想编辑微调器,就像当用户单击国家/地区时那样,它应该获得州值,我该怎么做?

通过在谷歌上搜索,我尝试编写了从网络服务获取数据并在微调器中显示的代码,但它显示了来自网络服务的完整数据,例如 ID 名称标志状态,现在我想编辑它,就像单击印度那样显示印度国家值(value)观..

检查JSON

private Spinner spinner;
private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);

setSupportActionBar(toolbar);

FloatingActionButton fab = findViewById(R.id.fab);
spinner = findViewById(R.id.spinner);
textView = findViewById(R.id.text_view);

fab.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
fetchJsonDataFromUrl();
}
});
}

private void fetchJsonDataFromUrl() {

JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, URL, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
parseJson(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});

Volley.newRequestQueue(this).add(jsonArrayRequest);
}

private void parseJson(JSONArray response)
{

List<Country> countries = new ArrayList<>();

try {
for (int i = 0; i < response.length(); i++) {
JSONObject obj = response.getJSONObject(i);

String cid = obj.getString("CID");
String countryName = obj.getString("CName");
String flag = obj.getString("Flag");
String states = obj.getString("States");

countries.add(new Country(cid, countryName, flag, states));
}
Spinner(countries);
} catch (JSONException e) {
e.printStackTrace();
}
}

private void Spinner(final List<Country> countries) {
if (countries.size() > 0) {
ArrayAdapter<Country> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, countries);
spinner.setAdapter(adapter);

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Country country = countries.get(position);
String meta = "ID: " + country.getCid() + "\nNAME: " + country.getName() + "\nFLAG: " + country.getFlag() + "\nSTATES: " + country.getStates();
textView.setText(meta);
}


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

}
});
}
}
}

最佳答案

您想在另一个 View 中显示州名吗?

关于java - 当用户单击微调器中的一项时,它应该显示另一项数据将从 JSON Web 服务获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57839706/

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