i've created a DBhelper, class for my products and a product repo. Currently my application is using a listview but all the data is being stored from the app.
public class Product implements Serializable {
// Labels table name
public static final String TABLE = "Product";
// Labels Table Columns names
public static final String KEY_ID = "id";
public static final String KEY_name_1 = "name_1";
public static final String KEY_name_2 = "name_2";
public static final String KEY_additional_info = "additional_info";
public static final String KEY_prod_image = "prod_image";
public static final String KEY_location = "location";
// property help us to keep data
public int product_ID;
public String name_1;
public String name_2;
public String location;
public String additional_info;
public byte[] prod_image;
}
How would I store some data in the application so when I open the app its there already?
databasehelper
public class DBHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 4;
// Database Name
private static final String DATABASE_NAME = "products.db";
public DBHelper(Context context ) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
//All necessary tables you like to create will create here
String CREATE_TABLE_PRODUCT = "CREATE TABLE " + Product.TABLE + "("
+ Product.KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT ,"
+ Product.KEY_name_1 + " TEXT, "
+ Product.KEY_name_2 + " INTEGER, "
+ Product.KEY_additional_info + " INTEGER, "
+ Product.KEY_prod_image + " BLOB, "
+ Product.KEY_location + " INTEGER) ";
db.execSQL(CREATE_TABLE_PRODUCT);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed, all data will be gone!!!
db.execSQL("DROP TABLE IF EXISTS " + Product.TABLE);
// Create tables again
onCreate(db);
}
}
would this be done in the main activity like this:
String[][] recordsArray = {
{"1", "steve", "jobs", "creator of apple", steve.jpg}};
Mainactivity.java
public class MainActivity extends Activity {
ListView list;
ImageButton btnAddNewRecord;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAddNewRecord = (ImageButton) findViewById(R.id.btnAddRecord);
btnAddNewRecord.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, NewProductActivity.class);
startActivity(myIntent);
finish();
}
});
ProductRepo prodRepo= new ProductRepo(this);
final List<Product> productList = prodRepo.getProductList();
ProductAdapter adapter=new ProductAdapter(this, productList);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Bundle bundle = new Bundle();
Intent intent = new Intent(MainActivity.this, ProductDetailActivity.class);
Product product = productList.get(position);
bundle.putSerializable("PRODUCT_KEY", product);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
Aucun commentaire:
Enregistrer un commentaire