如何使这个对象“训练”为单例对象

时间:2013-12-16 07:38:40

标签: android arraylist

我想将训练对象作为单例对象,因为我想让每个训练会话都进行所有应用程序活动。单击训练列表中的一个项目后,应启动会话。你可以帮忙:))

final Cursor cursor = db.select("SELECT * FROM training");

    while (cursor. moveToNext()) {
        Training training = new Training();
        training.setId(cursor.getInt(cursor

                .getColumnIndex(DatabaseHelper.TRAINING_ID)));

        training.setTitle(cursor.getString(cursor
                .getColumnIndex(DatabaseHelper.TRAINING_TITLE)));

        training.setDescription(cursor.getString(cursor
                .getColumnIndex(DatabaseHelper.TRAINING_DESCRIPTION)));

        training.setDate(cursor.getString(cursor
                .getColumnIndex(DatabaseHelper.TRAINING_DATE)));

        training.setLocation(cursor.getString(cursor
                .getColumnIndex(DatabaseHelper.TRAINING_LOCATION)));

        // Pass to the arraylist
        this.trainingArrayList.add(training);
    }
public ArrayList<Training> getTrainingArrayList() {

    return trainingArrayList;
}

public void setTrainingArrayList(ArrayList<Training> trainingArrayList) {
    this.trainingArrayList = trainingArrayList;
}

1 个答案:

答案 0 :(得分:0)

您可以创建一个名为Training或其他类的单独的类,并声明以下内容:

static Training train = null;
public static Training getInstance(){
    if(train == null){
        train = new Training();
    }
    return train;
}

您可以将Training对象声明为

Training train = nameofclass.getInstance();