diff --git a/Listify/app/src/main/java/com/example/listify/List.java b/Listify/app/src/main/java/com/example/listify/List.java index ae251ea..f011f96 100644 --- a/Listify/app/src/main/java/com/example/listify/List.java +++ b/Listify/app/src/main/java/com/example/listify/List.java @@ -1,15 +1,19 @@ package com.example.listify; import android.content.Context; +import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; +import android.widget.Button; +import android.widget.EditText; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; +import java.util.ArrayList; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -17,40 +21,64 @@ import androidx.appcompat.app.AppCompatActivity; public class List extends AppCompatActivity { ListView listView; - String listName = "Sample List"; - String[] pNames = {"Half-gallon organic whole milk"}; - String[] pStores = {"Kroger"}; - String[] pPrices = {"$5.00"}; - int[] pImages = {R.drawable.milk}; + MyAdapter myAdapter; - //List(String name) { - // listName = name; - //} + Button incrQuan; + Button decrQuan; + Button removeItem; + + ArrayList pNames = new ArrayList<>(); //String[] pNames = {"Half-gallon organic whole milk"}; + ArrayList pStores = new ArrayList<>(); //String[] pStores = {"Kroger"}; + ArrayList pPrices = new ArrayList<>(); //String[] pPrices = {"$5.00"}; + ArrayList pQuantity = new ArrayList<>(); //String[] pQuantity = {"1"}; + ArrayList pImages = new ArrayList<>(); //int[] pImages = {R.drawable.milk}; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { + pNames.add("Half-gallon organic whole milk"); + pStores.add("Kroger"); + pPrices.add("$5.00"); + pQuantity.add("1"); + pImages.add(R.drawable.milk); + + pNames.add("5-bunch medium bananas"); + pStores.add("Kroger"); + pPrices.add("$3.00"); + pQuantity.add("1"); + pImages.add(R.drawable.bananas); + + pNames.add("JIF 40-oz creamy peanut butter"); + pStores.add("Kroger"); + pPrices.add("$7.00"); + pQuantity.add("1"); + pImages.add(R.drawable.peanutbutter); + + int currSize = pNames.size(); + super.onCreate(savedInstanceState); setContentView(R.layout.activity_list); listView = findViewById(R.id.listView); + myAdapter = new MyAdapter(this, pNames, pStores, pPrices, pQuantity, pImages); - MyAdapter myAdapter = new MyAdapter(this, pNames, pStores, pPrices, pImages); listView.setAdapter(myAdapter); } class MyAdapter extends ArrayAdapter { Context context; - String[] pNames; - String[] pStores; - String[] pPrices; - int[] pImages; + ArrayList pNames; //String[] pNames; + ArrayList pStores; //String[] pStores; + ArrayList pPrices; //String[] pPrices; + ArrayList pQuantity; //String[] pQuantity; + ArrayList pImages; //int[] pImages; - MyAdapter (Context c, String[] names, String[] stores, String[] prices, int[] images) { + MyAdapter (Context c, ArrayList names, ArrayList stores, ArrayList prices, ArrayList quantity, ArrayList images) { super(c, R.layout.listproduct, R.id.productView, names); context = c; pNames = names; pStores = stores; pPrices = prices; + pQuantity = quantity; pImages = images; } @@ -60,15 +88,65 @@ public class List extends AppCompatActivity { LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View listproduct = layoutInflater.inflate(R.layout.listproduct, parent,false); - ImageView image = listproduct.findViewById(R.id.imageView); + decrQuan = (Button) listproduct.findViewById(R.id.buttonDecr); + incrQuan = (Button) listproduct.findViewById(R.id.buttonIncr); + removeItem = (Button) listproduct.findViewById(R.id.buttonDel); + + decrQuan.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + int q = Integer.parseInt(pQuantity.get(position)) - 1; + pQuantity.set(position, Integer.toString(q)); + listView.setAdapter(myAdapter); + } + }); + if(Integer.parseInt(pQuantity.get(position)) <= 1) { + decrQuan.setEnabled(false); + } + if(Integer.parseInt(pQuantity.get(position)) < 10) { + incrQuan.setEnabled(true); + } + + incrQuan.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + int q = Integer.parseInt(pQuantity.get(position)) + 1; + pQuantity.set(position, Integer.toString(q)); + listView.setAdapter(myAdapter); + } + }); + if(Integer.parseInt(pQuantity.get(position)) > 1) { + decrQuan.setEnabled(true); + } + if(Integer.parseInt(pQuantity.get(position)) >= 10) { + incrQuan.setEnabled(false); + } + + removeItem.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + pNames.remove(position); + pStores.remove(position); + pPrices.remove(position); + pQuantity.remove(position); + pImages.remove(position); + listView.setAdapter(myAdapter); + } + }); + TextView name = listproduct.findViewById(R.id.productView); TextView store = listproduct.findViewById(R.id.storeView); TextView price = listproduct.findViewById(R.id.priceView); + TextView quantity = listproduct.findViewById(R.id.quantityView); + ImageView image = listproduct.findViewById(R.id.imageView); - image.setImageResource(pImages[position]); - name.setText(pNames[position]); - store.setText(pStores[position]); - price.setText(pPrices[position]); + if(!pNames.isEmpty()) { + name.setText(pNames.get(position)); + store.setText(pStores.get(position)); + price.setText(pPrices.get(position)); + quantity.setText(pQuantity.get(position)); + image.setImageResource(pImages.get(position)); + } return listproduct; } diff --git a/Listify/app/src/main/res/drawable/bananas.png b/Listify/app/src/main/res/drawable/bananas.png new file mode 100644 index 0000000..ff750c7 Binary files /dev/null and b/Listify/app/src/main/res/drawable/bananas.png differ diff --git a/Listify/app/src/main/res/drawable/peanutbutter.png b/Listify/app/src/main/res/drawable/peanutbutter.png new file mode 100644 index 0000000..74dff48 Binary files /dev/null and b/Listify/app/src/main/res/drawable/peanutbutter.png differ diff --git a/Listify/app/src/main/res/layout/listproduct.xml b/Listify/app/src/main/res/layout/listproduct.xml index a4d65fe..96a6fed 100644 --- a/Listify/app/src/main/res/layout/listproduct.xml +++ b/Listify/app/src/main/res/layout/listproduct.xml @@ -4,7 +4,8 @@ xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" - android:layout_height="match_parent"> + android:layout_height="match_parent" + android:descendantFocusability="blocksDescendants">