如何在运行时填充微调器?

时间:2015-01-13 11:57:22

标签: android android-arrayadapter

我不知道出了什么问题,我想做的就是在运行时填充Spinner。但是,当我在移动设备上运行我的应用程序时,它会用我的课程路径填充微调器。这是我正在做的代码

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;
    }
}

并且适配器的类是

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;
    }
}

请帮忙。提前谢谢

4 个答案:

答案 0 :(得分:4)

您需要覆盖toString()的{​​{1}}。

答案 1 :(得分:1)

试试这个......

  @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);



    }

答案 2 :(得分:1)

添加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;
}

答案 3 :(得分:0)

只需将以下内容添加到TeamClass中:

@Override
public String toString() {
    return getTeamName();
}