Hallo Community! Ich habe eine eigene Datenbank die ich mit der App mitliefern möchte. (Screenshot angehängt)
Die Daten aus der Tabelle (AD_TAB) möchte ich in eine ListView bzw. ein ListFragment bringen, jedoch komme ich nicht damit weiter wie ich das richtig mache da es mehrere Einträge in der Datenbank sind, und dazu noch wie ich dann abfragen kann auf welche geklickt wurde.
Den Code schreibe ich unten an, ich hoffe ihr könnt mir helfen!
DataAdapter:
Code
package com.simonic.filmstudio;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class DataAdapter {
//Daten AD
public static final String AD_ID = "_id";
public static final String AD_LEVEL = "lvl";
public static final String AD_CUST = "customer";
public static final String AD_PROFIT = "profit";
public static final String AD_MINDEST = "mindest";
public static final String AD_MAX = "maximum";
public static final String AD_DUR = "duration";
public static final String AD_RUNTIME = "runtime";
public static final String[] AD_ALL_KEYS = new String[]{AD_ID, AD_LEVEL, AD_CUST, AD_PROFIT, AD_MINDEST, AD_MAX, AD_DUR, AD_RUNTIME};
//AD Feld Nummern
public static final int AD_KEY_ID = 0;
public static final int AD_KEY_LVL = 1;
public static final int AD_KEY_CUST = 2;
public static final int AD_KEY_PROFIT = 3;
public static final int AD_KEY_MINDEST = 4;
public static final int AD_KEY_MAX = 5;
public static final int AD_KEY_DUR = 6;
public static final int AD_KEY_RUNTIME = 7;
//Tables
public static final String AD_TABLE = "AD_TAB";
// Context of application who uses us.
private final Context context;
private DataBaseHelper myDBHelper;
private SQLiteDatabase db;
/////////////////////////////////////////////////////////////////////
// Public methods:
/////////////////////////////////////////////////////////////////////
public DataAdapter(Context ctx) {
this.context = ctx;
myDBHelper = new DataBaseHelper(context);
}
// Open the database connection.
public DataAdapter open() {
db = myDBHelper.getReadableDatabase();
return this;
}
// Close the database connection.
public void close() {
myDBHelper.close();
}
// Return all data in the database.
public Cursor getAllRows() {
String where = null;
Cursor c = db.query(true, AD_TABLE, AD_ALL_KEYS,
where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
// Get a specific row (by rowId)
public Cursor getRow(long rowId) {
String where = AD_KEY_ID + "=" + rowId;
Cursor c = db.query(true, AD_TABLE, AD_ALL_KEYS,
where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
}
Alles anzeigen
DatabaseHelper:
Code
package com.simonic.filmstudio;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class DataBaseHelper extends SQLiteOpenHelper
{
public static String DATABASE_NAME = "data";
public static final int DATABASE_VERSION = 1;
private static String DB_PATH;
private SQLiteDatabase myDB;
private final Context mContext;
DataBaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
DB_PATH = context.getDatabasePath(DataBaseHelper.DATABASE_NAME).toString();
this.mContext = context;
}
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
}else{
this.getReadableDatabase();
try{
copyDataBase();
}catch(IOException e){
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
checkDB = SQLiteDatabase.openDatabase(DB_PATH, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
}
if(checkDB !=null){
checkDB.close();
}
return checkDB != null ? true : false ;
}
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = mContext.getAssets().open(DATABASE_NAME);
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(DB_PATH);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException {
myDB = SQLiteDatabase.openDatabase(DB_PATH, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
}
}
Alles anzeigen
Mein ListFragment:
Code
package com.simonic.filmstudio;
import android.app.ListFragment;
import android.app.LoaderManager;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import java.util.ArrayList;
import java.util.List;
public class AdvertActivity extends ListFragment{
DataAdapter myDB;
private Context mContext;
private SimpleCursorAdapter adapter;
//Variablen
int id;
int lvl;
String customer;
int profit;
int mindest;
int maximum;
int duration;
int runtime;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
openDB();
}
private void openDB(){
myDB = new DataAdapter(mContext);
myDB.open();
}
@Override
public void onDestroy(){
super.onDestroy();
closeDB();
}
private void closeDB(){
myDB = new DataAdapter(mContext);
myDB.close();
}
public void getAds(){
Cursor cursor = myDB.getAllRows();
displayRecordSet(cursor);
}
private void displayRecordSet(Cursor cursor){
do {
}while(cursor.moveToNext());
if(cursor.moveToFirst()){
do{
id = cursor.getInt(DataAdapter.AD_KEY_ID);
lvl = cursor.getInt(DataAdapter.AD_KEY_LVL);
customer = cursor.getString(DataAdapter.AD_KEY_CUST);
profit = cursor.getInt(DataAdapter.AD_KEY_PROFIT);
mindest = cursor.getInt(DataAdapter.AD_KEY_MINDEST);
maximum = cursor.getInt(DataAdapter.AD_KEY_MAX);
duration = cursor.getInt(DataAdapter.AD_KEY_DUR);
runtime = cursor.getInt(DataAdapter.AD_KEY_RUNTIME);
}while(cursor.moveToNext());
}
cursor.close();
}
/*adapter = new SimpleCursorAdapter(this,
R.layout.ad_list_layout,
null,
new String[]{DataAdapter.AD_LEVEL, DataAdapter.AD_CUST,
DataAdapter.AD_PROFIT, DataAdapter.AD_MINDEST, DataAdapter.AD_MAX,
DataAdapter.AD_DUR, DataAdapter.AD_RUNTIME },
new int[]{R.id.AD_LEVEL, R.id.AD_CUST, R.id.AD_PROFIT, R.id.AD_MINDEST
R.id.AD_MAX, R.id.AD_DUR, R.id.AD_RUNTIME},
0);*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View fraglayout = inflater.inflate(R.layout.advertisment_fragment, null);
return fraglayout;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
Alles anzeigen