请提供示例代码(Android),该示例代码用于从JSON文件(例如,转换器,区域,代码)填充3个微调框

时间:2018-11-03 09:17:40

标签: android json spinner

我有3个微调器,我想将JSON数据解析为它们。例如,JSON文件结构具有Converterno,zoneno和codeid。所以我想填充所有转换器。在一个微调器和zoneno中。在另一个微调器和第三个微调器中。我在Android中尝试此操作并尝试解析json中的数据。请帮忙。 我的xml文件很好,只需要Java类中的帮助即可将值下拉到

这是我在@Lucefer的帮助下完成的最新代码(Selectcode.java)

package com.example.com;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

    public class selectcode extends AppCompatActivity {
      String URL = "*url_to_json_php*";
      List<String> converter = new ArrayList<>();
      List<String> zone = new ArrayList<>();
      List<String> codeid = new ArrayList<>();
      JSONObject jsonObject;
      JSONArray jsonArray;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_selectcode);
        try{
            jsonObject=new JSONObject(URL);
            jsonArray=jsonObject.getJSONArray("server_response");
            for (int i=0;i<jsonArray.length();i++){
                jsonObject=jsonArray.getJSONObject(i);
                converter.add(jsonObject.optString("converternumber"));
                zone.add(jsonObject.optString("zonenumber"));
                codeid.add(jsonObject.optString("codeid"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Spinner spinner1=(Spinner) findViewById(R.id.selectconverter);
        ArrayAdapter<String> adapter1=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(adapter1);
        Spinner spinner2=(Spinner) findViewById(R.id.selectzone);
        ArrayAdapter<String> adapter2=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(adapter2);
        Spinner spinner3=(Spinner) findViewById(R.id.selectcode);
        ArrayAdapter<String> adapter3=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item);
        spinner3.setAdapter(adapter3);
    }
    }

2 个答案:

答案 0 :(得分:0)

您需要在xml文件中创建三个狙击手,并且您可以按照以下方式提供数据

    List<String> areas = new ArrayList<>();
    List<String> schools = new ArrayList<>();
    List<String> classes = new ArrayList<>();
    jsonobject = JSONfunctions.getJSONfromURL("http://ww");

            try {

                jsonarray = jsonobject.getJSONArray("server_response");
                for (int i = 0; i < jsonarray.length(); i++) {
                    jsonobject = jsonarray.getJSONObject(i);
                    areas.add(jsonobject.optString("converternumber"));
                    schools.add(jsonobject.optString("zonenumber"));
                    classes.add(jsonobject.optString("codeid"));
                }
  .......................
  //finally u need to set those three lists to three snippers

    Spinner spinner1 = (Spinner) findViewById(R.id.Spinner1); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, area);
    adapter.setDropDownViewResource(android.R.layout.simple_spin‌​ner_dropdown_item);
    spinner1.setAdapter(adapter);

    Spinner spinner2 = (Spinner) findViewById(R.id.Spinner2); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, school);
    adapter.setDropDownViewResource(android.R.layout.simple_spin‌​ner_dropdown_item);
    spinner1.setAdapter(adapter);


    Spinner spinner1 = (Spinner) findViewById(R.id.Spinner3); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, class);
    adapter.setDropDownViewResource(android.R.layout.simple_spin‌​ner_dropdown_item);
    spinner1.setAdapter(adapter);

答案 1 :(得分:0)

我找到了解决方法

CountryStateCitySpinner