0

我是android新手,遇到了一些麻烦。我创建了 2 个微调器。第一个去获取 YEARS 并动态填充第一个微调器。根据用户选择的年份,第二个微调器关闭并获取该特定年份的汽车制造商(福特、本田等)并动态更新第二个微调器。当我启动应用程序时,第一个微调器会正确填充年份,但是当我选择一年时,选择不会被选中。我一直在阅读,我 90% 确定我的所有代码都是正确的,但我认为其中一些只是在错误的地方。例如,我应该更多地使用 onCreate 方法吗?我应该用不同的方法拆分每个微调器吗?谁能告诉我我做错了什么,或者如果您特别好,请重新格式化以使其正常工作?这是我的代码。

public class MainActivity extends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

    final Spinner year=(Spinner)findViewById(R.id.spinner_year);
    final Spinner make=(Spinner)findViewById(R.id.spinner_make);

    final ArrayList<String> year_options = new ArrayList<String>();
    final ArrayList<String> make_options = new ArrayList<String>();


    /*
     * ASYNC TASK CLASSES
     */

    class populateYear extends AsyncTask<URL, Void, NodeList> {
        protected NodeList doInBackground(URL... urls) {
            NodeList yearList = null;
            try {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                Document doc = db.parse(urls[0].openStream());
                Element docEle = doc.getDocumentElement();

                yearList = docEle.getElementsByTagName("menuItem");


            } catch (Exception e) {
                System.out.println(e);
            }
            return yearList;

        }

        protected void onPostExecute(NodeList yearList) {

            if (yearList != null && yearList.getLength() > 0) {
                for (int i = 0; i < yearList.getLength(); i++) {
                    Node node = yearList.item(i);
                    if (node.getNodeType() == Node.ELEMENT_NODE) {
                        Element e = (Element) node;
                        NodeList nodeList = e.getElementsByTagName("text");
                        year_options.add(nodeList.item(0).getChildNodes().item(0).getNodeValue());
                    }
                }
            } else {
                System.exit(1);
            }

        }
    }


    class populateMake extends AsyncTask<String, Void, NodeList> {
        protected NodeList doInBackground(String... strings) {
            NodeList makeList = null;
            try {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                //Building a URL with "strings[0]" being equal to the year that we pass to the class.
                URL url = new URL("http://www.fueleconomy.gov/ws/rest/vehicle/menu/make?year=" + strings[0]);
                Document doc = db.parse(url.openStream());
                Element docEle = doc.getDocumentElement();

                makeList = docEle.getElementsByTagName("menuItem");


            } catch (Exception e) {
                System.out.println(e);
            }
            return makeList;

        }

        protected void onPostExecute(NodeList makeList) {

            if (makeList != null && makeList.getLength() > 0) {
                for (int i = 0; i < makeList.getLength(); i++) {
                    Node node = makeList.item(i);
                    if (node.getNodeType() == Node.ELEMENT_NODE) {
                        Element e = (Element) node;
                        NodeList nodeList = e.getElementsByTagName("text");
                        make_options.add(nodeList.item(0).getChildNodes().item(0).getNodeValue());
                    }
                }
            } else {
                System.exit(1);
            }

        }
    }


    /*
     * END OF ASYNC TASK CLASSES
     */


    final ArrayAdapter<String> yearAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item,year_options);

    //Filling the YEAR SPINNER
    URL xmlURL = null;
    try {
        xmlURL = new URL("http://www.fueleconomy.gov/ws/rest/vehicle/menu/year");
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //Parse the XML and use the value by calling the AsyncTaskClass
    new populateYear().execute(xmlURL);

    //Setting the Year Adapter
    year.setAdapter(yearAdapter);

    final ArrayAdapter<String> makeAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item,make_options);
    //Disabling the MAKE spinner onLoad. Will be enabled later when user picks a year.
    make.setEnabled(false);
    //Setting the Make Adapter
    make.setAdapter(makeAdapter);  



    //Adding the listener for when someone selects a YEAR
    year.setOnItemSelectedListener(new OnItemSelectedListener() {


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

            year.setSelection(position);
            String selectedYear=year_options.get(position).toString();




            resetMake(selectedYear);                               
        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {

        }

        public void resetMake(String selectedYear) {      
            //Clear the ArrayList belonging to MAKE so you can add new data based on the YEAR selection
            make_options.clear();
            new populateMake().execute(selectedYear);

            //Clear the makeAdapter and then add the new makes bases on the YEAR selection.
            makeAdapter.clear();
            for(int i=0; i < make_options.size(); i++) {
                makeAdapter.add(make_options.get(i));
            }
            makeAdapter.notifyDataSetChanged();
            make.setEnabled(true);
        }


}
4

1 回答 1

1

an 的重点AsyncTask是让它从UI线程异步运行,以便应用程序可以继续运行。这意味着当您启动任务时,函数中的其余代码将继续运行。因此,当您输入public void resetMake(String selectedYear) {运行时AsyncTask,该函数中的其余代码也是如此,这意味着您Adapter在下载新数据之前尝试填充。

有几种方法可以处理这个问题。您可以在执行任务后将该函数中的所有代码移动到onPostExecute()该任务中。

protected void onPostExecute(NodeList makeList) {

        if (makeList != null && makeList.getLength() > 0) {
            for (int i = 0; i < makeList.getLength(); i++) {
                Node node = makeList.item(i);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    Element e = (Element) node;
                    NodeList nodeList = e.getElementsByTagName("text");
                    make_options.add(nodeList.item(0).getChildNodes().item(0).getNodeValue());
                }
            }
  //Clear the makeAdapter and then add the new makes bases on the YEAR selection.
        makeAdapter.clear();
        for(int i=0; i < make_options.size(); i++) {
            makeAdapter.add(make_options.get(i));
        }
        makeAdapter.notifyDataSetChanged();
        make.setEnabled(true);
        } else {
            System.exit(1);
        }

    }

这样做只会让你的功能

 public void resetMake(String selectedYear) {      
        //Clear the ArrayList belonging to MAKE so you can add new data based on the YEAR selection
        make_options.clear();
        new populateMake().execute(selectedYear);

所以你可以把它移到onItemSelected()你的第一个Spinner并取消这个功能。

注意我没有检查这个代码的完整性,所以你可能有语法错误,需要移动大括号等,这样它才能编译。

补充说明

通常,您将初始化您的Viewsin onCreate(),尽管您可以将它们声明为成员变量,并且您通常不需要制作它们final。这也将使您的代码更具可读性。因此,您的课程的开头可能如下所示:

  public class MainActivity extends Activity {
    Spinner year, make; // you can declare them here so you can use them anywhere in the Activity

    ArrayList<String> year_options = new ArrayList<String>();
    ArrayList<String> make_options = new ArrayList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        year = (Spinner)findViewById(R.id.spinner_year);  // and initialize them here
        make=(Spinner)findViewById(R.id.spinner_make);
    }
于 2013-07-03T00:55:59.887 回答