我正在尝试创建一个简单的Android应用程序,我遇到了一个我不确定如何修复的问题。 我是Android和Java的新手,所以如果你能提供帮助,我们将不胜感激。
这是我的代码:
public class arraylist extends ListActivity {
ArrayList<Team> teams = new ArrayList<Team>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Creating the array list
teams.add(new Team("Everton ", "everton", "Liverpool", "1878", "Jagielka", "Premier League"));
teams.add(new Team("Arsenal ", "arsenal", "London", "1886", "Arteta", "Premier League"));
teams.add(new Team("Manchester United ", "manutd", "Manchester", "1878", "Rooney", "Premier League"));
teams.add(new Team("Leeds United ", "leedsutd", "Leeds", "1919", "Cooper", "Championship"));
CustomAdapter custAdapter = new CustomAdapter(this, teams);
setListAdapter(custAdapter);
registerForContextMenu(getListView());
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Team t = teams.get(position);
Toast.makeText(this, t.getTeamName() + "are a team from " + t.getLocation() + ".", Toast.LENGTH_SHORT).show();
}
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo main) {
super.onCreateContextMenu(menu, v, main);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
}
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
String itemTitle = (String) item.getTitle();
switch (item.getItemId()) {
case R.id.year:
Toast.makeText(this, teams.getTeamName() + " were created in " + teams.getYear() + ".", Toast.LENGTH_SHORT).show();
return true;
case R.id.captain:
Toast.makeText(this, teams.getCaptain() + " is the current captain of " + teams.getTeamName() + ".", Toast.LENGTH_SHORT).show();
return true;
case R.id.league:
Toast.makeText(this, teams.getTeamName() + "are currently in the " + teams.getLeague() + ".", Toast.LENGTH_SHORT).show();
return true;
}
return super.onContextItemSelected(item);
}
}
这是我的Team.java:
public class Team {
private String teamName;
private String imageName;
private String location;
private String year;
private String captain;
private String league;
Team (String teamName, String imageName, String location, String year, String captain, String league) {
this.teamName = teamName;
this.imageName = imageName;
this.location = location;
this.year = year;
this.captain = captain;
this.league = league;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
public void setImageName(String imageName) {
this.imageName = imageName;
}
public void setLocation(String location) {
this.location = location;
}
public void setYear(String year) {
this.year = year;
}
public void setCaptain(String captain) {
this.captain = captain;
}
public void setLeague(String league) {
this.league = league;
}
public String getTeamName() {
return teamName;
}
public String getImageName() {
return imageName;
}
public String getLocation() {
return location;
}
public String getYear() {
return year;
}
public String getCaptain() {
return captain;
}
public String getLeague() {
return league;
}
}
我在这里收到一条错误,说“方法getTeamName()未定义类型ArrayList”我还试着从ArrayList调用这个:
Toast.makeText(this, teams.getTeamName() + " were created in " + teams.getYear() + ".", Toast.LENGTH_SHORT).show();
return true;
case R.id.captain:
Toast.makeText(this, teams.getCaptain() + " is the current captain of " + teams.getTeamName() + ".", Toast.LENGTH_SHORT).show();
return true;
case R.id.league:
Toast.makeText(this, teams.getTeamName() + "are currently in the " + teams.getLeague() + ".", Toast.LENGTH_SHORT).show();
return true;
任何提示或帮助? 如果这是一个简单的修复,我很抱歉,我没有长时间使用Java。
谢谢, DOM
答案 0 :(得分:1)
这是错误的:
teams.getTeamName()
getTeamName()
是Team
的方法,而不是ArrayList<Team>
的方法。
应该是
teams.get(i).getTeamName()
对于i
和0
之间的某些teams.size()-1
,。
同样适用于teams.getCaptain()
,teams.getLeague()
等......