Merge pull request #39 from ClaytonWWilson/aaron-branch

Aaron branch
This commit is contained in:
DreamCoder23 2020-10-06 18:36:52 -07:00 committed by GitHub
commit da14953daf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 107 additions and 28 deletions

View File

@ -1,15 +1,19 @@
package com.example.listify; package com.example.listify;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.ListView; import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
import java.util.ArrayList;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -17,40 +21,64 @@ import androidx.appcompat.app.AppCompatActivity;
public class List extends AppCompatActivity { public class List extends AppCompatActivity {
ListView listView; ListView listView;
String listName = "Sample List"; MyAdapter myAdapter;
String[] pNames = {"Half-gallon organic whole milk"};
String[] pStores = {"Kroger"};
String[] pPrices = {"$5.00"};
int[] pImages = {R.drawable.milk};
//List(String name) { Button incrQuan;
// listName = name; Button decrQuan;
//} Button removeItem;
ArrayList<String> pNames = new ArrayList<>(); //String[] pNames = {"Half-gallon organic whole milk"};
ArrayList<String> pStores = new ArrayList<>(); //String[] pStores = {"Kroger"};
ArrayList<String> pPrices = new ArrayList<>(); //String[] pPrices = {"$5.00"};
ArrayList<String> pQuantity = new ArrayList<>(); //String[] pQuantity = {"1"};
ArrayList<Integer> pImages = new ArrayList<>(); //int[] pImages = {R.drawable.milk};
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { 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); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list); setContentView(R.layout.activity_list);
listView = findViewById(R.id.listView); 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); listView.setAdapter(myAdapter);
} }
class MyAdapter extends ArrayAdapter<String> { class MyAdapter extends ArrayAdapter<String> {
Context context; Context context;
String[] pNames; ArrayList<String> pNames; //String[] pNames;
String[] pStores; ArrayList<String> pStores; //String[] pStores;
String[] pPrices; ArrayList<String> pPrices; //String[] pPrices;
int[] pImages; ArrayList<String> pQuantity; //String[] pQuantity;
ArrayList<Integer> pImages; //int[] pImages;
MyAdapter (Context c, String[] names, String[] stores, String[] prices, int[] images) { MyAdapter (Context c, ArrayList<String> names, ArrayList<String> stores, ArrayList<String> prices, ArrayList<String> quantity, ArrayList<Integer> images) {
super(c, R.layout.listproduct, R.id.productView, names); super(c, R.layout.listproduct, R.id.productView, names);
context = c; context = c;
pNames = names; pNames = names;
pStores = stores; pStores = stores;
pPrices = prices; pPrices = prices;
pQuantity = quantity;
pImages = images; pImages = images;
} }
@ -60,15 +88,65 @@ public class List extends AppCompatActivity {
LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View listproduct = layoutInflater.inflate(R.layout.listproduct, parent,false); 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 name = listproduct.findViewById(R.id.productView);
TextView store = listproduct.findViewById(R.id.storeView); TextView store = listproduct.findViewById(R.id.storeView);
TextView price = listproduct.findViewById(R.id.priceView); 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]); if(!pNames.isEmpty()) {
name.setText(pNames[position]); name.setText(pNames.get(position));
store.setText(pStores[position]); store.setText(pStores.get(position));
price.setText(pPrices[position]); price.setText(pPrices.get(position));
quantity.setText(pQuantity.get(position));
image.setImageResource(pImages.get(position));
}
return listproduct; return listproduct;
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 127 KiB

View File

@ -4,7 +4,8 @@
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants">
<androidx.constraintlayout.widget.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout" android:id="@+id/constraintLayout"
@ -15,22 +16,22 @@
app:layout_constraintTop_toTopOf="parent"> app:layout_constraintTop_toTopOf="parent">
<Button <Button
android:id="@+id/buttonIncr" android:id="@+id/buttonDecr"
android:layout_width="30dp" android:layout_width="30dp"
android:layout_height="30dp" android:layout_height="30dp"
android:layout_marginStart="25dp" android:layout_marginStart="25dp"
android:text="+" android:text="-"
android:textSize="10dp" android:textSize="10dp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView" app:layout_constraintStart_toEndOf="@+id/imageView"
app:layout_constraintTop_toBottomOf="@+id/storeView" /> app:layout_constraintTop_toBottomOf="@+id/storeView" />
<Button <Button
android:id="@+id/buttonDecr" android:id="@+id/buttonIncr"
android:layout_width="30dp" android:layout_width="30dp"
android:layout_height="30dp" android:layout_height="30dp"
android:layout_marginStart="75dp" android:layout_marginStart="75dp"
android:text="-" android:text="+"
android:textSize="10dp" android:textSize="10dp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView" app:layout_constraintStart_toEndOf="@+id/imageView"
@ -93,13 +94,13 @@
app:layout_constraintTop_toBottomOf="@+id/storeView" /> app:layout_constraintTop_toBottomOf="@+id/storeView" />
<TextView <TextView
android:id="@+id/textViewQuantity" android:id="@+id/quantityView"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="1" android:text="q"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/buttonDecr" app:layout_constraintEnd_toStartOf="@+id/buttonIncr"
app:layout_constraintStart_toEndOf="@+id/buttonIncr" app:layout_constraintStart_toEndOf="@+id/buttonDecr"
app:layout_constraintTop_toBottomOf="@+id/storeView" /> app:layout_constraintTop_toBottomOf="@+id/storeView" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>