在android中从中选择数据时,Spinner无法正常工作

时间:2017-02-27 06:34:48

标签: android json android-studio

有很多与之相关的问题,我搜索过它们并尝试使用提供的解决方案,但无法解决我的问题,这就是我发布新问题的原因。

我正在研究spinner。在我的应用中,我使用dropdown作为local JSON。所有数据都是从我的资产文件夹中的JSON文件生成的。以下是我的{"reference": [ {"ref_no":"11111111111111","Name":"Faisal"}, {"ref_no":"22222222222222","Name":"Salman"}, {"ref_no":"33333333333333","Name":"Asim"}, {"ref_no":"44444444444444","Name":"Asad"}, {"ref_no":"55555555555555","Name":"Mateen"}, {"ref_no":"66666666666666","Name":"Omar"}, {"ref_no":"77777777777777","Name":"Usama"} ]} 文件

ref.json

Spinner dd_ref, dd_m_type,dd_site_status, dd_pole_type;
String refr_no, meter_type, latitude, longitude, site_status, comm_status, pole_type;
ArrayList<String> refList, m_type_List, site_status_List, pole_type_List;

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    try {
        loadData();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

 public void loadData() throws JSONException {

//////////////// Below method is for reference number call
    JSONArray RefjsonArray;
    JSONObject Ref_obj;
    refList = new ArrayList<String>();

   try
   {
       Ref_obj = new JSONObject(loadRefJSONFromAssest());
       RefjsonArray = Ref_obj.getJSONArray("reference");


       for(int i = 0; i<RefjsonArray.length();i++)
       {
           Ref_obj = RefjsonArray.getJSONObject(i);
           Log.d("Details-->", Ref_obj.getString("ref_no"));
           String ref = Ref_obj.getString("ref_no");


           refList.add(ref);
       }
       refList.add(0,"Select a reference number");


   } catch (JSONException e)
   {
       e.printStackTrace();
   }
    dd_ref = (Spinner)findViewById(R.id.dd_ref);

    dd_ref.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, refList));


    dd_ref.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                refr_no = String.valueOf(refList.get(position));
                //text_pos.setText("Hi " + refr_no);

           // Log.d(" out ",refr_no);

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

        }
    });//// end reference spinner
///////// start of method meter type dropdown
    JSONArray m_type_Array;
    JSONObject m_type_obj;
    m_type_List = new ArrayList<String>();

    try{
        m_type_obj = new JSONObject(loadMeterTypeJSONFromAssest());

        m_type_Array = m_type_obj.getJSONArray("meter");

        for(int i = 0; i<m_type_Array.length(); i++)
        {
            m_type_obj = m_type_Array.getJSONObject(i);
            Log.d("Details-->", m_type_obj.getString("m_type"));

            String m_type = m_type_obj.getString("m_type");

            m_type_List.add(m_type);
        }
        m_type_List.add(0,"Select Meter Type");
    }catch (JSONException e)
    {
        e.printStackTrace();
    }

    dd_m_type = (Spinner)findViewById(R.id.dd_m_type);

    dd_m_type.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, m_type_List));

    dd_ref.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            meter_type = String.valueOf(m_type_List.get(position));

        }

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

        }
    });// end dropdown meter type
}

  ////// method for loading meter type data
public String loadMeterTypeJSONFromAssest() {

    String json = " ";

    try{
        InputStream is = getAssets().open("meter.json");

        int size = is.available();

        byte[] buffer = new byte[size];

        is.read(buffer);
        is.close();

        json = new String(buffer, "UTF-8");
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

    return json;



}

//// load reference # data
public String loadRefJSONFromAssest() {

    String json = " ";

    try{
        InputStream is = getAssets().open("ref.json");

        int size = is.available();

        byte[] buffer = new byte[size];

        is.read(buffer);
        is.close();

        json = new String(buffer, "UTF-8");
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

    return json;


}

MainActivity.java

spinner

当我运行我的应用时,它会将数据加载到JSON。我的列表中有7个参考号,如33333333333333文件中所示。应用程序运行正常,选择参考号44444444444444,当我选择logcat或更多时,应用程序崩溃,同时在java.lang.ArrayIndexOutOfBoundsException: length=12; index=-1 at java.util.ArrayList.get(ArrayList.java:310) at com.example.accurat.application.MainActivity$2.onItemSelected(MainActivity.java:131) at android.widget.AdapterView.fireOnSelected(AdapterView.java:931) at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:920) at android.widget.AdapterView.-wrap1(AdapterView.java) at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:890) at android.os.Handler.handleCallback(Handler.java:746) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5491) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

中给出以下错误
meter_type = String.valueOf(m_type_List.get(position));

它正在点meter type,我不知道为什么它会在这一点上发生,因为它与{{1}}没有任何联系。

我进行了搜索并找到了delimiter,但它无法解决我的问题。

任何帮助都将受到高度赞赏。

3 个答案:

答案 0 :(得分:1)

变化

dd_ref.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

dd_m_type.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

以米型下拉方式

答案 1 :(得分:1)

您正在添加错误的微调器项目单击侦听器,将dd_ref更改为dd_m_type微调器。

 dd_m_type = (Spinner)findViewById(R.id.dd_m_type);

    dd_m_type.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, m_type_List));

    dd_m_type.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            meter_type = String.valueOf(m_type_List.get(position));

        }

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

        }
    });// end dropdown meter type

答案 2 :(得分:0)

我认为从Spinner获取所选项目存在问题,请尝试以下方法获取所选项目。

  String Text = dd_m_type.getSelectedItem().toString();
    OR

 dd_m_type.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String item = parent.getItemAtPosition(position);
        }
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });