我是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);
}
}