我是这种编程的新手。我认为我错过了一些东西,但我不知道我错过了什么。运行模拟器时我没有收到任何错误,并且我能够验证数据是否正在进入数据库。我只是不能让它显示在列表中。列表显示但没有数据。请帮忙!
显示课程.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.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
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();
}
private void setListAdapter(ArrayAdapter<Course> adapter) {
// TODO Auto-generated method stub
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}}
courseDataSource.java
package com.greygoosefarmpa.db;
import java.util.ArrayList;
import java.util.List;
import com.greygoosefarmpa.model.Course;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.*;
import android.util.Log;
public class courseDataSource {
public static final String LOGTAG = "courseDatabase";
SQLiteOpenHelper dbhelper;
SQLiteDatabase database;
private 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");
cursor.moveToFirst();
while(!cursor.isAfterLast()) {
cursorToCourse(cursor);
cursor.moveToNext();
}
cursor.close();
return courses;
}
private Course cursorToCourse(Cursor cursor) {
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)));
return course;
}
}
课程.java
package com.greygoosefarmpa.model;
import java.text.NumberFormat;
import android.R.string;
import android.view.View;
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
package com.greygoosefarmpa.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
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);
}
}