Thanks to contributors from Stackoverflow, I was able to develop my first Android app using Android Studio without any prior knowledge of Java. It's an unique-interface barcode scanning app (using ZXing) with two EditText fields and an OK button. The two fields are filled with the barcode scanner.
I want to store the data in the two EditText fields on a local database with two columns (for the two fields) and one table.
I am not asking for code, I'm just asking for guidelines and pieces of advice to push forward the development of my app.
Thank you
I think you should use Sqlite Database and this is the way you should use it.
DBHelper.java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "MyDBName.db";
public static final String CONTACTS_TABLE_NAME = "barcode";
public static final String CONTACTS_COLUMN_ID = "id";
public static final String CONTACTS_COLUMN_TEXT1 = "field1";
public static final String CONTACTS_COLUMN_TEXT2 = "field2";
private HashMap hp;
public DBHelper(Context context) {
super(context, DATABASE_NAME , null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(
"create table barcode" +
"(id integer primary key, field1 text, field2 text)"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS barcode");
onCreate(db);
}
public boolean insertData (String text1, String text2) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("field1", text1);
contentValues.put("field2", text2);
db.insert("barcode", null, contentValues);
return true;
}
public Cursor getData(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from barcode where id="+id+"", null );
return res;
}
public boolean updateBarcode (Integer id, String text1, String text2) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("field1", text1);
contentValues.put("field2", text2);
db.update("barcode", contentValues, "id = ? ", new String[] { Integer.toString(id) } );
return true;
}
Your Activity or Fragment
private DBHelper mydb ;
mydb = new DBHelper(this);
//insert
if(mydb.insertBarcode(EditText1.getText().toString(),
EditText2.getText().toString())){
Toast.makeText(getApplicationContext(), "done",
Toast.LENGTH_SHORT).show();
} else{
Toast.makeText(getApplicationContext(), "not done",
Toast.LENGTH_SHORT).show();
}
//Read
Cursor getBarcode= mydb.getData(id_to_search);
You can also add more function like readfull data from table etc in DBHelper.java