P
paxis
Fortgeschrittenes Mitglied
- 0
Hallo Leute,
ich bin gerade dabei mich in SQL einzuarbeiten und wollte eine kleine ToDoList App schreiben
!
Jetzt erstmal mein Code:
hauptklasse:
SQKlasse:
Klasse Aufgabe:
Und die XML Datei:
Zur Info, habe mich an meinem Lehrbuch orientiert
!
Folgendes Problem tritt auf:
Wenn ich die APP im emulator starten will, komtm nichts nur die Meldung das er sich unerwartet beendet hat!
Wäre super wenn mir hier jemand helfen könnte oder vlt. auf ein anderes Forum verweisen könnte, wo vlt. mehr entwicklter unterwegs sind!
danke schonmal
))
paxis
ich bin gerade dabei mich in SQL einzuarbeiten und wollte eine kleine ToDoList App schreiben

Jetzt erstmal mein Code:
hauptklasse:
Code:
package de.tobitodolist;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;
import de.tobitodolist.R.id;
import android.R.string;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.text.Editable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
public class MainActivity<tobi> extends Activity {
private DBZugriff dbZugriff;
private ListView anzeigeListe;
private SimpleCursorAdapter adapter;
private Cursor cursor;
private String tobi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) this.findViewById(R.id.button1);
button.setOnClickListener((OnClickListener) this);
dbZugriff = new DBZugriff(null, null);
EditText test2 = (EditText) this.findViewById(R.id.editText1);
tobi = test2.toString();
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.button1 :
Aufgabe test = new Aufgabe(tobi,1);
dbZugriff.datensatzEinfügen(test);
case R.id.button2 :
TextView test3 = (TextView) this.findViewById(R.id.textView1);
List<Aufgabe> test5 = dbZugriff.gibAlle();
test3.setText(getString(test5));
}
}
private CharSequence getString(List<Aufgabe> test5) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
package de.tobitodolist;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBZugriff extends SQLiteOpenHelper{
private SQLiteDatabase db;
private String tabelle;
public DBZugriff(Context activity, String dbName) {
super(activity, dbName, null, 1);
db = getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
try{
String sql = "CREATE TABLE aufgaben" +
"(id INTEGER PRIMARY JEY AUTONINCREMENT," +
"aufgabe string " +
"priorität int)";
db.execSQL(sql);
}
catch(Exception ex) {
Log.e("Test", ex.getMessage());
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public synchronized void close() {
if(db != null){
db.close();
db = null;
}
super.close();
}
public Aufgabe erstellen(Cursor cursor) {
Aufgabe af = new Aufgabe(tabelle, 0);
af.id = cursor.getLong(0);
af.aufgabe = cursor.getString(0);
af.priorität = cursor.FIELD_TYPE_INTEGER;
return af;
}
public long datensatzEinfügen(Aufgabe af) {
try {
ContentValues daten = erzeugeDatenObjekt(af);
return db.insert(tabelle, null, daten); // id wird automatisch von SQLite gefüllt
}
catch(Exception ex) {
Log.d("carpelibrum", ex.getMessage());
return -1;
}
}
private ContentValues erzeugeDatenObjekt(Aufgabe af) {
// TODO Auto-generated method stub
return null;
}
public void loeschen(Aufgabe af) {
db.delete("freunde", "id" + af.id, null);
}
public List<Aufgabe> gibAlle(){
List<Aufgabe> ergebnis = new ArrayList<Aufgabe>();
Cursor cursor = null;
try {
cursor = db.query(tabelle, null, null, null, null, null, null);
int anzahl = cursor.getCount();
cursor.moveToFirst();
for(int i = 0; i < anzahl; i++) {
Aufgabe ds = erstellen(cursor);
ergebnis.add(ds);
cursor.moveToNext();
}
}
catch(Exception ex) {
Log.e("carpelibrum", ex.getMessage());
}
finally {
// egal ob Erfolg oder Exception:: cursor schließen
if(cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
return ergebnis; }
}
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBZugriff extends SQLiteOpenHelper{
private SQLiteDatabase db;
private String tabelle;
public DBZugriff(Context activity, String dbName) {
super(activity, dbName, null, 1);
db = getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
try{
String sql = "CREATE TABLE aufgaben" +
"(id INTEGER PRIMARY JEY AUTONINCREMENT," +
"aufgabe string " +
"priorität int)";
db.execSQL(sql);
}
catch(Exception ex) {
Log.e("Test", ex.getMessage());
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public synchronized void close() {
if(db != null){
db.close();
db = null;
}
super.close();
}
public Aufgabe erstellen(Cursor cursor) {
Aufgabe af = new Aufgabe(tabelle, 0);
af.id = cursor.getLong(0);
af.aufgabe = cursor.getString(0);
af.priorität = cursor.FIELD_TYPE_INTEGER;
return af;
}
public long datensatzEinfügen(Aufgabe af) {
try {
ContentValues daten = erzeugeDatenObjekt(af);
return db.insert(tabelle, null, daten); // id wird automatisch von SQLite gefüllt
}
catch(Exception ex) {
Log.d("carpelibrum", ex.getMessage());
return -1;
}
}
private ContentValues erzeugeDatenObjekt(Aufgabe af) {
// TODO Auto-generated method stub
return null;
}
public void loeschen(Aufgabe af) {
db.delete("freunde", "id" + af.id, null);
}
public List<Aufgabe> gibAlle(){
List<Aufgabe> ergebnis = new ArrayList<Aufgabe>();
Cursor cursor = null;
try {
cursor = db.query(tabelle, null, null, null, null, null, null);
int anzahl = cursor.getCount();
cursor.moveToFirst();
for(int i = 0; i < anzahl; i++) {
Aufgabe ds = erstellen(cursor);
ergebnis.add(ds);
cursor.moveToNext();
}
}
catch(Exception ex) {
Log.e("carpelibrum", ex.getMessage());
}
finally {
// egal ob Erfolg oder Exception:: cursor schließen
if(cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
return ergebnis; }
}
Klasse Aufgabe:
Code:
package de.tobitodolist;
public class Aufgabe {
public long id;
public String aufgabe;
public int priorität;
public Aufgabe(String aufgabe, int priorität) {
this.aufgabe = aufgabe;
id= -1;
}}
Code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="27dp"
android:layout_marginTop="26dp"
android:text="Button" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button1"
android:layout_marginLeft="60dp"
android:layout_toRightOf="@+id/button1"
android:text="TextView" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="19dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/editText1"
android:layout_marginTop="16dp"
android:text="Button" />
</RelativeLayout>
Zur Info, habe mich an meinem Lehrbuch orientiert
Folgendes Problem tritt auf:
Wenn ich die APP im emulator starten will, komtm nichts nur die Meldung das er sich unerwartet beendet hat!

Wäre super wenn mir hier jemand helfen könnte oder vlt. auf ein anderes Forum verweisen könnte, wo vlt. mehr entwicklter unterwegs sind!
danke schonmal
paxis
Zuletzt bearbeitet: