Android 从微调器获取所选项目的数据。并通过点击按钮发送
我已将数据传递给Spinner。这是我的JSON数据 我有一个旋转器,我在其中传递这个。这是来自post方法的数据 我的计划是在Spinner中获取以下数据。选择批次后,我将单击下面的按钮,将“批次Id”发送到下一个活动,它将从spinner中选择的“批次”中获取“批次Id”。因为下一个活动需要批处理Id从URL获取数据 我已经完成了“批处理”的解析。但我陷入了第二部分,我想根据选择的批次发送批次IdAndroid 从微调器获取所选项目的数据。并通过点击按钮发送,android,json,post,android-spinner,Android,Json,Post,Android Spinner,我已将数据传递给Spinner。这是我的JSON数据 我有一个旋转器,我在其中传递这个。这是来自post方法的数据 我的计划是在Spinner中获取以下数据。选择批次后,我将单击下面的按钮,将“批次Id”发送到下一个活动,它将从spinner中选择的“批次”中获取“批次Id”。因为下一个活动需要批处理Id从URL获取数据 我已经完成了“批处理”的解析。但我陷入了第二部分,我想根据选择的批次发送批次Id [ { "Batch_Id": "1", "Batch": "2016-21" },
[
{
"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");
}
final JSONArray jsonArraybatch=JSONArray.getJSONArray(1);
JSONArray jsonArraysection=JSONArray.getJSONArray(2);
JSONArray jsonArraysubject=JSONArray.getJSONArray(3);
JSONObject jsonResponse=jsonArrayTeacherName.getJSONObject(0);
teachername=jsonResponse.optString(“教师姓名”);
teacherid=jsonResponse.optString(“教师id”);
for(int i=0;i
创建模型类:
class BatchModel{
String batchId;
String batchName;
}
batchlist变量必须是BatchModel类型
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);
ex:List List=newarraylist();
字符串batch=jsonObject.optString(“batch”);
字符串batchid=jsonObject.optString(“批处理Id”);
BatchModel bm=新的BatchModel();
bm.batchId=batchId;
bm.批次=批次;
列表。添加(bm);
自旋适配器类
public class SpinnerAdapter extends ArrayAdapter<BatchModel> {
public SpinnerAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public ListAdapter(Context context, int resource, ,List<BatchModel> listItems) {
super(context, resource, listItems);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.itemlistrow, null);
}
BatchModel bm = getItem(position);
if (v != null) {
TextView tt1 = (TextView) v.findViewById(R.id.batch);
tt1.setText(bm.batch);
}
return v;
}
}
公共类SpinnerAdapter扩展了ArrayAdapter{
公共SpinnerAdapter(上下文,int textViewResourceId){
super(上下文,textViewResourceId);
}
公共ListAdapter(上下文上下文、int资源、列表项){
超级(上下文、资源、列表项);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
如果(v==null){
拉平机vi;
vi=LayoutInflater.from(getContext());
v=vi.充气(R.layout.itemlistrow,空);
}
BatchModel bm=getItem(位置);
如果(v!=null){
TextView tt1=(TextView)v.findViewById(R.id.batch);
tt1.setText(bm.batch);
}
返回v;
}
}
创建模型类:
class BatchModel{
String batchId;
String batchName;
}
batchlist变量必须是BatchModel类型
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);
ex:List List=newarraylist();
字符串batch=jsonObject.optString(“batch”);
字符串batchid=jsonObject.optString(“批处理Id”);
BatchModel bm=新的BatchModel();
bm.batchId=batchId;
bm.批次=批次;
列表。添加(bm);
自旋适配器类
public class SpinnerAdapter extends ArrayAdapter<BatchModel> {
public SpinnerAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public ListAdapter(Context context, int resource, ,List<BatchModel> listItems) {
super(context, resource, listItems);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.itemlistrow, null);
}
BatchModel bm = getItem(position);
if (v != null) {
TextView tt1 = (TextView) v.findViewById(R.id.batch);
tt1.setText(bm.batch);
}
return v;
}
}
公共类SpinnerAdapter扩展了ArrayAdapter{
公共SpinnerAdapter(上下文,int textViewResourceId){
super(上下文,textViewResourceId);
}
公共ListAdapter(上下文上下文、int资源、列表项){
超级(上下文、资源、列表项);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
如果(v==null){
拉平机vi;
vi=LayoutInflater.from(getContext());
v=vi.充气(R.layout.itemlistrow,空);
}
BatchModel bm=getItem(位置);
如果(v!=null){
TextView tt1=(TextView)v.findViewById(R.id.batch);
tt1.setText(bm.batch);
}
返回v;
}
}
首先,将此方法置于外部:
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");
}
mySpinner.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
selectedBatch=mySpinner.getItemAtPosition(position.toString();
batchid=jsonObject.optString(“批次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");
}
}
}
@覆盖
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
selectedBatch=mySpinner.getItemAtPosition(position.toString();
for(int i=0;jsonArraybatch.length();i++){
JSONObject JSONObject=jsonArraybatch.getJSONObject(i);
if(selectedBatch.equals(jsonObject.optString(“批次”)){
batchid=jsonObject.optString(“批次Id”);
}
}
}
首先,将此方法置于外部:
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");
}
mySpinner.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
selectedBatch=mySpinner.getItemAtPosition(position.toString();
batchid=jsonObject.optString(“批次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");
}
}
}
@覆盖
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
selectedBatch=mySpinner.getItemAtPosition(位置).toStri
String name = spinner.getSelectedItem().toString();
String id = spinnerMap.get(name);