Hi currently I’m doing project on shoe sticker and I have created database for my stickers? Can someone guide me how to start the coding on the two identifier for the 2 stickers? What my codes is showing now is workable codes but it’s hard coded, what should I add to I link them to the database I’m currently using android sqlite. I want the mobile device able to display relevant information of product when sticker is picked up. Currently, I have 2 stickers, so I want the 2 different sticker when picked up will display the information of shoe. For example sticker A when picked up display brand nike then when sticker B is picked up it will display brand Addidas. I following the codes given on github just that I made some changes. The database file I did it on my own.
this is my database file:
public class Database_nearable extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = Environment.getExternalStorageDirectory().toString() + "/result.db";
private static final String TABLE_NEARABLE = "nearable";
private static final String KEY_ID = "id";
private static final String KEY_STICKERID = "stickerID";
private static final String KEY_DESC = "desc";
private static final String KEY_SIZE = "size";
private static final String KEY_COLOUR = "colour";
private static final String KEY_COUNTRY = "country";
private static final String KEY_MODEL = "model";
private static final String KEY_PRICE = "price";
public Database_nearable(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
//creating Tables
@Override
public void onCreate(SQLiteDatabase db){
String CREATE_NEARABLES_TABLE = "CREATE TABLE " + TABLE_NEARABLE + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_STICKERID + " TEXT," + KEY_DESC +
" TEXT,"+ KEY_SIZE + " TEXT," + KEY_COLOUR + " TEXT," + KEY_COUNTRY + " TEXT," + KEY_MODEL + " TEXT," + KEY_PRICE + " REAL" + ")";
db.execSQL(CREATE_NEARABLES_TABLE);
}
//upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
//drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NEARABLE);
//create tables again
onCreate(db);
}
public void addNearable(Nearable nearable){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_STICKERID, nearable.getStickerID()); //get sticker id
values.put(KEY_DESC, nearable.getDesc()); //get description
values.put(KEY_SIZE, nearable.getSize()); //get size available
values.put(KEY_COLOUR, nearable.getColour()); //get colours available
values.put(KEY_COUNTRY, nearable.getCountry()); //get country of origin
values.put(KEY_MODEL, nearable.getModel()); //get shoe model
values.put(KEY_PRICE, nearable.getPrice()); //get shoe price
db.insert(TABLE_NEARABLE, null, values);
db.close();
}
public int updateNearable(Nearable nearable) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_STICKERID, nearable.getStickerID()); //get sticker id
values.put(KEY_DESC, nearable.getDesc()); //get description
values.put(KEY_SIZE, nearable.getSize()); //get size available
values.put(KEY_COLOUR, nearable.getColour()); //get colours available
values.put(KEY_COUNTRY, nearable.getCountry()); //get country of origin
values.put(KEY_MODEL, nearable.getModel()); //get shoe model
values.put(KEY_PRICE, nearable.getPrice()); //get shoe price
//updating row
return db.update(TABLE_NEARABLE, values, KEY_ID + " = ?", new String[]{String.valueOf(nearable.getId())});
}
public Nearable findNearable(String model){
String selectQuery = "SELECT * FROM " + TABLE_NEARABLE + "WHERE" + KEY_MODEL + "= \"" + model + "\"";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
//looping through all rows and adding to list
Nearable nearable = new Nearable();
if (cursor.moveToFirst()) {
cursor.moveToFirst();
nearable.setId(Integer.parseInt(cursor.getString(0)));
nearable.setStickerID(cursor.getString(1));
nearable.setDesc(cursor.getString(2));
nearable.setSize(cursor.getString(3));
nearable.setColour(cursor.getString(4));
nearable.setCountry(cursor.getString(5));
nearable.setModel(cursor.getString(6));
nearable.setPrice(Float.parseFloat(cursor.getString(7)));
cursor.close();
} else {
nearable = null;
}
db.close();
return nearable;
}
}
NearablesDemoActivity.java:
public class NearablesDemoActivity extends BaseActivity {
private static final String TAG = NearablesDemoActivity.class.getSimpleName();
private Nearable currentNearable;
private BeaconManager beaconManager;
private String scanId;
private Database_nearable db;
TextView Desc, COO, SM, Price; //description, for Country of origin, for shoe model, for price
Spinner spinnerDropDown; //for size available
String[] size = {
"Click to see",
"5.5'",
"6'",
"7'",
"7.5'",
"8.5'"
}; //for size available
Spinner spinnerDropDown2; //for colours available
String[] colour = {
"Click to see",
"Gold",
"Silver",
"Copper"
}; //for colours available
ImageView image1, image2, image3; //For image 1, 2, 3
Button btnRating; //for button
ArrayList<String> ArrayofNearable = new ArrayList<String>();
List<Nearable> nearables;
static ArrayAdapter<String> dbAdapter;
@Override
protected int getLayoutResId() {
return R.layout.nearable_demo;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nearable_demo);
Desc =(TextView) findViewById(R.id.textview1); //for description
// Get reference of SpinnerView from layout/nearable_demo.xml
spinnerDropDown =(Spinner)findViewById(R.id.spinner1); //For size available
spinnerDropDown2 =(Spinner)findViewById(R.id.spinner2);//for colours available
image1 = (ImageView) findViewById(R.id.shoe1); //display image of shoe
image2 = (ImageView) findViewById(R.id.shoe1a); //display image of onsale
image3 = (ImageView) findViewById(R.id.shoe1b); //display image of buy1get1frees
COO =(TextView) findViewById(R.id.textview2);//for country of origin
SM =(TextView) findViewById(R.id.textview3); //for shoe model
Price =(TextView) findViewById(R.id.textview4); //for price
btnRating = (Button) findViewById(R.id.rd); //for ratingbar
db = new Database_nearable(this);
dbAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, ArrayofNearable);
currentNearable = getIntent().getExtras().getParcelable(ListNearablesActivity.EXTRAS_NEARABLE);
displayCurrentNearableInfo();
beaconManager = new BeaconManager(this);
ArrayAdapter<String> adapter= new ArrayAdapter<>(this,android.R.layout.simple_spinner_dropdown_item ,size); //for size available
spinnerDropDown.setAdapter(adapter);
ArrayAdapter<String> adapter1= new ArrayAdapter<>(this,android.R.layout.simple_spinner_dropdown_item ,colour); //for colours available
spinnerDropDown2.setAdapter(adapter1);
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setNavigationIcon(R.drawable.ic_action_navigation_arrow_back);
toolbar.setTitle(getTitle());
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
btnRating=(Button)findViewById(R.id.rd);
btnRating.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent pageforRatingBar = new Intent(getApplicationContext(), RatingBar_main.class);
startActivity(pageforRatingBar);
}
});
}
@Override
protected void onResume() {
super.onResume();
beaconManager.setNearableListener(new BeaconManager.NearableListener() {
@Override
public void onNearablesDiscovered(List<Nearable> nearables) {
updateCurrentNearable(nearables);
displayCurrentNearableInfo();
}
});
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
@Override
public void onServiceReady() {
scanId = beaconManager.startNearableDiscovery();
}
});
}
@Override
protected void onStop() {
beaconManager.disconnect();
super.onStop();
}
private void displayCurrentNearableInfo() {
Desc.setText("The perfect on-point court heels, these classic pumps by KIM&BEN " +
"flaunt a luxe suedette finish with a sleek silhouette. Chic and sophisticated, " +
"these lovelies brim with an air of sheer elegance.");
COO.setText("USA");
SM.setText("ABXYZ123");
Price.setText("$99.99");
}
private void updateCurrentNearable(List<Nearable> nearables) {
for (Nearable nearable : nearables) {
if (nearable.equals(currentNearable)) {
currentNearable = nearable;
}
}
}
}