Advertisement
mmayoub

School, Shoping list, 14.10.2017

Oct 14th, 2017
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.63 KB | None | 0 0
  1. Item.java
  2. ---------
  3. package com.example.mohamadpc.listviewshopping;
  4.  
  5. /**
  6.  * Created by MOHAMADPC on 10/10/2017.
  7.  */
  8.  
  9. public class Item {
  10.     private String name;
  11.     private int quentity;
  12.  
  13.  
  14.     public Item(String name, int quentity) {
  15.         this.name = name;
  16.         this.quentity = quentity;
  17.     }
  18.  
  19.     public Item(String name) {
  20.         this.name = name;
  21.         this.quentity = 1;
  22.     }
  23.  
  24.     public int getQuentity() {
  25.         return quentity;
  26.     }
  27.  
  28.     public String getName() {
  29.         return name;
  30.     }
  31.  
  32.     public void setName(String name) {
  33.         this.name = name;
  34.     }
  35.  
  36.     public void setQuentity(int quentity) {
  37.         this.quentity = quentity;
  38.     }
  39.  
  40. }
  41.  
  42.  
  43. ItemAdapter.java
  44. ----------------
  45. package com.example.mohamadpc.listviewshopping;
  46.  
  47.  
  48. import android.content.Context;
  49. import android.view.LayoutInflater;
  50. import android.view.View;
  51. import android.view.ViewGroup;
  52. import android.widget.BaseAdapter;
  53. import android.widget.TextView;
  54.  
  55. import java.util.List;
  56.  
  57. import static android.icu.lang.UCharacter.GraphemeClusterBreak.V;
  58.  
  59. /**
  60.  * Created by MOHAMADPC on 10/10/2017.
  61.  */
  62.  
  63. public class ItemAdapter extends BaseAdapter {
  64.     private Context context;
  65.     private List<Item> itemsList;
  66.  
  67.     public ItemAdapter(List<Item> itemsList, Context context) {
  68.         this.itemsList = itemsList;
  69.         this.context = context;
  70.     }
  71.  
  72.     @Override
  73.     public int getCount() {
  74.         return this.itemsList.size();
  75.     }
  76.  
  77.     @Override
  78.     public Object getItem(int position) {
  79.         return itemsList.get(position);
  80.     }
  81.  
  82.     @Override
  83.     public long getItemId(int position) {
  84.         return position;
  85.     }
  86.  
  87.     @Override
  88.     public View getView(int position, View convertView, ViewGroup parent) {
  89.         View myView = LayoutInflater.from(context).inflate(R.layout.list_row_layout, null);
  90.         Item item = itemsList.get(position);
  91.         ((TextView) myView.findViewById(R.id.tvItemName)).setText(item.getName());
  92.         ((TextView) myView.findViewById(R.id.tvQuentity)).setText(item.getQuentity() + "");
  93.         return myView;
  94.     }
  95. }
  96.  
  97. MainActivity.java
  98. -----------------
  99. package com.example.mohamadpc.listviewshopping;
  100.  
  101. import android.app.AlertDialog;
  102. import android.content.ClipData;
  103. import android.content.Context;
  104. import android.content.DialogInterface;
  105. import android.content.SharedPreferences;
  106. import android.support.v7.app.AppCompatActivity;
  107. import android.os.Bundle;
  108. import android.view.LayoutInflater;
  109. import android.view.View;
  110. import android.widget.AdapterView;
  111. import android.widget.Button;
  112. import android.widget.EditText;
  113. import android.widget.ListView;
  114. import android.widget.Toast;
  115.  
  116. import java.util.ArrayList;
  117. import java.util.List;
  118. import java.util.Set;
  119.  
  120. import static android.os.Build.VERSION_CODES.M;
  121.  
  122. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  123.     private EditText etNewItem;
  124.     private Button btnAddItem;
  125.     private ListView lsvItems;
  126.     List<Item> lst;
  127.     Context context;
  128.  
  129.     @Override
  130.     protected void onCreate(Bundle savedInstanceState) {
  131.         super.onCreate(savedInstanceState);
  132.         setContentView(R.layout.activity_main);
  133.  
  134.         connectToLayout();
  135.     }
  136.  
  137.     private void connectToLayout() {
  138.         this.context = this;
  139.         etNewItem = (EditText) findViewById(R.id.etNewItem);
  140.         btnAddItem = (Button) findViewById(R.id.btnAddItem);
  141.         lsvItems = (ListView) findViewById(R.id.lsvItems);
  142.         btnAddItem.setOnClickListener(this);
  143.  
  144.         lst = loadFromFile();
  145.         ItemAdapter myAdapter = new ItemAdapter(lst, this);
  146.         lsvItems.setAdapter(myAdapter);
  147.  
  148.         lsvItems.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
  149.             @Override
  150.             public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
  151.                 final AlertDialog.Builder myDialog = new AlertDialog.Builder(context);
  152.                 final View myView = LayoutInflater.from(context).inflate(R.layout.update_dialog, null);
  153.                 myDialog.setView(myView);
  154.                 myDialog.setTitle("Update Quentity");
  155.                 myDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  156.                     @Override
  157.                     public void onClick(DialogInterface dialog, int which) {
  158.                         String inputData = ((EditText) myView.findViewById(R.id.etNewQuentity)).getText().toString();
  159.                         if (!inputData.isEmpty()) {
  160.                             int newQ = Integer.parseInt(inputData);
  161.                             String itemName = lst.get(position).getName();
  162.  
  163.                             // save to file
  164.                             SharedPreferences myfile = context.getSharedPreferences("MyShoppingList", Context.MODE_PRIVATE);
  165.                             if (newQ > 0) {
  166.                                 myfile.edit().putInt(itemName, newQ).commit();
  167.                             } else {
  168.                                 myfile.edit().remove(itemName).commit();
  169.                             }
  170.  
  171.                             // update list
  172.                             lst = loadFromFile();
  173.                             lsvItems.setAdapter(new ItemAdapter(lst, context));
  174.                             dialog.cancel();
  175.                         }
  176.                     }
  177.                 });
  178.  
  179.                 myDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  180.                     @Override
  181.                     public void onClick(DialogInterface dialog, int which) {
  182.                         dialog.dismiss();
  183.                     }
  184.                 });
  185.                 myDialog.show();
  186.                 return true;
  187.             }
  188.         });
  189.     }
  190.  
  191.     private List<Item> loadFromFile() {
  192.         SharedPreferences myfile = this.getSharedPreferences("MyShoppingList", Context.MODE_PRIVATE);
  193.         Object[] keys = myfile.getAll().keySet().toArray();
  194.  
  195.         List<Item> lst = new ArrayList<>();
  196.         for (int i = 0; i < keys.length; i += 1) {
  197.             String name = (String) keys[i];
  198.             lst.add(new Item(name, myfile.getInt(name, 0)));
  199.         }
  200.         return lst;
  201.     }
  202.  
  203.     @Override
  204.     public void onClick(View v) {
  205.         switch (v.getId()) {
  206.             case R.id.btnAddItem:
  207.                 String newItemName = etNewItem.getText().toString();
  208.                 if (!newItemName.isEmpty()) {
  209.                     // add the item
  210.                     SharedPreferences myfile = this.getSharedPreferences("MyShoppingList", Context.MODE_PRIVATE);
  211.  
  212.                     int oldQ = myfile.getInt(newItemName, -1);
  213.                     if (oldQ == -1) {
  214.                         myfile.edit().putInt(newItemName, 1).commit();
  215.                     } else {
  216.                         myfile.edit().putInt(newItemName, oldQ + 1).commit();
  217.                     }
  218.  
  219.                     // update the list
  220.                     lst = loadFromFile();
  221.                     lsvItems.setAdapter(new ItemAdapter(lst, this));
  222.  
  223.                     etNewItem.setText("");
  224.                 }
  225.                 break;
  226.             default:
  227.                 Toast.makeText(this, "Error handling a click event", Toast.LENGTH_LONG).show();
  228.                 break;
  229.         }
  230.     }
  231. }
  232.  
  233. activity_main.xml
  234. -----------------
  235. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  236.     android:layout_width="match_parent"
  237.     android:layout_height="match_parent"
  238.     android:orientation="vertical">
  239.  
  240.     <LinearLayout
  241.         android:layout_width="match_parent"
  242.         android:layout_height="wrap_content"
  243.         android:orientation="horizontal">
  244.  
  245.         <EditText
  246.             android:id="@+id/etNewItem"
  247.             android:layout_width="match_parent"
  248.             android:layout_height="wrap_content"
  249.             android:layout_weight="1"
  250.             android:textSize="20sp" />
  251.  
  252.         <Button
  253.             android:id="@+id/btnAddItem"
  254.             android:layout_width="match_parent"
  255.             android:layout_height="wrap_content"
  256.             android:layout_weight="2"
  257.             android:text="Add"
  258.             android:textSize="20sp" />
  259.     </LinearLayout>
  260.  
  261.     <ListView
  262.         android:id="@+id/lsvItems"
  263.         android:layout_width="match_parent"
  264.         android:layout_height="wrap_content"
  265.         android:choiceMode="singleChoice"></ListView>
  266. </LinearLayout>
  267.  
  268. list_row_layout.xml
  269. -------------------
  270. <?xml version="1.0" encoding="utf-8"?>
  271. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  272.     android:layout_width="match_parent"
  273.     android:layout_height="wrap_content"
  274.     android:orientation="horizontal">
  275.  
  276.     <TextView
  277.         android:id="@+id/tvItemName"
  278.         android:layout_width="match_parent"
  279.         android:layout_height="wrap_content"
  280.         android:layout_weight="1"
  281.         android:textSize="18sp" />
  282.  
  283.     <TextView
  284.         android:id="@+id/tvQuentity"
  285.         android:layout_width="match_parent"
  286.         android:layout_height="wrap_content"
  287.         android:layout_weight="1"
  288.         android:textSize="18sp" />
  289.  
  290. </LinearLayout>
  291.  
  292. update_dialog.xml
  293. -----------------
  294. <?xml version="1.0" encoding="utf-8"?>
  295. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  296.     android:layout_width="match_parent"
  297.     android:layout_height="match_parent"
  298.     android:orientation="vertical">
  299.  
  300.     <EditText
  301.         android:id="@+id/etNewQuentity"
  302.         android:layout_width="match_parent"
  303.         android:layout_height="wrap_content"
  304.         android:hint="Enter quentity ..."
  305.         android:inputType="number"
  306.         android:textSize="20sp" />
  307.  
  308. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement