0
我有一個問題,我找不到解決方案。我查看並嘗試了多種不同的解決方案,以瞭解爲什麼微調不顯示選定的項目。在使用硬編碼數組時,它工作正常,但不適用於我在onCreate中加載的ArrayList。微調器已填充,但不會顯示所選項目,微調器的空白區域變寬。微調不顯示選定的項目,從數組列表加載微調框
public class MainActivity extends ActionBarActivity {
String[] test = new String[] {"1", "2", "3"};
ArrayList<String> routes = new ArrayList<String>();
Spinner sp;
String[] routesArray;
ArrayAdapter<String> adapter1;
final static String routeURL = "http://www.ctabustracker.com/bustime/api/v1/getroutes?key=";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, routes);
sp = (Spinner) findViewById(R.id.spnRoute);
loadRoutes();
adapter1.notifyDataSetChanged();
// routesArray = routes.toArray(new String[routes.size()]);
// sp.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, routesArray));
// routesArray = routes.toArray(new String[routes.size()]);
// sp.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, routesArray));
// sp.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, test));
}
public void loadRoutes() {
new RequestTask().execute(routeURL);
// routesArray = routes.toArray(new String[routes.size()]);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter1);
adapter1.notifyDataSetChanged();
}
class RequestTask extends AsyncTask<String, String[], Void> {
@Override
protected Void doInBackground(String... strings) {
try {
URL routesURL = new URL(strings[0]);
BufferedReader in = new BufferedReader(new InputStreamReader(routesURL.openStream()));
String line;
while((line = in.readLine()) != null) {
if(line.contains("<rt>")) {
int firstPos = line.indexOf("<rt>");
String tempNum = line.substring(firstPos);
tempNum = tempNum.replace("<rt>", "");
int lastPos = tempNum.indexOf("</rt>");
routes.add(tempNum.substring(0, lastPos));
}
}
in.close();
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
}
I <3你這麼多。過去兩天這讓我瘋狂。我完全忘記了AsyncTask同時運行,甚至沒有考慮到適配器未被填充。 – user3192092