如何在 运行 时间填充微调器?
How to poulate the spinner at run time?
我不知道出了什么问题,我只想在 运行 时间填充 Spinner。但是当我 运行 我的应用程序在移动设备上时,它会用我的 class 路径填充微调器。这是我正在做的代码
public class FullscreenActivity extends Activity {
Spinner spSelectTeam;
TextView tvTeam;
ArrayList<TeamClass> teamClasses;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout2);
init();
teamClasses = populateList();
//applying adapter
ArrayAdapter myAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, teamClasses);
spSelectTeam.setAdapter(myAdapter);
}
void init(){
spSelectTeam = (Spinner) findViewById(R.id.sp_team);
tvTeam = (TextView) findViewById(R.id.tv_team);
}
public ArrayList<TeamClass> populateList() {
//ArrayList<TeamClass> myTeamName = new ArrayList<TeamClass>();
ArrayList<TeamClass> teamClasses = new ArrayList<>();
teamClasses.add(new TeamClass("----- All -----"));
teamClasses.add(new TeamClass("Computer Direct"));
teamClasses.add(new TeamClass("Datasys"));
teamClasses.add(new TeamClass("Host Interactive"));
return teamClasses;
}
}
适配器的 class 是
public class TeamClass {
private String TeamName;
public TeamClass(String teamName){
this.TeamName = teamName;
}
public String getTeamName() {
return TeamName;
}
public void setTeamName(String teamName) {
TeamName = teamName;
}
}
请帮忙。提前致谢
您需要为 TeamClass
覆盖 toString()
。
试试这个...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout2);
init();
teamClasses = populateList();
//applying adapter
ArrayAdapter myAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, teamClasses);
myAdapter .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spSelectTeam.setAdapter(myAdapter);
}
加入你的TeamClass
@Override
public String toString() {
return this.TeamName ;
}
也像下面这样更改您的代码..
List<TeamClass> teamClasses;
teamClasses = populateList();
public List<TeamClass> populateList() {
List<TeamClass> teamClasses = new ArrayList<TeamClass>();
teamClasses.add(new TeamClass("----- All -----"));
teamClasses.add(new TeamClass("Computer Direct"));
teamClasses.add(new TeamClass("Datasys"));
teamClasses.add(new TeamClass("Host Interactive"));
return teamClasses;
}
只需将以下内容添加到您的 TeamClass 中:
@Override
public String toString() {
return getTeamName();
}
我不知道出了什么问题,我只想在 运行 时间填充 Spinner。但是当我 运行 我的应用程序在移动设备上时,它会用我的 class 路径填充微调器。这是我正在做的代码
public class FullscreenActivity extends Activity {
Spinner spSelectTeam;
TextView tvTeam;
ArrayList<TeamClass> teamClasses;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout2);
init();
teamClasses = populateList();
//applying adapter
ArrayAdapter myAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, teamClasses);
spSelectTeam.setAdapter(myAdapter);
}
void init(){
spSelectTeam = (Spinner) findViewById(R.id.sp_team);
tvTeam = (TextView) findViewById(R.id.tv_team);
}
public ArrayList<TeamClass> populateList() {
//ArrayList<TeamClass> myTeamName = new ArrayList<TeamClass>();
ArrayList<TeamClass> teamClasses = new ArrayList<>();
teamClasses.add(new TeamClass("----- All -----"));
teamClasses.add(new TeamClass("Computer Direct"));
teamClasses.add(new TeamClass("Datasys"));
teamClasses.add(new TeamClass("Host Interactive"));
return teamClasses;
}
}
适配器的 class 是
public class TeamClass {
private String TeamName;
public TeamClass(String teamName){
this.TeamName = teamName;
}
public String getTeamName() {
return TeamName;
}
public void setTeamName(String teamName) {
TeamName = teamName;
}
}
请帮忙。提前致谢
您需要为 TeamClass
覆盖 toString()
。
试试这个...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout2);
init();
teamClasses = populateList();
//applying adapter
ArrayAdapter myAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, teamClasses);
myAdapter .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spSelectTeam.setAdapter(myAdapter);
}
加入你的TeamClass
@Override
public String toString() {
return this.TeamName ;
}
也像下面这样更改您的代码..
List<TeamClass> teamClasses;
teamClasses = populateList();
public List<TeamClass> populateList() {
List<TeamClass> teamClasses = new ArrayList<TeamClass>();
teamClasses.add(new TeamClass("----- All -----"));
teamClasses.add(new TeamClass("Computer Direct"));
teamClasses.add(new TeamClass("Datasys"));
teamClasses.add(new TeamClass("Host Interactive"));
return teamClasses;
}
只需将以下内容添加到您的 TeamClass 中:
@Override
public String toString() {
return getTeamName();
}