Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.81 KB | None | 0 0
  1. btnSaveChanges.setOnClickListener(new View.OnClickListener() {
  2. @Override
  3. public void onClick(View v) {
  4. try {
  5. costPrice = Double.parseDouble(etItemCostPrice.getText().toString());
  6. sellingPrice = Double.parseDouble((etItemSellingPrice.getText().toString()));
  7. quantitySold = Integer.parseInt(spQuantity.getSelectedItem().toString());
  8. itemName = etItemName.getText().toString();
  9. datesold = new Date();
  10. datesold2 = etDateOfSale.getText().toString();
  11. //staffRate = true;
  12. if (rbtnStaffRateNo.isChecked())
  13. {
  14. rbtnvalue = "No";
  15. }
  16. if (rbtnStaffRateYes.isChecked())
  17. {
  18. rbtnvalue = "Yes";
  19. }
  20.  
  21. s = new Sale(itemName, costPrice, sellingPrice, quantitySold, staffRate, datesold);
  22. displayString(s.toString());
  23.  
  24. //DATABASE
  25. mc.addToDatabase(itemName, costPrice, sellingPrice, quantitySold, datesold2, rbtnvalue, getApplicationContext());
  26.  
  27.  
  28. } catch (NumberFormatException e) {
  29. costPrice = 0;
  30. sellingPrice = 0;
  31. quantitySold = 0;
  32.  
  33. }
  34. }
  35. });
  36.  
  37. public long addToDatabase(String entryName, double entryTel, double sellingprice, int quantitysold, String datesold, String staffdiscount, Context c)
  38. {
  39. MyDbAdapter db = new MyDbAdapter(c);
  40. db.open();
  41.  
  42. long rowIDofInsertedEntry = db.insertEntry(entryName, entryTel, sellingprice, quantitysold, datesold, staffdiscount);
  43. db.close();
  44. return rowIDofInsertedEntry;
  45. }
  46.  
  47. private static final String DATABASE_NAMEA = "test.db";
  48. private static final String DATABASE_TABLE = "myTestDb";
  49.  
  50. private static final int DATABASE_VERSION = 1;
  51. private SQLiteDatabase _db;
  52. private final Context context;
  53.  
  54. public static final String KEY_ID = "_id";
  55. public static final int COLUMN_KEY_ID = 0;
  56. public static final String ENTRY_NAME = "entry_name";
  57. public static final int COLUMN_NAME_ID = 1;
  58. public static final String ENTRY_TEL = "entry_telephone";
  59. public static final int COLUMN_TEL_ID = 2;
  60. public static final String ENTRY_SELLINGPRICE = "entry_sellingprice";
  61. public static final int COLUMN_SELLINGPRICE_ID = 3;
  62. public static final String ENTRY_QTYSOLD = "entry_qtysold";
  63. public static final int COLUMN_QTYSOLD_ID = 4;
  64. public static final String ENTRY_DATESOLD = "entry_datesold";
  65. public static final int COLUMN_DATESOLD_ID = 5;
  66. public static final String ENTRY_STAFFDISCOUNT = "entry_staffdiscount";
  67. public static final int COLUMN_STAFFDISCOUNT_ID = 6;
  68. private String MYDBADAPTER_LOG_CAT = "MY_LOG";
  69.  
  70. public long insertEntry(String entryName, double entryTel, double sellingprice, int quantitysold, String datesold, String staffdiscount)
  71. {
  72. try {
  73. //step 19 - insert record into table
  74. ContentValues newEntryValues = new ContentValues();
  75. newEntryValues.put(ENTRY_NAME, entryName);
  76. newEntryValues.put(ENTRY_TEL, entryTel);
  77. newEntryValues.put(ENTRY_SELLINGPRICE, sellingprice);
  78. newEntryValues.put(ENTRY_QTYSOLD, quantitysold);
  79. newEntryValues.put(ENTRY_DATESOLD, datesold);
  80. newEntryValues.put(ENTRY_STAFFDISCOUNT, staffdiscount);
  81.  
  82. Log.w(MYDBADAPTER_LOG_CAT, "Inserted EntryName = " + entryName +
  83. "EntryTel = " + entryTel + "EntrySP = " + sellingprice + "EntryQt = " + quantitysold + "Entryds = " + datesold +
  84. "Entrysd = " + staffdiscount + DATABASE_TABLE);
  85.  
  86.  
  87. check = _db.insert(DATABASE_TABLE, null, newEntryValues);
  88. }
  89. catch (Exception ex)
  90. {
  91. Log.e("ERROR", ex.toString());
  92. }
  93.  
  94. return check;
  95.  
  96. }
  97.  
  98. public Cursor retrieveAllEntriesCursor() {
  99. //step 21 - retrieve all records from table
  100. Cursor c = null;
  101. try {
  102. c = _db.query(DATABASE_TABLE, new String[] {KEY_ID, ENTRY_NAME, ENTRY_TEL, ENTRY_SELLINGPRICE, ENTRY_QTYSOLD, ENTRY_DATESOLD, ENTRY_STAFFDISCOUNT}, null, null, null, null, null);
  103.  
  104. }
  105. catch (SQLiteException e)
  106. {
  107. Log.w(MYDBADAPTER_LOG_CAT, "Retrieve fail!");
  108. }
  109.  
  110.  
  111.  
  112. return c;
  113.  
  114. }
  115.  
  116. public ArrayList<SalesItemInformationLV> retrieveAllForlist2(Context c)
  117. {
  118. ArrayList<SalesItemInformationLV> items = new ArrayList<SalesItemInformationLV>();
  119.  
  120. Cursor myCursor;
  121. String mystring = "";
  122.  
  123. MyDbAdapter db = new MyDbAdapter(c);
  124. db.open();
  125. //contactIdList.clear();
  126. //contactList.clear();
  127. myCursor = db.retrieveAllEntriesCursor();
  128.  
  129. if (myCursor != null && myCursor.getCount() > 0)
  130. {
  131. myCursor.moveToFirst();
  132. do {
  133. //contactIdList.add(myCursor.getInt(db.COLUMN_KEY_ID)); contactIdList is only for delete purposes
  134. items.add(new SalesItemInformationLV(myCursor.getString(db.COLUMN_NAME_ID), myCursor.getDouble(db.COLUMN_TEL_ID), myCursor.getDouble(db.COLUMN_SELLINGPRICE_ID), myCursor.getInt(db.COLUMN_QTYSOLD_ID), myCursor.getString(db.COLUMN_DATESOLD_ID), myCursor.getString(db.COLUMN_STAFFDISCOUNT_ID)));
  135. } while (myCursor.moveToNext());
  136. }
  137. db.close();
  138.  
  139. return items;
  140. }
  141.  
  142. MyItems mi;
  143. //For Items display - ArrayList
  144. private ArrayList<SalesItemInformationLV> displayiteminfo;
  145.  
  146. @Override
  147. protected void onCreate(Bundle savedInstanceState) {
  148. super.onCreate(savedInstanceState);
  149. setContentView(R.layout.activity_list_sale_item);
  150.  
  151. mi = MyItems.getInstance();
  152. displayiteminfo = mi.retrieveAllForlist(getApplicationContext());
  153.  
  154. //New array adapter for customised ArrayAdapter
  155. final ArrayAdapter<SalesItemInformationLV> adapter = new itemArrayAdapter(this, 0, displayiteminfo);
  156.  
  157. //Find the list view, bind it with custom adapter
  158. final ListView listView = (ListView)findViewById(R.id.customListview);
  159. listView.setAdapter(adapter);
  160.  
  161. //Selecting the listview item!
  162. listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  163. @Override
  164. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  165.  
  166. SalesItemInformationLV saleitem = displayiteminfo2.get(position);
  167.  
  168. String namevalue = saleitem.getItemname();
  169. Double costpvalue = saleitem.getCostprice();
  170. Double sellpvalue = saleitem.getSellingprice();
  171. int qtyvalue = saleitem.getItemquantity();
  172. String datevalue = saleitem.getDatesold();
  173. String staffvalue = saleitem.getStaffdiscount();
  174.  
  175. Intent myintent = new Intent(ListSaleItemActivity.this, ViewSaleDetails.class);
  176. myintent.putExtra("itemname", namevalue);
  177. myintent.putExtra("itemcp", costpvalue);
  178. myintent.putExtra("itemsp", sellpvalue);
  179. myintent.putExtra("itemqty", qtyvalue);
  180. myintent.putExtra("itemds", datevalue);
  181. myintent.putExtra("itemsstaffdis", staffvalue);
  182.  
  183. startActivity(myintent);
  184.  
  185. }
  186. });
  187.  
  188. protected void onCreate(Bundle savedInstanceState) {
  189. super.onCreate(savedInstanceState);
  190. setContentView(R.layout.activity_view_sale_details);
  191.  
  192. //Change action bar name to AddSales
  193. setTitle("SaleDetailActivity");
  194.  
  195. nameTV = (TextView)findViewById(R.id.viewname);
  196. costTV = (TextView)findViewById(R.id.viewcostprice);
  197. sellTV = (TextView)findViewById(R.id.viewsellingprice);
  198. qtyTV = (TextView)findViewById(R.id.viewqtysold);
  199. datesoldTV = (TextView)findViewById(R.id.viewdatesold);
  200. staffdisTV = (TextView)findViewById(R.id.viewstaffdiscount);
  201.  
  202.  
  203. androidIm =(ImageView) findViewById(R.id.androidimageview);
  204. androidIm.setImageResource(R.drawable.android);
  205.  
  206. //retrieve data
  207. Bundle extras = this.getIntent().getExtras();
  208. String itemName = extras.getString("itemname");
  209. float itemcost = extras.getFloat("itemcp", 0f);
  210. float itemselling = extras.getFloat("itemsp", 0f);
  211. int itemquantity = extras.getInt("itemqty", 0);
  212. String itemdatesold = extras.getString("itemds");
  213. String itemstaffdiscount= extras.getString("itemsstaffdis");
  214.  
  215. //display data
  216. nameTV.setText(itemName);
  217. costTV.setText(String.valueOf(itemcost)); //take note of float
  218. sellTV.setText(String.valueOf(itemselling));
  219. qtyTV.setText(String.valueOf(itemquantity));
  220. datesoldTV.setText(itemdatesold);
  221. staffdisTV.setText(itemstaffdiscount);
  222.  
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement