Hallo,
will meine erste app schreiben (einfache einkaufsliste). Obwohl daten in meiner sql-lite datenbank drinnen sind (hab mir die datenbank mit sql-liste studio angesehen), bleibt meine einkaufsliste leer.
Hier die relevanten codeteile:
layout fuer die liste:
die rows der liste:
[XML]<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/name_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/product" />
<TextView
android:id="@+id/amount_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/amount" />
</LinearLayout>[/XML]
die liste selber:
[XML]<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/name_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/product" />
<TextView
android:id="@+id/amount_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/amount" />
</LinearLayout>[/XML]
hier will ich die daten abfragen:
hab einfach mal die anfragen in rawQuery reinkopiert - in sql-lite studio werden mit den strings die gewünschten daten abgefragt
und meine view für die liste
Bitte um eure tipps!
Danke!
will meine erste app schreiben (einfache einkaufsliste). Obwohl daten in meiner sql-lite datenbank drinnen sind (hab mir die datenbank mit sql-liste studio angesehen), bleibt meine einkaufsliste leer.
Hier die relevanten codeteile:
layout fuer die liste:
die rows der liste:
[XML]<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/name_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/product" />
<TextView
android:id="@+id/amount_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/amount" />
</LinearLayout>[/XML]
die liste selber:
[XML]<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/name_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/product" />
<TextView
android:id="@+id/amount_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/amount" />
</LinearLayout>[/XML]
hier will ich die daten abfragen:
hab einfach mal die anfragen in rawQuery reinkopiert - in sql-lite studio werden mit den strings die gewünschten daten abgefragt
Java:
package shoppinglist.dao;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.Calendar;
import shoppinglist.db.ShoppingListDatabaseHelper;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;
public class ShoppingListDao extends Activity implements IShoppingListDao{
private ShoppingListDatabaseHelper dbHelper;
private SQLiteDatabase database;
private Context context;
private static final String TAG = "ShoppingListDao";
public static final String KEY_ROWID = "_id";
public static final String KEY_AMOUNT = "menge";
public static final String KEY_NAME = "produkt_name";
public static final String KEY_DONE = "erledigt";
public static final String KEY_TIMESTAMP = "datum_eingabe";
private static final String DATABASE_TABLE = "shoppinglist_item";
private int duration = Toast.LENGTH_SHORT;
/**
* @param context
*/
public ShoppingListDao(Context context) {
this.context = context;
}
public void delete(ShoppingListItem item) {
// TODO Auto-generated method stub
}
public void update(ShoppingListItem item) {
// TODO Auto-generated method stub
}
public void create(ShoppingListItem item) {
ContentValues initialValues = createContentValues(item.getAmount(),item.getName(), item.isOpen());
try{
open();
database.insert(DATABASE_TABLE, null, initialValues);
CharSequence text = "Produkt wurde hinzugefügt!";
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}catch(SQLException sqlE){
CharSequence text = "Produkt konnte nicht hinzugefügt werden!";
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
close();
}
private ContentValues createContentValues(int amount, String name, boolean open) {
ContentValues values = new ContentValues();
values.put(KEY_AMOUNT, amount);
values.put(KEY_NAME, name);
values.put(KEY_DONE, open);
values.put(KEY_TIMESTAMP, new Timestamp((System.currentTimeMillis())).toString());
return values;
}
public ShoppingListDao open() throws SQLException {
dbHelper = new ShoppingListDatabaseHelper(context);
database = dbHelper.getWritableDatabase();
return this;
}
public void close() {
dbHelper.close();
}
public Cursor fetchAllShoppingListItems() {
open();
Cursor cursor = database.rawQuery("select _id, produkt_name, menge, erledigt from shoppinglist_item; ", null);
close();
return cursor;
}
public Cursor fetchAllOpenShoppingListItems() {
Cursor cursor = null;
try{
open();
cursor = database.rawQuery("select _id, produkt_name, menge, erledigt from shoppinglist_item; ", null);
close();
}catch(Exception ex){
CharSequence text = "Die Liste kann nicht erstellt werden!";
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
return cursor;
}
}
und meine view für die liste
Java:
package shoppinglist.ui;
import shoppinglist.dao.IShoppingListDao;
import shoppinglist.dao.ShoppingListDao;
import shoppinglist.main.R;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SimpleCursorAdapter;
public class ShoppingItemsListView extends ListActivity{
IShoppingListDao shoppingListDao;
private Cursor cursor;
private Intent addIntent;
OnClickListener addItem = new OnClickListener() {
public void onClick(View v) {
startActivity(addIntent);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_shopping);
shoppingListDao = new ShoppingListDao(this);
fillData();
addIntent = new Intent(this, ShoppingListNewEntry.class);
Button buttonAdd = (Button) findViewById(R.id.addProduktToList);
buttonAdd.setOnClickListener(addItem);
}
private void fillData() {
cursor = shoppingListDao.fetchAllOpenShoppingListItems();
startManagingCursor(cursor);
String[] from = new String[] {ShoppingListDao.KEY_NAME,ShoppingListDao.KEY_AMOUNT};
int[] to = new int[] { R.id.name_display, R.id.amount_display};
SimpleCursorAdapter entries = new SimpleCursorAdapter(this,
R.layout.list_shopping_row, cursor, from, to);
this.setListAdapter(entries);
}
}
Bitte um eure tipps!
Danke!