Commit f5f53ffc authored by Sam's avatar Sam

FINAL VERSION with added EditTexts in the cafe and shop recyclerview

parent 96c2a5fe
......@@ -81,17 +81,34 @@ public class cafe_activity extends AppCompatActivity implements View.OnClickList
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
//click listener for the buttons in the cafe and shop activites if pressed the counter will change
adapter.setOnItemClickListener((position, b) -> {
int quantCounter = Integer.valueOf(cafeItemList.get(position).getQuantity());
if (b) {
quantCounter += 1;
adapter.setOnItemClickListener((position, buttonChecker, quantity_counter) -> {
int quantityCounter;
String quant_CounterString = quantity_counter.getText().toString();
if (quant_CounterString.length() < 1) {
quantityCounter = 0;
} else {
if (!cafeItemList.get(position).getQuantity().equals("0")) {
quantCounter -= 1;
quantityCounter = Integer.valueOf(String.valueOf(quantity_counter.getText()));
}
if (buttonChecker && !quant_CounterString.equals("99")) {
quantityCounter++;
} else if (!buttonChecker && !quant_CounterString.equals("0") && !quant_CounterString.equals("")) {
quantityCounter--;
} else {
quantityCounter = 0;
quantity_counter.setText("0");
}
cafeItemList.get(position).setQuantity(String.valueOf(quantCounter));
cafeItemList.get(position).setQuantity(String.valueOf(quantityCounter));
adapter.notifyItemChanged(position);
});
adapter.setEdListener((position, qc) -> {
cafeItemList.get(position).setQuantity(qc.getText().toString());
});
}
......@@ -156,6 +173,7 @@ public class cafe_activity extends AppCompatActivity implements View.OnClickList
itemPriceCafe = new ArrayList<>();
ArrayList<ArrayList> cafeItems = new ArrayList<>();
for (int i = 0; i < cafeItemList.size(); i++) {
String cafeQuantity = cafeItemList.get(i).getQuantity();
String cafeName = cafeItemList.get(i).getItem();
......@@ -165,7 +183,6 @@ public class cafe_activity extends AppCompatActivity implements View.OnClickList
itemNameCafe.add(cafeName);
itemPriceCafe.add(cafePrice);
}
}
if (itemNameCafe.size() > 0) {
......
package com.example.museumworld.cafe_shop;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
......@@ -17,10 +20,10 @@ public class item_adapter extends RecyclerView.Adapter<item_adapter.ViewHolder_i
private ArrayList<items> itemList;
private OnItemClickListener itemListener;
private OnEditTextChange EdListener;
public item_adapter(ArrayList<items> itemList) {
this.itemList = itemList;
}
public void setOnItemClickListener(OnItemClickListener listener) {
......@@ -28,11 +31,15 @@ public class item_adapter extends RecyclerView.Adapter<item_adapter.ViewHolder_i
}
public void setEdListener(OnEditTextChange listener) {
EdListener = listener;
}
@NonNull
@Override
public ViewHolder_item onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_cafe_viewholder, parent, false);
ViewHolder_item cvh = new ViewHolder_item(v, itemListener);
ViewHolder_item cvh = new ViewHolder_item(v, itemListener, EdListener);
return cvh;
}
......@@ -42,8 +49,10 @@ public class item_adapter extends RecyclerView.Adapter<item_adapter.ViewHolder_i
items currentItem = itemList.get(position);
holder.itemText.setText(currentItem.getItem());
holder.priceText.setText("£"+currentItem.getPrice()+".00");
holder.priceText.setText("£" + currentItem.getPrice() + ".00");
holder.quantity_counter.setText(currentItem.getQuantity());
holder.quantity_counter.setTag(position);
}
......@@ -53,40 +62,71 @@ public class item_adapter extends RecyclerView.Adapter<item_adapter.ViewHolder_i
}
public interface OnItemClickListener {
void onItemClick(int position, boolean b);
void onItemClick(int position, boolean buttonChecker, EditText quantity_counter);
}
public interface OnEditTextChange {
void onETChange(int position, EditText qc);
}
public static class ViewHolder_item extends RecyclerView.ViewHolder {
TextView itemText, priceText, quantity_counter;
TextView itemText, priceText;
EditText quantity_counter;
ImageButton plus_btn, minus_btn;
public ViewHolder_item(@NonNull View itemView, OnItemClickListener listener) {
public ViewHolder_item(@NonNull View itemView, OnItemClickListener listener, OnEditTextChange EditTextChange) {
super(itemView);
itemText = itemView.findViewById(R.id.item_textView);
priceText = itemView.findViewById(R.id.price_textView);
quantity_counter = itemView.findViewById(R.id.quantity_counter);
quantity_counter.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if (EditTextChange != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
EditTextChange.onETChange(position, quantity_counter);
}
}
}
});
plus_btn = itemView.findViewById(R.id.plus_btn);
minus_btn = itemView.findViewById(R.id.minus_btn);
plus_btn.setOnClickListener(view -> {
if(listener != null){
if (listener != null) {
int position = getAdapterPosition();
if(position != RecyclerView.NO_POSITION){
listener.onItemClick(position, true);
if (position != RecyclerView.NO_POSITION) {
listener.onItemClick(position, true, quantity_counter);
}
}
});
//event listener for the minus nad plus buttons in each views
//event listener for the minus and plus buttons in each views
minus_btn.setOnClickListener(view -> {
if(listener != null){
if(listener != null) {
int position = getAdapterPosition();
if(position != RecyclerView.NO_POSITION){
listener.onItemClick(position,false);
if (position != RecyclerView.NO_POSITION) {
listener.onItemClick(position, false, quantity_counter);
}
}
......
......@@ -36,6 +36,7 @@ public class shop_activity extends AppCompatActivity implements View.OnClickList
private ImageView top_img;
private String topImgURL;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......@@ -55,6 +56,7 @@ public class shop_activity extends AppCompatActivity implements View.OnClickList
langaugeDefault = "en";
}
URL += langaugeDefault;
MuseumDetails md = new MuseumDetails();
top_img = findViewById(R.id.top_img);
topImgURL= getIntent().getStringExtra("topURL");
......@@ -78,17 +80,35 @@ public class shop_activity extends AppCompatActivity implements View.OnClickList
adapter = new item_adapter(shopItemList);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
adapter.setOnItemClickListener((position, b) -> {
int quantCounter = Integer.valueOf(shopItemList.get(position).getQuantity());
if (b) {
quantCounter += 1;
adapter.setOnItemClickListener((position, buttonChecker, quantity_counter) -> {
int quantityCounter;
String quant_CounterString = quantity_counter.getText().toString();
if (quant_CounterString.length() < 1) {
quantityCounter = 0;
} else {
if (!shopItemList.get(position).getQuantity().equals("0")) {
quantCounter -= 1;
quantityCounter = Integer.valueOf(String.valueOf(quantity_counter.getText()));
}
if (buttonChecker && !quant_CounterString.equals("99")) {
quantityCounter++;
} else if (!buttonChecker && !quant_CounterString.equals("0") && !quant_CounterString.equals("")) {
quantityCounter--;
} else {
quantityCounter = 0;
quantity_counter.setText("0");
}
shopItemList.get(position).setQuantity(String.valueOf(quantCounter));
shopItemList.get(position).setQuantity(String.valueOf(quantityCounter));
adapter.notifyItemChanged(position);
});
adapter.setEdListener((position, qc) -> {
shopItemList.get(position).setQuantity(qc.getText().toString());
});
......@@ -108,8 +128,6 @@ public class shop_activity extends AppCompatActivity implements View.OnClickList
}
}
RecyclerViewInit();
//initialising the recycler view with the data
RecyclerViewInit();
} catch (JSONException e) {
......
......@@ -105,7 +105,7 @@ public class checkout extends AppCompatActivity implements View.OnClickListener
allItems.addAll(ItemNames);
allQuantities.addAll(ItemQuantity);
allPrices.addAll(ItemPrices);
Log.d("ItemNames", ItemNames.toString());
Log.d("allItemsT", allItems.toString());
......@@ -144,6 +144,7 @@ public class checkout extends AppCompatActivity implements View.OnClickListener
ShopitemsPrice_CV_text.setText(ShopitemsPrice_CV_text.getText() + "\n" + "£" + totalOfItem + ".00");
totalShop += totalOfItem;
}
allPrices.add(String.valueOf(totalOfItem));
TotalnumberOfItems += currentQuantity;
totalOfCheckout += totalOfItem;
}
......
......@@ -85,12 +85,13 @@
>
<TextView
<EditText
android:id="@+id/quantity_counter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="@font/roboto"
android:text="0"
android:inputType="number"
android:padding="5dp"
android:maxLength="2"
android:textAlignment="center"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment