我是Android和SQLite数据库的新手。我正在编写一个教程,但我无法将其应用到我的应用程序中。
我想为每个级别构建一个高分数的高分数据库。因此,我的表格将被称为HighScore以及字段ID,Level和HighScore。 Level和HighScore是整数。
以下是我的课程:
public class HighScore {
private int id;
private Integer Level;
private Integer highscore;
public HighScore(){}
public HighScore(Integer Level, Integer highscore) {
super();
this.Level = Level;
this.highscore = highscore;
}
//getters & setters
public Integer getHighScore() {
return highscore;
}
public Integer getLevel() {
return Level;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void setLevel(Integer Level) {
this.Level = Level;
}
public void setHighScore(Integer highscore) {
this.highscore = highscore;
}
}
import java.util.LinkedList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class MySQLiteHelper extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "HighScore";
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// SQL statement to create HighScores table
String CREATE_HighScore_TABLE = "CREATE TABLE HighScores ( " +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"Level INTEGER, "+
"HIGHSCORE INTEGER )";
// create HighScores table
db.execSQL(CREATE_HighScore_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older HighScores table if existed
db.execSQL("DROP TABLE IF EXISTS HighScores");
// create fresh books table
this.onCreate(db);
}
//---------------------------------------------------------------------
/**
* CRUD operations (create "add", read "get", update, delete) book + get all books + delete all books
*/
// HighScores table name
private static final String TABLE_HIGHSCORES = "HighScores";
// HighScores Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_LEVEL = "Level";
private static final String KEY_HIGHSCORE = "HIGHSCORE";
private static final String[] COLUMNS = {KEY_ID,KEY_LEVEL,KEY_HIGHSCORE};
public void addHighScore(HighScore highScore){
Log.d("addHighScore", highScore.toString());
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. create ContentValues to add key "column"/value
ContentValues values = new ContentValues();
values.put(KEY_LEVEL, highScore.getLevel()); // get title
values.put(KEY_HIGHSCORE, highScore.getHighScore()); // get author
// 3. insert
db.insert(TABLE_HIGHSCORES, // table
null, //nullColumnHack
values); // key/value -> keys = column names/ values = column values
// 4. close
db.close();
}
public HighScore getHighScore(Integer Level){
// 1. get reference to readable DB
SQLiteDatabase db = this.getReadableDatabase();
// 2. build query
Cursor cursor =
db.query(TABLE_HIGHSCORES, // a. table
COLUMNS, // b. column names
" Level = ?", // c. selections
new String[] { String.valueOf(Level) }, // d. selections args
null, // e. group by
null, // f. having
null, // g. order by
null); // h. limit
// 3. if we got results get the first one
if (cursor != null)
cursor.moveToFirst();
// 4. build highScore object
HighScore highScore = new HighScore();
//highScore.setId(Integer.parseInt(cursor.getString(0)));
//highScore.setLevel(cursor.getInt(1));
highScore.setHighScore(cursor.getInt(2));
Log.d("getHighScore("+Level+")", highScore.toString());
// 5. return highScore
return highScore;
}
// Get All HighScores
public List<HighScore> getAllHighScores() {
List<HighScore> highScores = new LinkedList<HighScore>();
// 1. build the query
String query = "SELECT * FROM " + TABLE_HIGHSCORES;
// 2. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
// 3. go over each row, build highScore and add it to list
HighScore highScore = null;
if (cursor.moveToFirst()) {
do {
highScore = new HighScore();
highScore.setId(Integer.parseInt(cursor.getString(0)));
highScore.setLevel(cursor.getInt(1));
highScore.setHighScore(cursor.getInt(2));
// Add highScore to highScores
highScores.add(highScore);
} while (cursor.moveToNext());
}
Log.d("getAllHighScores()", highScores.toString());
// return highScores
return highScores;
}
// Updating single highScore
public int updateHighScore(HighScore highScore) {
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. create ContentValues to add key "column"/value
ContentValues values = new ContentValues();
values.put("Level", highScore.getLevel()); // get title
values.put("HighScore", highScore.getHighScore()); // get author
// 3. updating row
int i = db.update(TABLE_HIGHSCORES, //table
values, // column/value
KEY_ID+" = ?", // selections
new String[] { String.valueOf(highScore.getId()) }); //selection args
// 4. close
db.close();
return i;
}
// Deleting single highScore
public void deleteHighScore(HighScore highScore) {
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. delete
db.delete(TABLE_HIGHSCORES,
KEY_ID+" = ?",
new String[] { String.valueOf(highScore.getId()) });
// 3. close
db.close();
Log.d("deleteHighScore", highScore.toString());
}
}
然后在我的主类的SetStartupValues中,我有这段代码:
MySQLiteHelper db = new MySQLiteHelper(this);
intHighScore = db.getHighScore(intLevel);
我想要做的是当SetStartUpValues方法运行时,我可以从表中获得用户intLevel的高分。但是intHighScore是一个整数,getHighScore方法返回一个HighScore对象。如何从HighScore对象中获取1个字段值?
答案 0 :(得分:0)
只需将getHighScore更改为以下
即可import UIKit
class ViewController: UIViewController, UIPopoverControllerDelegate, UIPopoverPresentationControllerDelegate {
//declare variables
var popOverLocation: CGRect!
// Set outlets for all buttons
@IBAction func update(sender: AnyObject) {
texttest.text = newValue
}
@IBOutlet weak var texttest: UITextField!
@IBAction func testtexttouch(sender: AnyObject) {
texttest.resignFirstResponder()
popOverLocation = texttest.frame
popOver()
texttest.text = newValue
texttest.resignFirstResponder()
}
func update() {
texttest.text = newValue
}
//function for creating the popover
func popOver() {
//Read the height, and XY coordinates of the active button
var h = popOverLocation.height
var x = popOverLocation.origin.x
var y = popOverLocation.origin.y + h
//create the popover
var popoverContent = self.storyboard!.instantiateViewControllerWithIdentifier("Popover") as UIViewController
var nav = UINavigationController(rootViewController: popoverContent)
nav.modalPresentationStyle = UIModalPresentationStyle.Popover
var popover = nav.popoverPresentationController! as UIPopoverPresentationController
popover.sourceView = self.view
popover.delegate = self
//set the size of the popover
popoverContent.preferredContentSize = CGSizeMake(200,300);
//set the direction the popover arrow is allowed to point
popover.permittedArrowDirections = UIPopoverArrowDirection.Up
//using the buttons coordinates set the location of the popover
popover.sourceRect = CGRectMake(x,y,0,0)
self.presentViewController(nav, animated: true, completion: nil)
}
}
不要创建public int getHighScore(Integer Level){
// 1. get reference to readable DB
SQLiteDatabase db = this.getReadableDatabase();
// 2. build query
Cursor cursor =
db.query(TABLE_HIGHSCORES, // a. table
COLUMNS, // b. column names
" Level = ?", // c. selections
new String[] { String.valueOf(Level) }, // d. selections args
null, // e. group by
null, // f. having
null, // g. order by
null); // h. limit
// 3. if we got results get the first one
if (cursor != null)
cursor.moveToFirst();
// 4. return highScore
return cursor.getInt(2);
}
对象,只需返回HighScore
的{{1}}值。