Question

I try to save two strings to a sqlite db. I think I've created all by the right way, but if i try to fill a listview with the items I saved to db, there aren't any items shown in the listview.

Activity (here i try to add the strings to the db)

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

}

Activity that extends 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);
}
}

Activity to format the entry:

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

Activity for cursor:

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

At last the activity to fill listview from saved db:

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

Now I want to know, how I use print to log, because I want to know, if I fill the db correctly.

Était-ce utile?

La solution

to learn how to log read this

for example put this to your getAllEntries() method before return statement ...

Log.w("VerlaufDataSource","The size of return list from databaze is: " + EntriesList.size());

also try to put this after you catch exception in Einstellungen class

List<String> list = new ArrayList<String>();

for(Entry e: TrainingList){
   list.add(e.getSchwierigkeit());
}

 ArrayAdapter<String> adapterVerlauf = new ArrayAdapter<>(Einstellungen.this, android.R.layout.simple_list_item_1, list);
 ListView Verlauf = (ListView) findViewById(R.id.listview);
 Verlauf.setAdapter(adapterVerlauf);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top