我正在使用JSoup从特定年份获取NFL团队列表并将其加载到微调器中。我正在移植我为Android制作的程序以获得更多练习,因为我对Android仍然有点新鲜。我的问题与微调器有关。在我的PC程序中,我有一行:MainWindow.homeTeam.setModel(new DefaultComboBoxModel(teams.toArray()));
,在我的Android应用程序中,我有这个:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
teams.toArray(), android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
MainWindow.awayTeam.setAdapter(adapter);
我收到“this”错误,说“不能在静态环境中使用它”。我已经有一段时间没有获胜了。这是我的方法:
public static void setTeamComboBox(int comboBoxID, String year) throws IOException {
List<String> teams = new ArrayList<String>();
String AllGamesURL = "http://www.nfl.com/standings?category=league&season=" + year + "-REG";
Document AllGames = Jsoup.connect(AllGamesURL).get();
Elements AllGamesTeams = AllGames.select("table.data-table1 tr.tbdy1");
int i = 0;
for (Element teamName : AllGamesTeams) {
if(teamName.select("td[align] > a[href]").text().trim().equals("")) {
teams.add(teamName.select("td[align]").text().trim());
} else {
teams.add(teamName.select("td[align] > a[href]").text().trim());
}
}
Collections.sort(teams);
if(comboBoxID == 1) {
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
teams.toArray(), android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
MainWindow.awayTeam.setAdapter(adapter);
} else {
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
teams.toArray(), android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
MainWindow.homeTeam.setAdapter(adapter);
}
}