我想让GolferDBManager类中的一个方法返回我从 SQLLite db 构造的对象数组。当我尝试构建数组时,方法中出现Source Not Found错误。
public Golfer[] retrieveGolfers(){
String[] columns = new String[]{"golfer_name", "golfer_init", "usga_index"};
Cursor cursor = db.query(true, DB_TABLE, columns, null, null, null, null, null, null, null);
cursor.moveToFirst();
int i = 0;
while (cursor.isAfterLast() == false) {
Golfer tGolfer = new Golfer(cursor.getString(0), cursor.getString(1), cursor.getFloat(2));
lGolfers[i] = new Golfer(cursor.getString(0), cursor.getString(1), cursor.getFloat(2));
cursor.moveToNext();
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
return lGolfers;
}
lGolfers在上面的类定义中定义为:
public class GolferDBManager {
public static final String DB_NAME = "golfgames";
public static final String DB_TABLE = "golfers";
public static final int DB_VERSION = 1;
private static final String CREATE_TABLE = "CREATE TABLE " + DB_TABLE + " (index INTEGER PRIMARY KEY, golfer_name TEXT, golfer_init TEXT, usga_index DOUBLE);";
private SQLHelper helper;
private SQLiteDatabase db;
private Context context;
private Golfer lGolfers[];
这
Golfer tGolfer = new Golfer(cursor.getString(0), cursor.getString(1), cursor.getFloat(2));
线路工作正常。我添加它是出于调试目的。下一行生成Source Not Found错误。我知道 Golfer 类的构造函数在那里,因为我刚刚在上一行中使用过。
这是调用所有这些的代码:
public class ReadGolfers extends Activity {
public Golfer Golfers[];
private GolferDBManager mydManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read_golfers);
mydManager = new GolferDBManager(this);
mydManager.openReadable();
Golfers = mydManager.retrieveGolfers();
mydManager.retrieveGolfers();
mydManager.close();
}
}
谁能帮我理解为什么这不起作用?
谢谢。
作为事后的想法,我更改了方法以将对象数组传递给它,如下所示:
public void retrieveGolfers(Golfer lgolfers[]){
String[] columns = new String[]{"golfer_name", "golfer_init", "usga_index"};
Cursor cursor = db.query(true, DB_TABLE, columns, null, null, null, null, null, null, null);
cursor.moveToFirst();
int i = 0;
while (cursor.isAfterLast() == false) {
lgolfers[i] = new Golfer(cursor.getString(0), cursor.getString(1), cursor.getFloat(2));
cursor.moveToNext();
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
仍然没有喜悦。