[/QUOTE][QUOTE]
Hallo Leute,
Nach Stundenlange ausprobieren, möchte ich euch um die Hilfe bieten.
ich versuche android programmieren zu lernen und habe ich vorgenommen ein kleines Projekt zu machen. Ich mache BarcodeScannerApp.
Layout wie es aussehen soll steht im Anhang
ich mache diese Tutorium und habe ich mehrere Fragen
1.Habe ich in MainActivity die Methode onCreate richtig implementiert?
2.Muss ich in der MainActivity in der Methode onClick von der Klasse FeedReaderDbHelper die Methode insertData aufrufen?
- in meinem Fall brauche ich doch
public static final String COLUMN_NAME_ENTRY_ID = "entryid";
nicht?
- in der Methode insertData ist rückgabewert
long
und warum wird kein Fehler gezeigt wo in der Methode keinreturn
steht?
- in der klasse FeedReaderContract diese Zeile
FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(getContext());
gibt mir Fehler dass getContext() nicht definiert ist
- habe ich insertData richtig umgeschrieben?
- ich habe keine Ahnung wie ich returnData() an mein Code anpassen soll. Könnt ihr mir helfen?
public static final String EXTRA_BARCODE = "extraBarcode";
public static final String EXTRA_FORMAT = "extraFormat";
public final static String EXTRA_MESSAGE = "com.example.pricescannerapp.MESSAGE";
private IntentIntegrator mIntentIntegrator;
private FeedReaderDbHelper mDbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.mIntentIntegrator = new IntentIntegrator(this);
mDbHelper = new FeedReaderDbHelper(this);
}
public void onClick(View view) {
this.mIntentIntegrator.initiateScan();
}
public void showProducts(View view) {
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(
requestCode, resultCode, intent);
if (scanResult != null) {
String barcode;
String type;
barcode = scanResult.getContents();
type = scanResult.getFormatName();
EditText edit_barcode = (EditText) findViewById(R.id.etBarcode);
edit_barcode.setText(barcode);
EditText edit_type = (EditText) findViewById(R.id.etType);
edit_type.setText(type);
}
}
}
public class FeedReaderDbHelper extends SQLiteOpenHelper {
// If you change the database schema, you must increment the database
// version.
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "product.db";
private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ",";
private static final String SQL_CREATE_ENTRIES = "CREATE TABLE "
+ FeedEntry.TABLE_NAME + " (" + FeedEntry._ID
+ " INTEGER PRIMARY KEY," + FeedEntry.COLUMN_NAME_BARCODE
+ TEXT_TYPE + COMMA_SEP + FeedEntry.COLUMN_NAME_TYPE + TEXT_TYPE +
" )";
private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS "
+ FeedEntry.TABLE_NAME;
public FeedReaderDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// This database is only a cache for online data, so its upgrade policy
// is
// to simply to discard the data and start over
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
}```
```public final class FeedReaderContract {
// To prevent someone from accidentally instantiating the contract class,
// give it an empty constructor.
public FeedReaderContract() {
}
FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(getContext());
public long insertData(String barcode, String type) {
// Gets the data repository in write mode
SQLiteDatabase db = mDbHelper.getWritableDatabase();
// Create a new map of values, where column names are the keys
ContentValues values = new ContentValues();
values.put(FeedEntry.COLUMN_NAME_BARCODE, barcode);
values.put(FeedEntry.COLUMN_NAME_TYPE, type);
// Insert the new row, returning the primary key value of the new row
long newRowId;
newRowId = db.insert(FeedEntry.TABLE_NAME, null, values);
}
public Cursor returnData() {
SQLiteDatabase db = mDbHelper.getReadableDatabase();
// Define a projection that specifies which columns from the database
// you will actually use after this query.
String[] projection = { FeedEntry._ID, FeedEntry.COLUMN_NAME_TITLE,
FeedEntry.COLUMN_NAME_UPDATED,
};
// How you want the results sorted in the resulting Cursor
String sortOrder = FeedEntry.COLUMN_NAME_UPDATED + " DESC";
Cursor c = db.query(FeedEntry.TABLE_NAME, // The table to query
projection, // The columns to return
selection, // The columns for the WHERE clause
selectionArgs, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
sortOrder // The sort order
);
cursor.moveToFirst();
long itemId = cursor.getLong(cursor
.getColumnIndexOrThrow(FeedEntry._ID));
}
/* Inner class that defines the table contents */
public static abstract class FeedEntry implements BaseColumns {
public static final String TABLE_NAME = "products";
public static final String COLUMN_NAME_BARCODE = "barcode";
public static final String COLUMN_NAME_TYPE = "type";
}
}
Auf eure Antwort wurde ich mich sehr freuen
Lg Anno