我试图从tableName使用SELECT *来获取Array列表。但我只是以奇怪的方式获得最后一排。我举例说明了我们。我只有两行数据,我手动放置。假设数据分别是StackOverFlow和Google与Ids 1和2。我得到的只是谷歌和谷歌两次。这是我的代码;
在投资组合类中;
trainingArrayList = new ArrayList<Training>();
//creates and open the database so we can use it
DatabaseHelper db = DatabaseHelper.getInstance();
// Getting a cursor to fetch data from the database
final Cursor cursor = db.select("SELECT * FROM training");
if (cursor.moveToFirst()) {
while (cursor.isAfterLast() == false) {
Training training = Training.getInstance();
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)));
this.trainingArrayList.add(training);
cursor.moveToNext();
}
}
我的培训课程是:
public class Training {
private static Training instance = null;
// global variables
public int id;
public String title;
public String description;
public String date;
public String location;
private Trainer trainer;
private ArrayList<Trainee> traineeArrayList;
private Training(){
//Restrict the constructor from being instantiated
}
public static Training getInstance(){
if(instance == null){
instance = new Training();
}
return instance;
}
// Constructor
public Training(int _id, String title, String description, String date,
String location) {
this.id = _id;
this.title = title;
this.description = description;
this.date = date;
this.location = location;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public Trainer getTrainer() {
return trainer;
}
public Training (Context context) {
DatabaseHelper db = DatabaseHelper.getInstance();
traineeArrayList = new ArrayList<Trainee>();
Cursor cursor = db.select("SELECT * FROM person p JOIN attendance a ON p.person_id = a.person_id WHERE training_id="+this.getId()+";");
while (cursor.moveToNext()) {
Trainee trainee = new Trainee();
trainee.setID(cursor.getInt(cursor.getColumnIndex(DatabaseHelper.PERSON_ID)));
trainee.setFirstname(cursor.getString(cursor.getColumnIndex(DatabaseHelper.PERSON_FIRSTNAME)));
trainee.setLastname(cursor.getString(cursor.getColumnIndex(DatabaseHelper.PERSON_LASTNAME)));
//TODO
// Pass to the arraylist
this.traineeArrayList.add(trainee);
}
}
/* returns the title of training in training ;ist.
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return title;
}
我的主要活动课程:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
portfolio = new Portfolio(this);
trainingListAdapter = new ArrayAdapter<Training>(this,
android.R.layout.simple_list_item_1,
portfolio.getTrainingArrayList());
setListAdapter(trainingListAdapter);
答案 0 :(得分:0)
使用
while (cursor.moveToNext()) {
...
}
光标在第一行之前开始。在第一次迭代中,如果存在,则移动到第一个结果。如果光标为空,或者最后一行已经处理,则循环退出
答案 1 :(得分:0)
使用:
// move the cursor pointer to position zero.
cursor.moveToFirst();
// if there is data after the current cursor position, add it
// to the ArrayList.
if (!cursor.isAfterLast())
{
do
{
// get `training` data
trainingArrayList.add(training);
}
// move the cursor's pointer up one position.
while (cursor.moveToNext());
}
答案 2 :(得分:0)
<强>问题:强>
您要将数据分配到Training
课程中Portfolio
的同一个实例。
Training training = Training.getInstance();
以上行将为您提供相同的实例,并且每次都将此相同的引用添加到ArrayList。由于对象是静态的,这就是为什么最后一次迭代的数据将在Training类变量中更新,因此你得到google和google,因为google是你最后一次迭代的数据。
<强>解决方案:强>
在每次迭代中创建新的Training
对象,如下所示:
Training training = new Training();
答案 3 :(得分:0)
您只是创建了Training对象的一个实例。实际上,您已经实现了单例模式。每次将光标前进到下一条记录时都要创建一个新的Training对象 -
if (cursor.moveToFirst()) {
while (cursor.isAfterLast() == false) {
Training training = new Training(cursor.getInt(cursor
.getColumnIndex(DatabaseHelper.TRAINING_ID)),
cursor.getString(cursor
.getColumnIndex(DatabaseHelper.TRAINING_TITLE))
cursor.getString(cursor
.getColumnIndex(DatabaseHelper.TRAINING_DESCRIPTION))
cursor.getString(cursor
.getColumnIndex(DatabaseHelper.TRAINING_DATE))
cursor.getString(cursor
.getColumnIndex(DatabaseHelper.TRAINING_LOCATION))
);
this.trainingArrayList.add(training);
cursor.moveToNext();
}
public class Training {
private static Training instance = null;
// global variables
public int id;
public String title;
public String description;
public String date;
public String location;
private Trainer trainer;
private ArrayList<Trainee> traineeArrayList;
public Training(int id, String title, String description, String date, String location) {
this.id = id;
this.title = title;
this.description = description;
this.date = date;
this.location = location;
}