我已将数据传递给Spinner。这是我的JSON数据,
我有一个微调器,我在JSON下面传递这个。这是post方法的数据。
我的计划是在Spinner中获取以下数据。选择批处理后,我将单击下面的按钮,这将向下一个活动发送“Batch_Id”,它将从微调器中选择的“批处理”中获取“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_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");
}
答案 0 :(得分:0)
创建模型类:
c2
batchlist变量必须是BatchModel类型
class BatchModel{
String batchId;
String batchName;
}
SpinnerAdapter类
ex: List<BatchModel> list= new ArrayList<>();
String batch = jsonObject.optString("Batch");
String batchid = jsonObject.optString("Batch_Id");
BatchModel bm= new BatchModel();
bm.batchId=batchid;
bm.batch=batch;
list.add(bm);
答案 1 :(得分:0)
首先将此方法保留在外:
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");
}
现在您可以使用这个来查找您的batchid:
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedBatch = mySpinner.getItemAtPosition(position).toString();
for(int i = 0;jsonArraybatch.length(); i++){
JSONObject jsonObject = jsonArraybatch.getJSONObject(i);
if (selectedBatch.equals(jsonObject.optString("Batch"))){
batchid = jsonObject.optString("Batch_Id");
}
}
}
答案 2 :(得分:0)
您有两个选择
Batch
和batchName
创建一个batchId
骨架
并为通过Array<Batch>
为微调器使用自定义适配器
<强>适配器强>
public class SchoolSpinnerAdapter extends BaseAdapter {
private ArrayList<School> mSchools;
private Context mContext;
public SchoolSpinnerAdapter(Context ctx, ArrayList<School> schools) {
mContext = ctx;
mSchools = schools;
}
@Override
public int getCount() {
return mSchools.size();
}
@Override
public Object getItem(int position) {
return mSchools.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.spinner_text, null);
}
((TextView) convertView).setText(mSchools.get(position).getName());
return convertView;
}
}
布局膨胀
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/dropDownItemStyle"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:background="#cccccc"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="40dp"
android:ellipsize="marquee" />
将此适配器设置为微调器
spinner.setAdapter(new SchoolAdapter(this,schoollist));
使用地图作为参考
为微调器准备值
String[] spinnerArray = new String[Province_ID.size()];
HashMap<String,String> spinnerMap = new HashMap<String, String>();
for (int i = 0; i < Province_ID.size(); i++)
{
spinnerMap.put(Province_NAME.get(i),Province_ID.get(i));
spinnerArray[i] = Province_NAME.get(i);
}
将值设置为微调器
ArrayAdapter<String> adapter =new ArrayAdapter<String>(context,android.R.layout.simple_spinner_item, spinnerArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
获取微调器的价值
String name = spinner.getSelectedItem().toString();
String id = spinnerMap.get(name);