Hallo,
also ich verzweifel so langsam an dieser Geschichte
Ich habe folgendes gemacht :
Java
package de.ordertogo;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class AddProduct extends Activity{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addproduct);
}
// This method is called at button click because we assigned the name to the
// "On Click property" of the button
public void onClickButton(View view) {
switch (view.getId()) {
case R.id.buttonProduktHinzufuegen:
showMyDialog();
/*Cursor cur = OrderToGo.helper.selectAllCursor();
startManagingCursor(cur);
Spinner dateSpinner = (Spinner) findViewById(R.id.spinner1);
cur.moveToFirst();
SimpleCursorAdapter spinadapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_spinner_item,
cur,
new String[] { "menge","einheit","name" },
new int[] {android.R.id.text1});
spinadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dateSpinner.setAdapter(spinadapter);*/
break;
case R.id.buttonZurueck:
AddProduct.this.finish();
break;
default:
}
}
public void showMyDialog(){
final EditText editTextProdukt = (EditText) findViewById(R.id.editText_Produkt);
final EditText editTextMenge = (EditText) findViewById(R.id.editText_Menge);
final Spinner spinnerEinheit = (Spinner) findViewById(R.id.spinner_Einheiten);
StringBuilder sb = new StringBuilder("Möchten Sie folgenden Artikel auf die Liste setzen: \n");
sb.append(editTextMenge.getText().toString());
sb.append(" ");
sb.append(spinnerEinheit.getSelectedItem().toString());
sb.append(" ");
sb.append(editTextProdukt.getText().toString());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(sb)
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
OrderToGo.helper.insert(editTextProdukt.getText().toString(), editTextMenge.getText().toString(),spinnerEinheit.getSelectedItem().toString() );
editTextProdukt.setText("Produkt eingeben");
editTextMenge.setText("0");
/*List selectAll = OrderToGo.helper.selectAll();
List<String> names = selectAll;
StringBuilder sb = new StringBuilder();
sb.append("Names in database:\n");
for (String name : names) {
for ( int i = 0 ; i < 2 ; i ++) {
sb.append(name + " ");
}
sb.append("\n");
}
Log.d("EXAMPLE", "names size - " + names.size());
Toast.makeText(AddProduct.this, sb.toString(), Toast.LENGTH_LONG).show();
*/
}
})
.setNegativeButton("Abbruch", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
OrderToGo.helper.deleteAll();
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
Alles anzeigen
Jedesmal, wenn ich das Auskommentierte versuche ans Laufen zu bringen, um den Spinner zu füllen, stürzt die Anwendung ab.
Kann mir vielleicht jemand bitte helfen ?
Java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">
<TextView android:text="TextView" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/editText_Produkt" android:text="@string/Produkt" android:inputType="text"></EditText>
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/editText_Menge" android:text="@string/Menge" android:inputType="number"></EditText>
<Spinner android:layout_width="fill_parent" android:id="@+id/spinner_Einheiten" android:entries="@array/spinnerEinheiten" android:layout_height="wrap_content"></Spinner>
<Button android:text="@string/ButtonHinzufuegen" android:id="@+id/buttonProduktHinzufuegen" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClickButton"></Button>
<Spinner android:id="@+id/spinner1" android:layout_height="wrap_content" android:layout_width="fill_parent"></Spinner>
<Button android:text="@string/ButtonZurueck" android:id="@+id/buttonZurueck" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClickButton"></Button>
</LinearLayout>
Alles anzeigen
Java
package de.ordertogo;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;
import android.widget.Toast;
public class OrderToGoHelper {
private static final String DATABASE_NAME = "example1.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "table1";
private Context context;
private SQLiteDatabase db;
private SQLiteStatement insertStmt;
private static final String INSERT = "insert into " + TABLE_NAME + "(name,menge,einheit) values (?,?,?)";
public OrderToGoHelper(Context context){
this.context = context;
OrderToGoOpenHelper openHelper = new OrderToGoOpenHelper(this.context);
this.db = openHelper.getWritableDatabase();
this.insertStmt = this.db.compileStatement(INSERT);
}
public long insert(String strName, String strMenge, String strEinheit) {
this.insertStmt.bindString(1, strName);
this.insertStmt.bindString(2, strMenge);
this.insertStmt.bindString(3, strEinheit);
Toast.makeText(this.context, strName + " " + strMenge + " " + strEinheit + " gespeichert", Toast.LENGTH_LONG).show();
return this.insertStmt.executeInsert();
}
public void deleteAll() {
this.db.delete(TABLE_NAME, null, null);
}
public List selectAll() {
List liste = new ArrayList();
Cursor cursor = this.db.query(TABLE_NAME, new String[] { "menge","einheit","name" }, null, null, null, null, "name desc");
if (cursor.moveToFirst()) {
do {
liste.add(0,cursor.getString(0));
liste.add(1,cursor.getString(1));
liste.add(2,cursor.getString(2));
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
return liste;
}
public Cursor selectAllCursor() {
return this.db.query(TABLE_NAME, new String[] { "menge","einheit","name" }, null, null, null, null, "name desc");
}
private static class OrderToGoOpenHelper extends SQLiteOpenHelper {
OrderToGoOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,menge TEXT,einheit TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w("Example", "Upgrading database, this will drop tables and recreate.");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); onCreate(db);
}
}
}
Alles anzeigen