SQLite数据库中没有项目保存

时间:2014-05-08 21:28:57

标签: android database sqlite android-listview sqliteopenhelper

我尝试将两个字符串保存到sqlite db。我想我已经通过正确的方式创建了所有内容,但是如果我尝试使用保存到db的项目填充listview,则listview中没有显示任何项目。

活动(这里我尝试将字符串添加到数据库中)

public class Ende extends Activity implements android.view.View.OnClickListener {

private String datum = "07.05.2014";
private String schwierigkeit = "Anfänger";

private VerlaufDataSource datasource;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.training_beendet);

    // DB
    datasource = new VerlaufDataSource(this);

    try {
        datasource.open();
        datasource.createEntry(schwierigkeit, datum);
        datasource.close();

    } catch (Exception e) {
        Toast.makeText(this,e.toString(), Toast.LENGTH_LONG).show();
    }

}

扩展SQLiteOpenHelper的活动

private static final String DATABASE_NAME = "verlaufRechnungen.db";
private static final int DATABASE_VERSION = 1;

private static final String TABLE_CREATE_VERLAUF = ""
            + "create table Verlauf("
            +" ID integer primary key autoincrement, "
            + "SCHWIERIGKEIT text, "
            + "DATUM text)"; 

public MySQLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);

    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase database) {
    // TODO Auto-generated method stub
    database.execSQL(TABLE_CREATE_VERLAUF);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    Log.w(MySQLiteHelper.class.getName(), "Upgrading database, which destroy all old data");
    db.execSQL("DROP TABLE IF EXISTS SCANITEM");
    onCreate(db);
}
}

格式化条目的活动:

public class Entry {
    private long id;
    private String schwierigkeit;
    private String datum;


    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getSchwierigkeit() {
        return schwierigkeit;
    }
    public void setSchwierigkeit(String schwierigkeit) {
        this.schwierigkeit = schwierigkeit;
    }
    public String getDatum() {
        return datum;
    }
    public void setDatum(String datum) {
        this.datum = datum;
    }

    @Override
    public String toString(){
        return String.format("Training: %s %s " , schwierigkeit, datum);
    }
}

游标活动:

public class VerlaufDataSource {

    private SQLiteDatabase database;
    private MySQLiteHelper dbhelper;
    private String[] allColumns = {
            "ID", "SCHWIERIGKEIT", "DATUM"  };


    public VerlaufDataSource(Context context) {
        dbhelper = new MySQLiteHelper(context);
    }

    public void open() throws SQLException {
        database = dbhelper.getWritableDatabase();
    }

    public void close() {
        dbhelper.close();
    }

    public Entry createEntry(String schwierigkeit, String datum) {
        ContentValues values = new ContentValues();
        values.put("SCHWIERIGKEIT", schwierigkeit);
        values.put("DATUM", datum);

        long insertId = database.insert("Verlauf", null, values);

        Cursor cursor = database.query("VERLAUF", allColumns, "ID =" + insertId, null, null, null,null);
        cursor.moveToFirst();

        return cursorToEntry(cursor);
    }

    public List<Entry> getAllEntries() {
        List<Entry> EntriesList = new ArrayList<Entry>();
        EntriesList = new ArrayList<Entry>();

        Cursor cursor = database.query("", allColumns, null, null, null, null, null);
        cursor.moveToFirst();

        if(cursor.getCount()==0) return EntriesList;

        while (cursor.isAfterLast() == false) {
        Entry entry = cursorToEntry(cursor);
        EntriesList.add(entry);
        cursor.moveToNext();
        }
        cursor.close();
        return EntriesList;
    }

    private Entry cursorToEntry(Cursor cursor) {
        Entry entry = new Entry();
        entry.setId(cursor.getLong(0));
        entry.setSchwierigkeit(cursor.getString(1));
        entry.setDatum(cursor.getString(2));
        return entry;
    }
}

最后从保存的db:

填充listview的活动
public class Einstellungen extends ActionBarActivity {
    private VerlaufDataSource datasource;
    List<Entry> TrainingList = new ArrayList<Entry>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.trainingstagebuch);


        //DB
        try {
            datasource.open();
            TrainingList = datasource.getAllEntries();
            datasource.close();

        } catch (Exception e) {
            Toast.makeText(this,e.toString(), Toast.LENGTH_LONG).show();
        }

        ArrayAdapter<Entry> adapterVerlauf = new ArrayAdapter<>(Einstellungen.this, android.R.layout.simple_list_item_1, TrainingList);

        ListView Verlauf = (ListView) findViewById(R.id.listview);
        Verlauf.setAdapter(adapterVerlauf);
    }

希望有人能帮忙找到我的错误!感谢

0 个答案:

没有答案