# PriceScannerApp: warum wird nach dem Scannen Display gleich schwarz?



## nata (25. Aug 2014)

Hallo Leute,

Ich habe 2 Activity Klassen und für Datenbank Product , MySQLiteHelper und ProductDataSource.
Wenn ich auf Scan Product drücke wird Scannen gestartet, aber gleich wird Display schwarz. Woran liegt mein Fehler?



```
public class MainActivity extends Activity {
    public static final String EXTRA_BARCODE = "extraBarcode";
    public static final String EXTRA_FORMAT = "extraFormat";
    public final static String EXTRA_MESSAGE = "com.example.pricescannerapp.MESSAGE";
 
    public boolean mainIsOpen = true;
 
    private IntentIntegrator mIntentIntegrator;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        this.mIntentIntegrator = new IntentIntegrator(this);
 
    }
 
    public void scanProduct(View view) {
 
        this.mIntentIntegrator.initiateScan();
 
    }
 
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        IntentResult scanResult = IntentIntegrator.parseActivityResult(
                requestCode, resultCode, intent);
 
        if (scanResult != null) {
            Intent intent1 = new Intent(this, EnterProductDetailActivity.class);
            intent1.putExtra(EXTRA_BARCODE, scanResult.getContents());
            intent1.putExtra(EXTRA_FORMAT, scanResult.getFormatName());
            startActivity(intent1);
 
        }
 
        try {
 
        } catch (Exception ex) {
            Toast.makeText(this, ex.toString(), Toast.LENGTH_LONG).show();
        }
    }
 
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
 
        if (keyCode == KeyEvent.KEYCODE_BACK && mainIsOpen == false) {
            mainIsOpen = true;
            setContentView(R.layout.activity_main);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
 
}
```


```
public class EnterProductDetailActivity extends Activity {
 
    SQLiteDatabase database;
    MySQLiteHelper dbHelper;
 
    List<Product> productsList = new ArrayList<Product>();
 
    private ProductDataSource datasource;
   
    String barcode;
    String format;
    EditText editbarcode;
    EditText editformat;
 
    boolean mainIsOpen;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enter_product_detail);
 
        // Instantiate own SQlite helper class
        dbHelper = new MySQLiteHelper(this);
        database = dbHelper.getWritableDatabase();
       
       
        datasource = new ProductDataSource(this);
 
        // Get the message from the intent
        Intent intent = getIntent();
        barcode = intent.getStringExtra(MainActivity.EXTRA_BARCODE);
        format = intent.getStringExtra(MainActivity.EXTRA_FORMAT);
        editbarcode = (EditText) findViewById(R.id.etBarcode);
        editformat = (EditText) findViewById(R.id.etType);
 
        editbarcode.setText(barcode);
        editformat.setText(format);
 
    }
 
 
    public void showProducts(View view) {
        setContentView(R.layout.activity_formular_filled);
        mainIsOpen = false;
       
        productsList.clear();
        try {
            datasource.open();
            productsList = datasource.getAllProducts();
            datasource.close();
        } catch (Exception ex) {
            Toast.makeText(this, ex.toString(), Toast.LENGTH_SHORT).show();
        }
 
        ArrayAdapter<Product> adapterProduct = new ArrayAdapter<Product>(
                EnterProductDetailActivity.this, android.R.layout.simple_list_item_1,
                productsList);
 
        ListView lVerlauf = (ListView) findViewById(R.id.listView1);
        lVerlauf.setAdapter(adapterProduct);
 
    }
   
 
   
    public void saveProduct(View view) {
 
        dbHelper = new MySQLiteHelper(this);
        database = dbHelper.getWritableDatabase();
       
        try {
            datasource.open();
            datasource.createProduct(barcode, format);
            datasource.close();
        } catch (Exception ex) {
            Toast.makeText(this, ex.toString(), Toast.LENGTH_LONG).show();
        }
       
    }
 
   
}
```


