T
theboss22
Ambitioniertes Mitglied
- 0
Hey,
habe für meine App eine Datenbank erstellt und möchte die Daten aus der Datenbank nun in einer ListView (ähnlich wie diese hier: http://i.stack.imgur.com/OlaAl.png) ausgeben. Habe mir jetzt schon verschiedene Tutorials dazu angeguckt wie man eine List View erstellt und habe es auch schon geschafft diese so umzusetzen, das sie in meinem emulator funktionieren, jedoch habe ich noch keine Idee, wie ich die Verbindung zu meiner SQL-Datenbank realisieren kann. Vielen Dank für Hilfe im voraus!
Ps: wenn das von Relevanz ist kann ich auch gerne die Codes für die ListView posten
Meine Datenbank enthält folgende drei Activities:
DatabaseHandler:
AndroidSQLLiteTutorialActivity:
Position.java:
habe für meine App eine Datenbank erstellt und möchte die Daten aus der Datenbank nun in einer ListView (ähnlich wie diese hier: http://i.stack.imgur.com/OlaAl.png) ausgeben. Habe mir jetzt schon verschiedene Tutorials dazu angeguckt wie man eine List View erstellt und habe es auch schon geschafft diese so umzusetzen, das sie in meinem emulator funktionieren, jedoch habe ich noch keine Idee, wie ich die Verbindung zu meiner SQL-Datenbank realisieren kann. Vielen Dank für Hilfe im voraus!
Ps: wenn das von Relevanz ist kann ich auch gerne die Codes für die ListView posten
Meine Datenbank enthält folgende drei Activities:
DatabaseHandler:
Code:
package com.example.fotooh2;
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;
public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 5;
// Database Name
private static final String DATABASE_NAME = "PositionManager";
// Positions table name
private static final String TABLE_POSITIONS = "positions";
// Positions Table Columns names
private static final String KEY_ID = "_id";
private static final String KEY_NAME = "Name";
private static final String KEY_KATEGORIE = "Kategorie";
private static final String KEY_LAENGE = "Laenge";
private static final String KEY_BREITE = "Breite";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_POSITIONS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_KATEGORIE + " TEXT," + KEY_LAENGE + " REAL," + KEY_BREITE
+ " REAL" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_POSITIONS);
// Create tables again
onCreate(db);
}
/**
*
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Adding new contact
void addPosition(Position position) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, position.getName()); // Name
values.put(KEY_KATEGORIE, position.getKategorie()); // Kategorie
values.put(KEY_LAENGE, position.getLaenge()); // Länge
values.put(KEY_BREITE, position.getBreite()); // Breite
// Inserting Row
db.insert(TABLE_POSITIONS, null, values);
db.close(); // Closing database connection
}
// Getting single position
Position getPosition(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_POSITIONS, new String[] { KEY_ID,
KEY_NAME, KEY_KATEGORIE, KEY_LAENGE, KEY_BREITE }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Position position = new Position(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2), cursor.getFloat(3),
cursor.getFloat(4));
// return position
return position;
}
// Getting All Positions
public List<Position> getAllPositions() {
List<Position> positionList = new ArrayList<Position>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_POSITIONS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Position position = new Position();
position.setID(Integer.parseInt(cursor.getString(0)));
position.setName(cursor.getString(1));
position.setKategorie(cursor.getString(2));
position.setLaenge(cursor.getFloat(3));
position.setBreite(cursor.getFloat(4));
// Adding position to list
positionList.add(position);
} while (cursor.moveToNext());
}
// return position list
return positionList;
}
// Updating single position
public int updatePosition(Position position) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, position.getName());
values.put(KEY_KATEGORIE, position.getKategorie());
values.put(KEY_LAENGE, position.getLaenge());
values.put(KEY_BREITE, position.getBreite());
// updating row
return db.update(TABLE_POSITIONS, values, KEY_ID + " = ?",
new String[] { String.valueOf(position.getID()) });
}
// Deleting single position
public void deletePosition(Position position) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_POSITIONS, KEY_ID + " = ?",
new String[] { String.valueOf(position.getID()) });
db.close();
}
// Getting positions Count
public int getPositionsCount() {
String countQuery = "SELECT * FROM " + TABLE_POSITIONS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int resultCount = cursor.getCount();
cursor.close();
// return count
return resultCount;
}
}
AndroidSQLLiteTutorialActivity:
Code:
package com.example.fotooh2;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class AndroidSQLiteTutorialActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DatabaseHandler db = new DatabaseHandler(this);
/**
* CRUD Operations
* */
// Inserting Positions
Log.d("Insert: ", "Inserting ..");
db.addPosition(new Position("Hobbersdorfer Felder", "Landschaft" , 53.94966, 10.70343));
db.addPosition(new Position("Felder bei Pelzerhaken", "Landschaft", 54.10039, 10.83878));
db.addPosition(new Position("Felder bei Neustadt", "Landschaft" , 54.10713, 10.84201));
db.addPosition(new Position("Felder bei Gronenberg", "Landschaft" , 54.04454, 10.69646));
db.addPosition(new Position("Felder bei Stolpe", "Landschaft" , 54.15785, 10.78290));
db.addPosition(new Position("Felder bei Ruhleben", "Landschaft" , 54.11740, 10.88896));
db.addPosition(new Position("Niendorfer Hafen: ", "Häfen" , 53.99321, 10.81323));
db.addPosition(new Position("Neustädter Hafen: ", "Häfen" , 54.10509, 10.81117));
// Reading all positions
Log.d("Reading: ", "Reading all positions..");
List<Position> positions = db.getAllPositions();
for (Position cn : positions) {
String log = "Id: "+cn.getID()+" ,Name: " + cn.getName() +" ,Kategorie: " + cn.getKategorie() + " ,Länge: " + cn.getLaenge()+" ,Breite: " + cn.getBreite();
// Writing Contacts to log
Log.d("Name: ", log);
}
}
}
Position.java:
Code:
package com.example.fotooh2;
public class Position{
//private variables
int _id;
String name;
String kategorie;
double laenge;
double breite;
// Empty constructor
public Position(){
}
// constructor
public Position(int id, String name, String kategorie, double laenge, double breite){
this._id = id;
this.name = name;
this.kategorie = kategorie;
this.laenge = laenge;
this.breite = breite;
}
// constructor
public Position(String name, String kategorie, double laenge, double breite){
this.name = name;
this.kategorie = kategorie;
this.laenge = laenge;
this.breite = breite;
}
// getting ID
public int getID(){
return this._id;
}
// setting id
public void setID(int id){
this._id = id;
}
// getting name
public String getName(){
return this.name;
}
// setting name
public void setName(String name){
this.name = name;
}
// getting category
public String getKategorie(){
return this.kategorie;
}
// setting category
public void setKategorie(String kategorie){
this.kategorie = kategorie;
}
// getting Laenge
public double getLaenge(){
return this.laenge;
}
// setting LAENGE
public void setLaenge(double laenge){
this.laenge = laenge;
}
// getting Breite
public double getBreite(){
return this.breite;
}
// setting BreiteE
public void setBreite(double breite){
this.breite = breite;
}
@Override
public String toString() {
return "Position [ laenge=" + laenge + ", breite=" + breite
+ "]";
}
}