0

I am able to get the data into the database. I am not display it when I click display. What am I missing? I am getting the error that allColumns cannot be resolved as a variable in my displayCourse file. I have it declared in my courseDataSource file. I am new to this programming so i'm probably leaving something out. I just dont know what it is.

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);

}

}
user2032899
  • 23
  • 1
  • 6

1 Answers1

0

to be able to get the data from the database and display it in a text view.

I can see your

TextView dc = (TextView) findViewById(R.id.displayCourses);

It should be possible to display data records in the 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
replace all Alocation with alocation and
replace all Blocation with blocation

replace
Log.i(LOGTAG, "returned " ....);
with
Log.i(courseDBOpenHelper.LOGTAG, "returned " ....);

replace
private static final String LOGTAG = "courseDatabase"; with
public static final String LOGTAG = "courseDatabase"; and put it to your
class courseDBOpenHelper public static final String section.

replace
Log.i(LOGTAG, "returned " + cursor.getCount() + " rows");
with
Log.i(courseDBOpenHelper.LOGTAG, "returned " + Integer.toString(cursor.getCount()) + " rows");

We can not see allColumns so,

replace allColumns
database.query(courseDBOpenHelper.TABLE_COURSES, allColumns, null,....);
with null
database.query(courseDBOpenHelper.TABLE_COURSES, null, null,....);

Have a look

sqlite-query-in-android-application
AndroidSQLite
sqlitedatabase-query-method

Community
  • 1
  • 1
moskito-x
  • 11,832
  • 5
  • 47
  • 60
  • I'm still missing something because its not recognizing allColumns and Logtag from my courseDataSource file... – user2032899 Mar 14 '13 at 14:46
  • i cant run it because of the errors with Cursor cursor = datasource.query(courseDBOpenHelper.TABLE_COURSES, allColumns, null, null, null, null, null); Log.i(LOGTAG, "returned " + cursor.getCount() + " rows"); error:cannot be resolved to a variable – user2032899 Mar 14 '13 at 15:14
  • And this fact is not it important for us ? Why do we get this important information only now? Let `Log.i(LOGTAG, "returned " + cursor.getCount() + " rows");` away for the moment. – moskito-x Mar 14 '13 at 15:20
  • ok i commented that out, what about this line... Cursor cursor = datasource.query(courseDBOpenHelper.TABLE_COURSES, allColumns, null, null, null, null, null); it doesnt like allColumns – user2032899 Mar 14 '13 at 15:23
  • i made the change, allColumns still cannot be resolved as a variable. I have it declared in my data source file though. I'm missing the connection. – user2032899 Mar 14 '13 at 16:45
  • Please answer the following inside your question. `I made the change`. what changes you made? Where do you made? `I'm missing the connection`. Where do you miss the connection? – moskito-x Mar 14 '13 at 17:56
  • the change i made was commenting out the logtag line. The connection i am referring to is between my courseDataSource file and my displayCourse file. I have the code in my courseDataSource file for allColumns but its not transferring to displayCourse file. So I'm not sure what i am missing. I'm new at this so I've very much still laerning – user2032899 Mar 14 '13 at 18:47
  • if i replace allColumns with null it gives me an error on datasource – user2032899 Mar 14 '13 at 20:02