```
public class MySQLiteHelper extends SQLiteOpenHelper {
 
    private static final String DATABASE_NAME = "products.db";
    private static final int DATABASE_VERSION = 11;
 
    private static final String TEXT_TYPE = " TEXT";
    private static final String COMMA_SEP = ", ";
   
   
    public static final String TABLE_NAME = "PRODUCTS ";
    public static final String PRODUCT_ID = "ID ";
    public static final String COLOMN_BARCODE = "BARCODE ";
    public static final String COLOMN_TYPE = "TYPE ";
   
   
   
    private static final String CREATE_TABLE_PRODUCTS = "CREATE TABLE "
            + TABLE_NAME + " (" + PRODUCT_ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT" + COMMA_SEP + COLOMN_BARCODE
            + TEXT_TYPE + COMMA_SEP + COLOMN_TYPE
            + TEXT_TYPE +
            " )";
 
 
    private static final String SQL_DELETE_PRODUCTS = "DROP TABLE IS EXISTS" + TABLE_NAME;
 
    public MySQLiteHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
 
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE_PRODUCTS);
 
    }
 
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(SQL_DELETE_PRODUCTS);
        onCreate(db);
 
    }
   
    @Override
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onUpgrade(db, oldVersion, newVersion);
    }
 
}
```


```
public class ProductDataSource {
    SQLiteDatabase database;
    MySQLiteHelper dbHelper;
    private String[] allColumns = { MySQLiteHelper.PRODUCT_ID,
            MySQLiteHelper.COLOMN_BARCODE, MySQLiteHelper.COLOMN_TYPE };
 
    public ProductDataSource(Context context) {
        // TODO Auto-generated constructor stub
        dbHelper = new MySQLiteHelper(context);
    }
 
    public void open() throws Exception {
        database = dbHelper.getWritableDatabase();
    }
 
    public void close() {
        dbHelper.close();
    }
 
    public Product createProduct(String barcode, String type) {
        ContentValues values = new ContentValues();
        values.put(MySQLiteHelper.COLOMN_BARCODE, barcode);
        values.put(MySQLiteHelper.COLOMN_TYPE, type);
 
        long insertId = database.insert(MySQLiteHelper.TABLE_NAME, null, values);
 
        Cursor cursor = database.query(MySQLiteHelper.TABLE_NAME, allColumns,  MySQLiteHelper.PRODUCT_ID +" = "
                + insertId, null, null, null, null);
        cursor.moveToFirst();
 
        return cursorToProduct(cursor);
    }
 
    public List<Product> getAllProducts() {
        List<Product> productsList = new ArrayList<Product>();
        productsList = new ArrayList<Product>();
 
        Cursor cursor = database.query( MySQLiteHelper.TABLE_NAME, allColumns, null, null, null,
                null, null);
        cursor.moveToFirst();
 
        if (cursor.getCount() == 0)
            return productsList;
 
        while (cursor.isAfterLast() == false) {
            Product product = cursorToProduct(cursor);
            productsList.add(product);
            cursor.moveToNext();
        }
 
        cursor.close();
 
        return productsList;
    }
 
    private Product cursorToProduct(Cursor cursor) {
        Product product = new Product();
        product.setId(cursor.getLong(0));
        product.setBarcode(cursor.getString(1));
        product.setType(cursor.getString(2));
 
        return product;
    }
}
```

Danke euch


----------



## dzim (26. Aug 2014)

Keine Ahnung. Vielleicht im Bug im Scanner.
Dein Quellcode ist schön und gut, aber weder kann ich hier noch auf deinen Screenshots irgendwo ein Problem sehen.


----------



## nata (26. Aug 2014)

In SQL Statements hatte ich Fehler


----------



## dzim (26. Aug 2014)

Welche? Idealerweise mit Stacktrace aus LogCat.

(Muss man echt für jede Information nachfragen?)


----------



## nata (26. Aug 2014)

Bei DROP_TABLE und CREATE _ TABLE  - abstand hat gefehlt.


----------

