我目前正在参加Udacity Android Basics Nanodegree计划。 现在的一项任务是创建一个ReportCard Java类,它允许学校存储学生在特定年份的成绩。 当我提交我的项目并成功通过审核时,我得到了使用ArrayList的建议,而不是对可以给出成绩的科目进行硬编码。 在实施这一建议时,我非常挣扎。我已经在StackOverflow上搜索了几个小时,还没有找到解决问题的答案。
所以,这是我的代码:
package com.example.android.reportcardclass;
/**
* Report Card class
* This class prepares a student report card.
*/
public class ReportCard {
/**
* Class Attributes
*/
private String mStudentName;
private int mYear;
private byte mGerman;
private byte mEnglish;
private byte mMathematics;
private byte mGeography;
private byte mSocialSciencesAndEconomics;
private byte mPhysics;
private byte mReligion;
private byte mPhysEd;
private byte mMusic;
/**
* Default Constructor for ReportCard class
*
* @param studentName Name of the student
* @param year Year in which the student is in
* @param german Mark in the subject German
* @param english Mark in the subject English
* @param mathematics Mark in the subject Mathematic
* @param geography Mark in the subject Geography
* @param socialSciencesAndEconomics Mark in the subject Social Sciences and Economics
* @param physics Mark in the subject Physcics
* @param religion Mark in the subject Religion
* @param physEd Mark in the subject Physical Education
* @param music Mark in the subject Music
*/
public ReportCard(String studentName, int year,
byte german, byte english, byte mathematics,
byte geography, byte socialSciencesAndEconomics,
byte physics, byte religion, byte physEd,
byte music) {
mGerman = german;
mEnglish = english;
mMathematics = mathematics;
mGeography = geography;
mSocialSciencesAndEconomics = socialSciencesAndEconomics;
mPhysics = physics;
mReligion = religion;
mPhysEd = physEd;
mMusic = music;
}
/**
* This method is to get Student Name
*
* @return Student Name
*/
public String getStudentName() {
return mStudentName;
}
/**
* This method is to set Student Name
*
* @param studentName Name of the student
*/
public void setStudentName(String studentName) {
mStudentName = studentName;
}
/**
* This method is to get the year in which the student is in
*
* @return Year in which the student is in
*/
public int getYear() {
return mYear;
}
/**
* This method is to set the year in which the student is in
*
* @param year Year in which the student is in
*/
public void setYear(int year) {
mYear = year;
}
/**
* These are the setter and getter methods for the marks of the student in the respective subject
*/
public byte getGerman() {
return mGerman;
}
public void setGerman(byte german) {
mGerman = german;
}
public byte getEnglish() {
return mEnglish;
}
public void setEnglish(byte english) {
mEnglish = english;
}
public byte getMathematics() {
return mMathematics;
}
public void setMathematics(byte mathematics) {
mMathematics = mathematics;
}
public byte getGeography() {
return mGeography;
}
public void setGeography(byte geography) {
mGeography = geography;
}
public byte getSocialSciencesAndEconomics() {
return mSocialSciencesAndEconomics;
}
public void setSocialSciencesAndEconomics(byte socialSciencesAndEconomics) {
mSocialSciencesAndEconomics = socialSciencesAndEconomics;
}
public byte getPhysics() {
return mPhysics;
}
public void setPhysics(byte physics) {
mPhysics = physics;
}
public byte getReligion() {
return mReligion;
}
public void setReligion(byte religion) {
mReligion = religion;
}
public byte getPhysEd() {
return mPhysEd;
}
public void setPhysEd(byte physEd) {
mPhysEd = physEd;
}
public byte getMusic() {
return mMusic;
}
public void setMusic(byte music) {
mMusic = music;
}
@Override
public String toString() {
return "Student Name: " + getStudentName() + "\nYear: " + getYear() + "German: "
+ getGerman() + "\nEnglish: " + getEnglish() + "\nMathematics: " +
getMathematics() + "\nGeography: " + getGeography() +
"\nSocialSciencesAndEconomics: " + getSocialSciencesAndEconomics() +
"\nPhysics: " + getPhysics() + "\nReligion: " + getReligion() +
"\nPhysical Education: " + getPhysEd() + "\nMusic: " + getMusic();
}
}
我现在要做的是将最初初始化的类属性(mGerman,mEnglish等)存储在ArrayList中。我怎么做?!我非常挣扎,因为我还不想设置任何值,只需初始化字节值(德国的评分系统从0到15点)。
请帮助我,非常感谢你!
答案 0 :(得分:0)
如果您知道将有多少等级,您可以使用以下内容初始化具有background.xml
值的ArrayList:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:bottom="20dp">
<shape android:shape="rectangle" >
<size android:height="100dp" />
<solid android:color="#ffffff" />
</shape>
</item>
<item android:top="300dp">
<shape android:shape="rectangle" >
<size android:height="10dp" />
<solid android:color="#000000" />
</shape>
</item>
更新列表中可以使用的值,例如
Main.xml
答案 1 :(得分:0)
试试这个
ArrayList<ReportCard> mList=new ArrayList<>();
ReportCard reportCard=new ReportCard();
reportCard.setStudentName("student name");
//fill all the details like this
reportCard.setYear("2017");
.
.
mList.add(reportCard);
当你想要获得详细信息时
String studentName=mList.get(position).getStudentName();
答案 2 :(得分:0)
我建议您使用EnumSet
而不是列表来存储成绩,原因如下:
首先使用成绩定义枚举:
import jenkins.*
import jenkins.model.*
def instance = Jenkins.getInstance()
println "--> setting Global properties (Environment variables)..."
def globalProps = hudson.model.Hudson.instance.globalNodeProperties
def props = globalProps.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class)
for (prop in props) {
prop.envVars.put("PATH", "/usr/local/sbin:/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin")
}
instance.save()
println "--> setting Global properties (Environment variables)... done!"
然后,您可以公开添加或删除等级的方法,这些方法将委托给public enum Grade {
GERMAN, ENGLISH, MATHEMATICS, GEOGRAPHIC, SOCIAL_SCIENCES_AND_ECONOMICS,
PHYSICS, RELIGION, PHYSICAL_EDUCATION, MUSIC
}
方法:
EnumSet