vendredi 20 février 2015

SQL and List Adapter

I am trying to learn android and watching and reading tutorials and stuff lead me to SQLite. But I stuck totally. I want to write values in a sql database and save them in a chronic. But when I open the cronic layout my list view only shows a "0". I dont get it. Please have a look:


My MainActivity:



import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import java.util.ArrayList;


public class MainActivity extends ActionBarActivity {

EditText AddCosts;
TextView Paid, Left, ResultText;
ListView Summery;
Button resButton, calculateButton;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AddCosts = (EditText) findViewById(R.id.Addcosts);
Paid = (TextView) findViewById(R.id.Paid);
Left = (TextView) findViewById(R.id.Left);
ResultText = (TextView) findViewById(R.id.ResultText);
resButton = (Button) findViewById(R.id.resButton);
calculateButton = (Button) findViewById(R.id.calculateButton);
dataSource = new VerlaufDataSource(this);


}
ArrayList<Ausgaben> AusgabenListe = new ArrayList<Ausgaben>();
private VerlaufDataSource dataSource;
public void calculate(View view) {


if (AddCosts.getText().toString().length() == 0) return;
//try {
int ausgabe= Integer.parseInt(AddCosts.getText().toString());
System.out.println("das steht jetzt in addCosts: "+ ausgabe);
//}catch (Exception ex){
// Toast.makeText(this,ex.toString(),Toast.LENGTH_SHORT).show();
//}
try {
dataSource.open();
dataSource.createAusgaben(ausgabe);
dataSource.close();
} catch (Exception ex) {
Toast.makeText(this, ex.toString(), Toast.LENGTH_SHORT).show();
}

}


public void verlaufAnzeigen(View view) {
setContentView(R.layout.verlauflayout);
AusgabenListe.clear();
try {
dataSource.open();
AusgabenListe = dataSource.getAllAusgaben();
dataSource.close();
} catch (Exception ex) {
Toast.makeText(this, ex.toString(), Toast.LENGTH_SHORT).show();
}
ArrayAdapter<Ausgaben> adapterVerlauf = new ArrayAdapter<Ausgaben>(MainActivity.this, android.R.layout.simple_list_item_1, AusgabenListe);
Summery = (ListView) findViewById(R.id.Summery);
try {
Summery.setAdapter(adapterVerlauf);
} catch (Exception ex) {
Toast.makeText(this, ex.toString(), Toast.LENGTH_SHORT).show();
}
}

}


MY SQLiteHelper:



import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class MySQLiteHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME="verlaufAusgaben.db";
private static final int DATABASE_VERSION=1;

private static final String TABLE_CREATE_VERLAUF =""
+"create table VERLAUF("
+" ID integer primary key autoincrement, "
+" AUSGEGEBEN int)";

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

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE_VERLAUF);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(MySQLiteHelper.class.getName(), "Upgrading database from version" + oldVersion +" to " + newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS SCAN ITEM");
onCreate(db);
}
}


My database source file



import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


import java.util.ArrayList;



public class VerlaufDataSource {
private SQLiteDatabase database;
private SQLiteOpenHelper dbHelper;
private String[] allColumns={"ID", "AUSGEGEBEN"};

public VerlaufDataSource(Context context){
dbHelper=new MySQLiteHelper(context);
}
public void open(){
database = dbHelper.getWritableDatabase();
}
public void close(){
dbHelper.close();
}
public Ausgaben createAusgaben ( int ausgabe){
System.out.println("Angekommen in create ausgabe: " +ausgabe);
ContentValues values = new ContentValues();
values.put("AUSGEGEBEN", ausgabe);
long insertID = database.insert("VERLAUF", null, values);
Cursor cursor = database.query("VERLAUF", allColumns, "ID = " + insertID, null, null, null,null);
cursor.moveToFirst();
return cursorToAusgaben(cursor);
}


protected ArrayList<Ausgaben> getAllAusgaben(){
ArrayList<Ausgaben> AusgabenListe = new ArrayList<Ausgaben>();
AusgabenListe= new ArrayList<Ausgaben>();

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

cursor.moveToFirst();
if(cursor.getCount()==0)return AusgabenListe;
if(cursor.isAfterLast()==false){
Ausgaben ausgaben = cursorToAusgaben(cursor);
AusgabenListe.add(ausgaben);
cursor.moveToNext();
}
return AusgabenListe;
}
private Ausgaben cursorToAusgaben(Cursor cursor){
Ausgaben ausgaben = new Ausgaben();
ausgaben.setId(cursor.getLong(0));
ausgaben.setAusgegeben(cursor.getInt(1));
return ausgaben;
}
}


And my item class



public class Ausgaben {
private long id;
private int ausgegeben;

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public int getAusgegeben() {
return ausgegeben;
}

public void setAusgegeben(int ausgegeben) {
this.ausgegeben = ausgegeben;
}
@Override
public String toString(){
return String.format("%d", ausgegeben);
}
}


Thank you:)


Aucun commentaire:

Enregistrer un commentaire