Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.85 KB | None | 0 0
  1. private static final String TAG = MainActivity.class.getSimpleName();
  2.  
  3. private SqliteDatabase mDatabase;
  4.  
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9.  
  10. FrameLayout fLayout = (FrameLayout) findViewById(R.id.activity_to_do);
  11.  
  12. RecyclerView productView = (RecyclerView)findViewById(R.id.product_list);
  13. LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
  14. productView.setLayoutManager(linearLayoutManager);
  15. productView.setHasFixedSize(true);
  16.  
  17. mDatabase = new SqliteDatabase(this);
  18. List<Product> allProducts = mDatabase.listProducts();
  19.  
  20. if(allProducts.size() > 0){
  21. productView.setVisibility(View.VISIBLE);
  22. ProductAdapter mAdapter = new ProductAdapter(this, allProducts);
  23. productView.setAdapter(mAdapter);
  24.  
  25. }else {
  26. productView.setVisibility(View.GONE);
  27. Toast.makeText(this, "There is no product in the database. Start adding now", Toast.LENGTH_LONG).show();
  28. }
  29.  
  30. FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  31. fab.setOnClickListener(new View.OnClickListener() {
  32. @Override
  33. public void onClick(View view) {
  34.  
  35. addTaskDialog();
  36. }
  37. });
  38. }
  39.  
  40. private void addTaskDialog(){
  41. LayoutInflater inflater = LayoutInflater.from(this);
  42. View subView = inflater.inflate(R.layout.add_product_layout, null);
  43.  
  44. final EditText nameField = (EditText)subView.findViewById(R.id.enter_name);
  45. final EditText quantityField = (EditText)subView.findViewById(R.id.enter_quantity);
  46.  
  47.  
  48. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  49. builder.setTitle("Add new product");
  50. builder.setView(subView);
  51. builder.create();
  52.  
  53. builder.setPositiveButton("ADD PRODUCT", new DialogInterface.OnClickListener() {
  54. @Override
  55. public void onClick(DialogInterface dialog, int which) {
  56.  
  57. final String name = nameField.getText().toString();
  58. final String quantityStr = quantityField.getText().toString();
  59.  
  60. if(TextUtils.isEmpty(name) || TextUtils.isEmpty(quantityStr))
  61. {
  62. Toast.makeText(MainActivity.this, "Something went wrong. Check your input values", Toast.LENGTH_LONG).show();
  63. }
  64. else
  65. {
  66. final int quantity = Integer.parseInt(quantityStr);
  67.  
  68. if(quantity<=0)
  69. {
  70. Toast.makeText(MainActivity.this,"input is less than or equal to zero",Toast.LENGTH_LONG).show();
  71. }
  72. else {
  73.  
  74. Product newProduct = new Product(name, quantity);
  75. mDatabase.addProduct(newProduct);
  76.  
  77. //refresh the activity
  78. finish();
  79. startActivity(getIntent());
  80. }
  81. }
  82.  
  83. }
  84. });
  85.  
  86. builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
  87. @Override
  88. public void onClick(DialogInterface dialog, int which) {
  89. Toast.makeText(MainActivity.this, "Task cancelled", Toast.LENGTH_LONG).show();
  90. }
  91. });
  92. builder.show();
  93. }
  94.  
  95. @Override
  96. protected void onDestroy() {
  97. super.onDestroy();
  98. if(mDatabase != null){
  99. mDatabase.close();
  100. }
  101. }
  102. }
  103.  
  104.  
  105. //product.java(in connection to sqlite)
  106.  
  107. public class Product {
  108. private int id;
  109. private String name;
  110. private int quantity;
  111.  
  112. public Product(String name, int quantity) {
  113. this.name = name;
  114. this.quantity = quantity;
  115. }
  116.  
  117. public Product(int id, String name, int quantity) {
  118. this.id = id;
  119. this.name = name;
  120. this.quantity = quantity;
  121. }
  122.  
  123. public int getId() {
  124. return id;
  125. }
  126.  
  127. public void setId(int id) {
  128. this.id = id;
  129. }
  130.  
  131. public String getName() {
  132. return name;
  133. }
  134.  
  135. public void setName(String name) {
  136. this.name = name;
  137. }
  138.  
  139. public int getQuantity() {
  140. return quantity;
  141. }
  142.  
  143. public void setQuantity(int quantity) {
  144. this.quantity = quantity;
  145. }
  146. }
  147.  
  148.  
  149. //product Adapter
  150.  
  151. public class ProductAdapter extends RecyclerView.Adapter<ProductViewHolder>{
  152.  
  153. private Context context;
  154. private List<Product> listProducts;
  155.  
  156. private SqliteDatabase mDatabase;
  157.  
  158. public ProductAdapter(Context context, List<Product> listProducts) {
  159. this.context = context;
  160. this.listProducts = listProducts;
  161. mDatabase = new SqliteDatabase(context);
  162. }
  163.  
  164. @Override
  165. public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  166. View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_list_layout, parent, false);
  167. return new ProductViewHolder(view);
  168. }
  169.  
  170. @Override
  171. public void onBindViewHolder(ProductViewHolder holder, int position) {
  172. final Product singleProduct = listProducts.get(position);
  173. holder.name.setText(singleProduct.getName());
  174.  
  175. // holder.quantity.setText(singleProduct.getQuantity());
  176.  
  177. holder.editProduct.setOnClickListener(new View.OnClickListener() {
  178. @Override
  179. public void onClick(View view) {
  180. editTaskDialog(singleProduct);
  181. }
  182. });
  183.  
  184. holder.deleteProduct.setOnClickListener(new View.OnClickListener() {
  185. @Override
  186. public void onClick(View view) {
  187. mDatabase.deleteProduct(singleProduct.getId());
  188. ((Activity)context).finish();
  189. context.startActivity(((Activity) context).getIntent());
  190. }
  191. });
  192. }
  193.  
  194. @Override
  195. public int getItemCount() {
  196. return listProducts.size();
  197. }
  198.  
  199. private void editTaskDialog(final Product product){
  200. LayoutInflater inflater = LayoutInflater.from(context);
  201. View subView = inflater.inflate(R.layout.add_product_layout, null);
  202. final EditText nameField = (EditText)subView.findViewById(R.id.enter_name);
  203. final EditText quantityField = (EditText)subView.findViewById(R.id.enter_quantity);
  204.  
  205. if(product != null){
  206. nameField.setText(product.getName());
  207. quantityField.setText(String.valueOf(product.getQuantity()));
  208. }
  209.  
  210. AlertDialog.Builder builder = new AlertDialog.Builder(context);
  211. builder.setTitle("Edit product");
  212. builder.setView(subView);
  213. builder.create();
  214. builder.setPositiveButton("EDIT PRODUCT", new DialogInterface.OnClickListener() {
  215. @Override
  216. public void onClick(DialogInterface dialog, int which) {
  217.  
  218. final String name = nameField.getText().toString();
  219. final String quantityStr = quantityField.getText().toString();
  220.  
  221. if(TextUtils.isEmpty(name) || TextUtils.isEmpty(quantityStr))
  222. {
  223. Toast.makeText(context, "Something went wrong. Check your input values", Toast.LENGTH_LONG).show();
  224. }
  225. else
  226. {
  227. final int quantity = Integer.parseInt(quantityStr);
  228.  
  229. if(quantity<=0)
  230. {
  231. Toast.makeText(context,"input is less than or equal to zero",Toast.LENGTH_LONG).show();
  232. }
  233. else {
  234.  
  235. mDatabase.updateProduct(new Product(product.getId(), name, quantity));
  236. //refresh the activity
  237. ((Activity)context).finish();
  238. context.startActivity(((Activity)context).getIntent());
  239. }
  240. }
  241. }
  242. });
  243.  
  244. builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
  245. @Override
  246. public void onClick(DialogInterface dialog, int which) {
  247. Toast.makeText(context, "Task cancelled", Toast.LENGTH_LONG).show();
  248. }
  249. });
  250. builder.show();
  251. }
  252. }
  253.  
  254.  
  255. //product viewHolder
  256.  
  257. public class ProductViewHolder extends RecyclerView.ViewHolder {
  258. public TextView name;
  259. public TextView quantity;
  260. public ImageView deleteProduct;
  261. public ImageView editProduct;
  262.  
  263. public ProductViewHolder(View itemView) {
  264. super(itemView);
  265. name = (TextView)itemView.findViewById(R.id.product_name);
  266. deleteProduct = (ImageView)itemView.findViewById(R.id.delete_product);
  267. editProduct = (ImageView)itemView.findViewById(R.id.edit_product);
  268. quantity=(TextView)itemView.findViewById(R.id.product_name2);
  269. }
  270. }
  271.  
  272.  
  273. Now the issue here is when is use holder.quantity.setText(singleProduct.getQuantity()); the application crashes
  274.  
  275. I also wanted to add date picker dialog and want to display it in the recycler view so for date the code is same like holder.object.setText or something else and can we use a form for alert dialog like
  276. final Dialog dialog = new Dialog(this);
  277. dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // before
  278. dialog.setContentView(R.layout.dialog_dark);
  279. dialog.setCancelable(true);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement