【发布时间】:2017-01-17 11:17:36
【问题描述】:
我已将数据传递给 Spinner。这是我的 JSON 数据,
我有一个微调器,我在其中将它传递到 JSON 下方。这是来自 post 方法的数据。
我的计划是在 Spinner 中获取以下数据。选择批次后,我将单击下面的按钮,它将“Batch_Id”发送到下一个活动,它将从微调器中选择“Batch”中获取“Batch_Id”。因为下一个活动希望 Batch_Id 从 URL 获取数据。
我已经完成了“批处理”的解析。但我被困在第二部分,我想根据选择的批次发送 Batch_Id。
[
{
"Batch_Id": "1",
"Batch": "2016-21"
},
{
"Batch_Id": "2",
"Batch": "2015-20"
},
{
"Batch_Id": "3",
"Batch": "2014-19"
},
{
"Batch_Id": "4",
"Batch": "2013-18"
},
{
"Batch_Id": "5",
"Batch": "2012-17"
},
{
"Batch_Id": "6",
"Batch": "2014-17"
},
{
"Batch_Id": "7",
"Batch": "2015-18"
},
{
"Batch_Id": "8",
"Batch": "2016-19"
}
]
我已经在 Spinner 中解析了“Batch”,现在我想明智地获取 Batch_Id 位置,比如如果我选择 2016-19,那么我将得到“batch_id”等于 8。
我试过了,
final JSONArray jsonArraybatch = jsonArray.getJSONArray(1);
JSONArray jsonArraysection = jsonArray.getJSONArray(2);
JSONArray jsonArraysubject = jsonArray.getJSONArray(3);
JSONObject jsonResponse = jsonArrayTeacherName.getJSONObject(0);
teachername = jsonResponse.optString("teacher_name");
teacherid = jsonResponse.optString("teacher_id");
for (int i = 0; i < jsonArraybatch.length(); i++)
{
final JSONObject jsonObject = jsonArraybatch.getJSONObject(i);
final Spinner mySpinner = (Spinner) findViewById(R.id.batch_spinner);
String batch = jsonObject.optString("Batch");
batchlist.add(batch);
mySpinner
.setAdapter(new ArrayAdapter<String>(PutCredentials.this,
android.R.layout.simple_spinner_dropdown_item,
batchlist));
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedBatch = mySpinner.getItemAtPosition(position).toString();
batchid = jsonObject.optString("Batch_Id");
}
【问题讨论】:
-
到目前为止尝试了什么。显示相同的代码
-
其实我只展示了JSON的一部分,因为我已经在Spinner中解析了Batch。但我的问题是我想将 Batch_Id 发送到 Button Click 上的下一个活动。
-
见
OnItemSelectedListener#onItemSelected,最后一个参数是id,你的适配器只需覆盖getItemId方法 -
here 你有完整的工作代码,只需将其添加到
Activity#onCreate即可查看
标签: android json post android-spinner