在android中读取数据库

时间:2012-04-18 14:40:03

标签: android

我试图从我的数据库回调信息但遇到错误。我写入数据库的第一个类看起来像这样。 (我已经删除了导入和其他非必要代码)

public class AddEntry extends Activity implements OnClickListener{

private TextView mTimeDisplay;
private Button mPickTime;
private int mHour;
private int mMinute;
private EditText mmodulecode; 
private Spinner mSpinner1;
private Spinner mSpinnerDuration;
private Spinner mSpinnerClass;
private EditText mroom;

static final int TIME_DIALOG_ID = 0;

private Spinner spinner1;
private Spinner spinnerNumber;
private Spinner spinnerType;
private EventData events;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addentry);
    events = new EventData(this);

    View addNewEntry = findViewById(R.id.addNewEntry);
    addNewEntry.setOnClickListener(this);


    // capture our View elements
    mTimeDisplay = (TextView) findViewById(R.id.timeDisplay);
    mPickTime = (Button) findViewById(R.id.pickTime);
    mmodulecode = (EditText) findViewById (R.id.modulecode);
    mSpinner1 = (Spinner)findViewById(R.id.spinner1);
    mSpinnerDuration = (Spinner)findViewById(R.id.spinnerNumber);
    mSpinnerClass = (Spinner)findViewById(R.id.spinnerType);
    mroom = (EditText) findViewById (R.id.room);

    // add a click listener to the button
    mPickTime.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(TIME_DIALOG_ID);
        }
    });
}

public void onClick(View v){
    switch (v.getId()){
    case R.id.addNewEntry:
        addEvent(mmodulecode.getText().toString(), mSpinner1.getSelectedItem().toString()
                , mTimeDisplay.getText().toString(), mSpinnerDuration.getSelectedItem().toString()
                , mSpinnerClass.getSelectedItem().toString(), mroom.getText().toString());
        Intent i = new Intent(this, Timetable.class);
        startActivity(i);
        break;
    }
}


private void addEvent (String modulecode, String day, String starttime, String duration, String typeofsession, String room){
    try{
    SQLiteDatabase db = events.getWritableDatabase();
    ContentValues values = new ContentValues ();
    values.put(MODULECODE, modulecode);
    values.put(DAY, day);
    values.put(STARTTIME, starttime);
    values.put(DURATION, duration);
    values.put(TYPEOFSESSION, typeofsession);
    values.put(ROOM, room);
    db.insertOrThrow(TABLE_NAME, null, values);
    }catch(SQLiteException ex){
        Toast toast = Toast.makeText(this, "Broken", 10);
        toast.show();
    }       
}
}

到目前为止,我有理由相信这段代码工作正常,它是在我遇到问题的下一堂课中。这是完整的课程。

package org.example.timetable;

import org.example.timetable.EventData;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import static android.provider.BaseColumns._ID;
import static org.example.timetable.Database.TABLE_NAME;
import static org.example.timetable.Database.MODULECODE;
import static org.example.timetable.Database.DAY;
import static org.example.timetable.Database.STARTTIME;
import static org.example.timetable.Database.DURATION;
import static org.example.timetable.Database.TYPEOFSESSION;
import static org.example.timetable.Database.ROOM;
import android.os.Bundle;
import android.widget.TextView;

public class ViewTimetable extends Activity{
private EventData events;
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewtimetable);
    events = new EventData(this);
    try {
        Cursor cursor = getEvents();
            showEvents(cursor);
    }finally{
        events.close();
    }
}

private static String[] FROM = { _ID, MODULECODE, DAY, STARTTIME, DURATION, TYPEOFSESSION, ROOM };
private static String ORDER_BY = DAY + " DESC";
private Cursor getEvents() {
    SQLiteDatabase db = events.getReadableDatabase();
    Cursor cursor = db.query(TABLE_NAME, FROM, null, null, null, null, ORDER_BY);
    startManagingCursor(cursor);
    return cursor;
}

private void showEvents(Cursor cursor) {
    StringBuilder builder = new StringBuilder("Saved Events:\n");
    while (cursor.moveToNext()){
        long id = cursor.getLong(0);
        String modulecode = cursor.getString(1);
        String day = cursor.getString(2);
        String starttime = cursor.getString(3);
        String duration = cursor.getString(4);
        String typeofsession = cursor.getString(5);
        String room = cursor.getString(6);
        builder.append(id).append(": ");
        builder.append(modulecode).append(": ");
        builder.append(day).append(": ");
        builder.append(starttime).append(": ");
        builder.append(duration).append(": ");
        builder.append(typeofsession).append(": ");
        builder.append(room).append("\n ");
    }
    TextView text = (TextView) findViewById(R.id.text);
    text.setText(builder);
}
}

从我设法发现的情况来看,问题出现了 游标cursor = db.query(TABLE_NAME,FROM,null,null,null,null,ORDER_BY);

我想要它做的就是返回表格中的所有数据。

由于

0 个答案:

没有答案