我做了2节课
Score
和ScoreDataSource
我在ScoreDataSource#cursorToScore(Cursor cursor)
中发现了一个错误(红色突出显示)。
这是我的完整代码
首先,Score.java
:
public class Score {
private int correct_listening;
private int correct_reading;
private int correct_structure;
private int final_score;
private long id;
private int number;
private String userName;
private long user_id;
public Score(int number, long user_id, int correct_listening, int correct_structure, int correct_reading, int final_score) {
this.number = number;
this.user_id = user_id;
this.correct_listening = correct_listening;
this.correct_structure = correct_structure;
this.correct_reading = correct_reading;
this.final_score = final_score;
}
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
public long getUser_id() {
return this.user_id;
}
public int getNumber() {
return this.number;
}
public void setNumber(int number) {
this.number = number;
}
public void setUser_id(long user_id) {
this.user_id = user_id;
}
public int getCorrect_listening() {
return this.correct_listening;
}
public void setCorrect_listening(int correct_listening) {
this.correct_listening = correct_listening;
}
public int getCorrect_structure() {
return this.correct_structure;
}
public void setCorrect_structure(int correct_structure) {
this.correct_structure = correct_structure;
}
public int getCorrect_reading() {
return this.correct_reading;
}
public void setCorrect_reading(int correct_reading) {
this.correct_reading = correct_reading;
}
public int getFinal_score() {
return this.final_score;
}
public void setFinal_score(int final_score) {
this.final_score = final_score;
}
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String toString() {
return this.number + ". " + this.userName + " | " + this.correct_listening + " | " + this.correct_structure + " | " + this.correct_reading + " | " + this.final_score;
}
public float calculateScore(int listeningScore, int structureScore, int readingScore) {
return (float) Math.round((float) ((((listeningScore + structureScore) + readingScore) / 3) * 10));
}
}
其次,ScoreDataSource.java
。
我在Score score = new Score();
public class ScoreDataSource {
private String[] allColumns;
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
public ScoreDataSource(Context context) {
this.allColumns = new String[]{MySQLiteHelper.USER_ID, MySQLiteHelper.SCORE_USER_ID, MySQLiteHelper.SCORE_LISTENING_CORRECT, MySQLiteHelper.SCORE_STRUCTURE_CORRECT, MySQLiteHelper.SCORE_READING_CORRECT, MySQLiteHelper.SCORE_FINAL};
this.dbHelper = new MySQLiteHelper(context);
}
public void open() throws SQLiteException {
this.database = this.dbHelper.getWritableDatabase();
}
public void close() {
this.database.close();
}
public List<Score> findAll() {
List<Score> score = new ArrayList();
Cursor cursor = this.database.query(MySQLiteHelper.TABLE_SCORE, this.allColumns, null, null, null, null, "final_score DESC");
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
score.add(cursorToScore(cursor));
cursor.moveToNext();
}
cursor.close();
return score;
}
public long count() {
return DatabaseUtils.queryNumEntries(this.database, MySQLiteHelper.TABLE_SCORE);
}
public List<Score> findAll(long id) {
List<Score> score = new ArrayList();
Cursor cursor = this.database.query(MySQLiteHelper.TABLE_SCORE, this.allColumns, "_id = " + id, null, null, null, "final_score DESC");
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
score.add(cursorToScore(cursor));
cursor.moveToNext();
}
cursor.close();
return score;
}
public Score findFirst(long id) {
Cursor cursor = this.database.query(MySQLiteHelper.TABLE_SCORE, this.allColumns, "_id = " + id, null, null, null, null);
cursor.moveToFirst();
Score scores = cursorToScore(cursor);
cursor.close();
return scores;
}
public Score cursorToScore(Cursor cursor) {
Score score = new Score();//<-- error here
score.setId(cursor.getLong(0));
score.setUser_id(cursor.getLong(1));
score.setCorrect_listening(cursor.getInt(2));
score.setCorrect_structure(cursor.getInt(3));
score.setCorrect_reading(cursor.getInt(4));
score.setFinal_score(cursor.getInt(5));
return score;
}
public long create(Score score) {
return this.database.insert(MySQLiteHelper.TABLE_SCORE, null, putValuesToObject(score));
}
public int update(Score score) {
return this.database.update(MySQLiteHelper.TABLE_SCORE, putValuesToObject(score), "_id = " + score.getId(), null);
}
public ContentValues putValuesToObject(Score score) {
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.SCORE_USER_ID, Long.valueOf(score.getUser_id()));
values.put(MySQLiteHelper.SCORE_LISTENING_CORRECT, Integer.valueOf(score.getCorrect_listening()));
values.put(MySQLiteHelper.SCORE_STRUCTURE_CORRECT, Integer.valueOf(score.getCorrect_structure()));
values.put(MySQLiteHelper.SCORE_READING_CORRECT, Integer.valueOf(score.getCorrect_reading()));
values.put(MySQLiteHelper.SCORE_FINAL, Integer.valueOf(score.getFinal_score()));
return values;
}
public int delete(long id) {
return this.database.delete(MySQLiteHelper.TABLE_SCORE, "_id = " + id, null);
}
public int deleteAll() {
return this.database.delete(MySQLiteHelper.TABLE_SCORE, "final_score > 0", null);
}
public void setDefaultData() {
}
}
答案 0 :(得分:1)
Score score = new Score();
好吧,既然你正在调用Score
类的无参数构造函数,那么你应该在Score
类中创建这样的构造函数:
public Score() {
...
}
它不是由编译器生成的,因为您定义了另一个构造函数(public Score(int number, long user_id, int correct_listening, int correct_structure, int correct_reading, int final_score) {...}
)。
答案 1 :(得分:1)
您必须在
中添加此方法Score.java
public Score() {
}
答案 2 :(得分:0)
替换此功能
public Score cursorToScore(Cursor cursor) {
Score score = new Score();//<-- error here
score.setId(cursor.getLong(0));
score.setUser_id(cursor.getLong(1));
score.setCorrect_listening(cursor.getInt(2));
score.setCorrect_structure(cursor.getInt(3));
score.setCorrect_reading(cursor.getInt(4));
score.setFinal_score(cursor.getInt(5));
return score;
}
使用此功能
public Score cursorToScore(Cursor cursor)
{
Score score = new Score(cursor.getLong(0),cursor.getLong(1),cursor.getInt(2),cursor.getInt(3),cursor.getInt(4),cursor.getInt(5));//<-- error here
return score;
}
答案 3 :(得分:0)
构造函数定义如何组合类的对象。
如果定义的类没有构造函数,则隐式生成默认的无参数构造函数,它将为所有属性分配默认值(对于字符串为null,对于基本int和long等为0)
一旦为类显式定义了构造函数,那么默认的无参构造函数就不复存在了。如果要复制默认的无参构造函数机制,则必须显式定义无参数构造函数
public Score() {
}