简体   繁体   English

当我在第一个微调器中再次选择一个项目时发生错误,然后在第二个微调器中未解析相关数据意味着它没有刷新

[英]error occur when i select an item again in 1st spinner then the related data do not parsed in 2nd spinner means it does'nt refresh

I have 3 spinners that use data parsed from a web service. 我有3个微调器,它们使用从Web服务解析的数据。 When I select an item from 1st spinner then the 2nd spinner will present related data that was parsed. 当我从第一个微调器中选择一个项目时,第二个微调器将显示已解析的相关数据。

But when I select an item again in 1st spinner then the related data is not parsed in 2nd spinner, so it doesn't refresh. 但是,当我在第一微调器中再次选择一个项目时,相关数据不会在第二微调器中解析,因此不会刷新。

Can you help solving this issue? 您能帮忙解决这个问题吗? Thanks in advance. 提前致谢。

    country_spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

    public void onItemSelected(AdapterView<?> parent, View view,
            int position, long id) {
            int item=country_spinner.getSelectedItemPosition();
            if(item!=0)
            {

                String selected1=country_spinner.getItemAtPosition(position).toString();
                System.out.println(selected1);
                progressDialog=ProgressDialog.show(getParent(), "", "Loading...");
                new StateTask().execute();

            }

            adapter1.notifyDataSetChanged();
    }

    @Override
    public void onNothingSelected(
            AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }
});

2nd spinner 第二个微调器

      state_spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

             public void onItemSelected(AdapterView<?> parent, View view,
                       int position, long id) {

                int item=state_spinner.getSelectedItemPosition();
               //   String sate=state_spinner.getSelectedItem().toString();
                if(item!=0)
                {

                    String selected=state_spinner.getItemAtPosition(position).toString();
                    System.out.println(selected);
                    city_spinner.setEnabled(false);
                    progressDialog=ProgressDialog.show(getParent(), "", "Loading...");
                        new CityTask().execute();
                }
                adapter2.notifyDataSetChanged();
               }

                    @Override
                    public void onNothingSelected(
                            AdapterView<?> arg0) {
                        // TODO Auto-generated method stub

                    }
        });

StateTask StateTask

  public class StateTask extends AsyncTask<String, String, String>{

    @Override
    protected String doInBackground(String... arg0) {
        //Creating JSON Parser instance//////////
        JSONParser jParser=new JSONParser();
        //Getting JSON String from URL..............
        JSONObject jObject=jParser.getJSONFromUrl(URL);
        try
        {
            jsonArray=jObject.getJSONArray(TAG_STATES);
            //final String[] country_array_spinner = new String[jsonArray.length()]; 


            for (int i=0; i<jsonArray.length(); i++)
            {
                //country_array_spinner[i]=jsonArray.getJSONObject(i).getString(TAG_STATE);
                adapter1.add(jsonArray.getJSONObject(i).getString(TAG_STATE));
            }

        }
        catch(JSONException e){
            e.printStackTrace();
        }
        return null;
    }
    @Override
    protected void onPostExecute(String result) {
         state_spinner.setEnabled(true);
        state_spinner.setAdapter(adapter1);
        progressDialog.dismiss();
        super.onPostExecute(result);
    }
}

CityTask 城市任务

public class CityTask extends AsyncTask<String, String, String>{

    @Override
    protected String doInBackground(String... arg0) {
        //Creating JSON Parser instance//////////
        JSONParser jParser=new JSONParser();
        //Getting JSON String from URL..............
        JSONObject jObject=jParser.getJSONFromUrl(URLs);
        try
        {
            String sate=state_spinner.getSelectedItem().toString();

            jsonArray=jObject.getJSONArray(TAG_CITIES);
            //final String[] country_array_spinner = new String[jsonArray.length()]; 

            for (int i=0; i<jsonArray.length(); i++)

            {
                JSONObject e=jsonArray.getJSONObject(i);
                String ab=e.getString(TAG_STATE);
                Log.v(ab, "hgdsagds");
                if(ab.contains(sate)){
                    adapter2.add(jsonArray.getJSONObject(i).getString(TAG_CITY));
                }
                //country_array_spinner[i]=jsonArray.getJSONObject(i).getString(TAG_STATE);

            }

        }
        catch(JSONException e){
            e.printStackTrace();
        }
        return null;
    }
    @Override
    protected void onPostExecute(String result) {
        city_spinner.setEnabled(true);
        city_spinner.setAdapter(adapter2);
        progressDialog.dismiss();
        super.onPostExecute(result);
    }
}

It looks to me like you have a code logic error... 在我看来,您好像有代码逻辑错误...

adapter1.add(jsonArray.getJSONObject(i).getString(TAG_STATE)); 

You never clear/null your adapter (at least in the posted code), you just keep adding to it using the above code. 您永远不会清除/清空适配器(至少在发布的代码中),而只是使用上面的代码添加到适配器中。 This could appear to you as not refreshing as the new items would be at the end of the spinner (possibly out of sight). 在您看来,这似乎没有刷新,因为项目将位于微调框的末尾(可能不可见)。

Or, you could have left out of the post the code where you do that... 或者,您可能会忽略了执行该操作的代码...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在更改第一微调器值时更改第二微调器值 - changing 2nd spinner values on changing 1st spinner value 由 1st Spinner 创建的 2nd Spinner 应更新 textview - 2nd Spinner which was created by 1st Spinner, should update textview 当我返回到第一个活动时,第二个活动未调用第二个活动的onCreate()函数 - When I return back to 1st Activity the onCreate() function of 2nd Activity is not called in 2nd time 从适配器单击第一个选项卡recyclerview项时,如何为第二个选项卡设置动画? - how to animate to 2nd tab when 1st tab recyclerview item is clicked from adapter? 如何从第二活动到第一活动获取数据 - How to get data from 2nd Activity to 1st Activity JComboBox仅在从第1到第2时才更改组件(而不是从第3到第2时) - JComboBox changes components only when going from 1st to 2nd (Not from 3rd to 2nd) 如果再次选择项目,则调整器 - spinner if item selected again 关于第一个init()和第二个init() - regarding 1st init() and 2nd init() 检查第一个 if 条件,然后检查第二个 - Checking 1st if condition and then 2nd 如何实现一个多维数组,其中随着添加更多的第二维数组,第一维会更改大小? - How do I implement a multidimensional array where the the 1st dimension changes size as more 2nd dimension arrays are added?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM