mirror of
https://github.com/ClaytonWWilson/Listify.git
synced 2025-12-16 10:48:46 +00:00
Can now change quantity of items
This commit is contained in:
parent
366f347e03
commit
94e636f1d5
@ -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;
|
||||||
@ -18,39 +22,48 @@ import androidx.appcompat.app.AppCompatActivity;
|
|||||||
public class List extends AppCompatActivity {
|
public class List extends AppCompatActivity {
|
||||||
ListView listView;
|
ListView listView;
|
||||||
String listName = "Sample List";
|
String listName = "Sample List";
|
||||||
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;
|
||||||
//}
|
|
||||||
|
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);
|
||||||
|
|
||||||
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 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 +73,52 @@ 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);
|
||||||
|
decrQuan.setOnClickListener(new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
TextView quantityText = (TextView) findViewById(R.id.quantityView);
|
||||||
|
int q = Integer.parseInt(quantityText.getText().toString()) - 1;
|
||||||
|
quantityText.setText(Integer.toString(q));
|
||||||
|
|
||||||
|
if(q <= 1) {
|
||||||
|
decrQuan.setEnabled(false);
|
||||||
|
}
|
||||||
|
if(q < 10) {
|
||||||
|
incrQuan.setEnabled(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
decrQuan.setEnabled(false);
|
||||||
|
|
||||||
|
incrQuan = (Button) listproduct.findViewById(R.id.buttonIncr);
|
||||||
|
incrQuan.setOnClickListener(new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
TextView quantityText = (TextView) findViewById(R.id.quantityView);
|
||||||
|
int q = Integer.parseInt(quantityText.getText().toString()) + 1;
|
||||||
|
quantityText.setText(Integer.toString(q));
|
||||||
|
|
||||||
|
if(q > 1) {
|
||||||
|
decrQuan.setEnabled(true);
|
||||||
|
}
|
||||||
|
if(q >= 10) {
|
||||||
|
incrQuan.setEnabled(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
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]);
|
name.setText(pNames.get(position));
|
||||||
name.setText(pNames[position]);
|
store.setText(pStores.get(position));
|
||||||
store.setText(pStores[position]);
|
price.setText(pPrices.get(position));
|
||||||
price.setText(pPrices[position]);
|
quantity.setText(pQuantity.get(position));
|
||||||
|
image.setImageResource(pImages.get(position));
|
||||||
|
|
||||||
return listproduct;
|
return listproduct;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,22 +15,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 +93,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>
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user