我能够将数据导入数据库。单击显示时,我不显示它。我错过了什么?我收到的错误是allColumns无法解析为我的displayCourse文件中的变量。我在我的courseDataSource文件中声明了它。我是这个编程的新手,所以我可能会遗漏一些东西。我只是不知道它是什么。
displayCourse.java
package com.greygoosefarmpa.fencecoursebuilder;
import java.util.ArrayList;
import java.util.List;
import com.google.android.maps.MapActivity;
import com.greygoosefarmpa.db.courseDBOpenHelper;
import com.greygoosefarmpa.db.courseDataSource;
import com.greygoosefarmpa.model.Course;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class displayCourse extends MapActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.displaycourse);
//TextView dc = (TextView) findViewById(R.id.displayCourses);
courseDataSource datasource = new courseDataSource(this);
datasource.open();
List<Course> course = datasource.findAll();
ArrayAdapter<Course> adapter = new ArrayAdapter<Course> (this,android.R.layout.simple_list_item_1, course);
setListAdapter(adapter);
datasource.close();
courseDataSource.java
public class courseDataSource {
public static final String LOGTAG = "courseDatabase";
SQLiteOpenHelper dbhelper;
SQLiteDatabase database;
public static final String[] allColumns = {
courseDBOpenHelper.COLUMN_ALOCATION,
courseDBOpenHelper.COLUMN_BLOCATION,
courseDBOpenHelper.COLUMN_COURSENAME,
courseDBOpenHelper.COLUMN_FENCENAME,
courseDBOpenHelper.COLUMN_ID
};
public courseDataSource(Context context) {
dbhelper = new courseDBOpenHelper(context);
database = dbhelper.getWritableDatabase();
}
public void open(){
Log.i(LOGTAG, "database open");
database = dbhelper.getWritableDatabase();
}
public void close(){
Log.i(LOGTAG, "database closed");
dbhelper.close();
}
public Course create(Course course) {
ContentValues values = new ContentValues();
values.put(courseDBOpenHelper.COLUMN_COURSENAME, course.getcourseid());
values.put(courseDBOpenHelper.COLUMN_FENCENAME, course.getcoursename());
values.put(courseDBOpenHelper.COLUMN_ALOCATION, course.getAlocation());
values.put(courseDBOpenHelper.COLUMN_BLOCATION, course.getBlocation());
long insertid = database.insert(courseDBOpenHelper.TABLE_COURSES, null, values);
course.setcourseid(insertid);
return course;
}
public List<Course> findAll() {
List<Course> courses = new ArrayList<Course>();
Cursor cursor = database.query(courseDBOpenHelper.TABLE_COURSES, allColumns, null, null, null, null, null);
Log.i(LOGTAG, "returned " + cursor.getCount() + " rows");
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
Course course = new Course();
course.setcourseid(cursor.getLong(cursor.getColumnIndex(courseDBOpenHelper.COLUMN_ID)));
course.setAlocation(cursor.getString(cursor.getColumnIndex(courseDBOpenHelper.COLUMN_ALOCATION)));
course.setBlocation(cursor.getString(cursor.getColumnIndex(courseDBOpenHelper.COLUMN_BLOCATION)));
course.setcoursename(cursor.getString(cursor.getColumnIndex(courseDBOpenHelper.COLUMN_COURSENAME)));
course.setfencename(cursor.getString(cursor.getColumnIndex(courseDBOpenHelper.COLUMN_FENCENAME)));
courses.add(course);
}
}
return courses;
}
}
Course.java
public class Course {
private long courseid;
private String coursename;
private String fencename;
private String Alocation;
private String Blocation;
public long getcourseid(){
return courseid;
}
public void setcourseid(long courseid) {
this.courseid = courseid;
}
public String getcoursename(){
return coursename;
}
public void setcoursename(String string){
this.coursename = string;
}
public String getfencename(){
return fencename;
}
public void setfencename(String fencename){
this.fencename = fencename;
}
public String getAlocation(){
return Alocation;
}
public void setAlocation(String string){
this.Alocation = string;
}
public String getBlocation(){
return Blocation;
}
public void setBlocation(String string){
this.Blocation = string;
}
}
courseDBOpenHelper.java
public class courseDBOpenHelper extends SQLiteOpenHelper {
private static final String LOGTAG = "courseDatabase";
private static final String DATABASE_NAME = "courses.db";
private static final int DATABASE_VERSION = 1;
public static final String TABLE_COURSES = "courses";
public static final String COLUMN_ID = "coursesID";
public static final String COLUMN_COURSENAME = "coursename";
public static final String COLUMN_FENCENAME = "fencename";
public static final String COLUMN_ALOCATION = "alocation";
public static final String COLUMN_BLOCATION = "blocation";
private static final String TABLE_CREATE =
"CREATE TABLE " + TABLE_COURSES + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_COURSENAME + " TEXT, " +
COLUMN_FENCENAME + " TEXT, " +
COLUMN_ALOCATION + " NUMERIC, " +
COLUMN_BLOCATION + " NUMBERIC " +
")";
public courseDBOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
Log.i(LOGTAG, "table created");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + TABLE_COURSES);
onCreate(db);
}
}
答案 0 :(得分:0)
能够从数据库中获取数据并在文本视图中显示。
我可以看到你的
TextView dc = (TextView) findViewById(R.id.displayCourses);
应该可以在TextView中显示数据记录。
[...]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.displaycourse);
TextView dc = (TextView) findViewById(R.id.displayCourses);
courseDataSource datasource = new courseDataSource(this);
datasource.open();
Cursor cursor = database.query(courseDBOpenHelper.TABLE_COURSES,
null, null, null, null, null, null);
Log.i(courseDBOpenHelper.LOGTAG, "returned " + Integer.toString(cursor.getCount()) + " rows");
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String coursename = cursor.getString(cursor.getColumnIndex(courseDBOpenHelper.COLUMN_COURSENAME));
dc.append("Name: ");
dc.append(coursename);
dc.append("\n");
// more content
}
}
[...]
EDIT
将所有Alocation
替换为alocation
和
将所有Blocation
替换为blocation
替换
Log.i(LOGTAG, "returned " ....);
与
Log.i(courseDBOpenHelper.LOGTAG, "returned " ....);
替换
private static final String LOGTAG = "courseDatabase";
与
public static final String LOGTAG = "courseDatabase";
并将其发送给您
class courseDBOpenHelper public static final String
部分。
替换
Log.i(LOGTAG, "returned " + cursor.getCount() + " rows");
与
Log.i(courseDBOpenHelper.LOGTAG, "returned " + Integer.toString(cursor.getCount()) + " rows");
我们看不到allColumns
,
替换allColumns
database.query(courseDBOpenHelper.TABLE_COURSES, allColumns, null,....);
与null
database.query(courseDBOpenHelper.TABLE_COURSES, null, null,....);
看看
sqlite-query-in-android-application
AndroidSQLite
sqlitedatabase-query-method