Skip to content
Snippets Groups Projects
Commit c23b3b91 authored by Fabio Heyming's avatar Fabio Heyming
Browse files

Added the sort

parent d3f3553f
No related branches found
No related tags found
1 merge request!1Mal aufn Master megren
package com.example.mampfmobil.ui; package com.example.mampfmobil.ui;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
...@@ -20,6 +21,8 @@ import com.example.mampfmobil.ui.Classes.ShopItem; ...@@ -20,6 +21,8 @@ import com.example.mampfmobil.ui.Classes.ShopItem;
import com.example.mampfmobil.ui.Classes.Supplier; import com.example.mampfmobil.ui.Classes.Supplier;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Vector; import java.util.Vector;
public class CustomerActivity extends AppCompatActivity { public class CustomerActivity extends AppCompatActivity {
...@@ -132,5 +135,32 @@ private ActivityCustomerBinding binding; ...@@ -132,5 +135,32 @@ private ActivityCustomerBinding binding;
} }
} }
public static void sortShop(String inputString, Context context) {
if (inputString.equals(context.getString(R.string.price))) {
Collections.sort(shopItemList, new Comparator<ShopItem>() {
@Override
public int compare(ShopItem item1, ShopItem item2) {
return Double.compare(item1.item.price, item2.item.price);
}
});
}
if (inputString.equals(context.getString(R.string.restamount))) {
Collections.sort(shopItemList, new Comparator<ShopItem>() {
@Override
public int compare(ShopItem item1, ShopItem item2) {
return Double.compare( item2.quantity,item1.quantity);
}
});
}
if (inputString.equals(context.getString(R.string.name))) {
Collections.sort(shopItemList, new Comparator<ShopItem>() {
@Override
public int compare(ShopItem item1, ShopItem item2) {
return item1.item.name.compareTo(item2.item.name);
}
});
}
}
} }
\ No newline at end of file
...@@ -16,13 +16,19 @@ import android.util.Log; ...@@ -16,13 +16,19 @@ import android.util.Log;
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.ArrayAdapter;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.Spinner;
import com.example.mampfmobil.R; import com.example.mampfmobil.R;
import com.example.mampfmobil.ui.Classes.Recyclerviewadapter_Shop; import com.example.mampfmobil.ui.Classes.Recyclerviewadapter_Shop;
import com.example.mampfmobil.ui.CustomerActivity; import com.example.mampfmobil.ui.CustomerActivity;
import java.util.ArrayList;
import java.util.List;
public class CustomerShopFragment extends Fragment { public class CustomerShopFragment extends Fragment {
private CustomerShopViewModel mViewModel; private CustomerShopViewModel mViewModel;
...@@ -39,6 +45,18 @@ public class CustomerShopFragment extends Fragment { ...@@ -39,6 +45,18 @@ public class CustomerShopFragment extends Fragment {
Button searchButton = rootView.findViewById(R.id.searchButton); Button searchButton = rootView.findViewById(R.id.searchButton);
EditText searchEditText = rootView.findViewById(R.id.editTextSearch); EditText searchEditText = rootView.findViewById(R.id.editTextSearch);
List<String> options = new ArrayList<>();
options.add(getString(R.string.name));
options.add(getString(R.string.price));
options.add(getString(R.string.restamount));
Spinner spinner = rootView.findViewById(R.id.spinnerOptions);
ArrayAdapter<String> adapter2 = new ArrayAdapter<>(requireContext(), android.R.layout.simple_spinner_item, options);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter2);
// Set the layout manager // Set the layout manager
LinearLayoutManager layoutManager = new LinearLayoutManager(requireActivity()); LinearLayoutManager layoutManager = new LinearLayoutManager(requireActivity());
...@@ -61,6 +79,20 @@ public class CustomerShopFragment extends Fragment { ...@@ -61,6 +79,20 @@ public class CustomerShopFragment extends Fragment {
} }
}); });
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedOption = options.get(position);
CustomerActivity.sortShop(selectedOption,getContext());
adapter.notifyDataSetChanged();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// Hier kannst du entsprechend reagieren, wenn nichts ausgewählt ist
}
});
AppCompatActivity activity = (AppCompatActivity) requireActivity(); AppCompatActivity activity = (AppCompatActivity) requireActivity();
// Enable the back button in the ActionBar // Enable the back button in the ActionBar
ActionBar actionBar = activity.getSupportActionBar(); ActionBar actionBar = activity.getSupportActionBar();
......
...@@ -26,12 +26,12 @@ ...@@ -26,12 +26,12 @@
android:id="@+id/myShopRecycler" android:id="@+id/myShopRecycler"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_marginTop="16dp" android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0" app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/searchButton" app:layout_constraintTop_toBottomOf="@+id/spinnerOptions"
app:layout_constraintVertical_bias="0.0"> app:layout_constraintVertical_bias="0.0">
</androidx.recyclerview.widget.RecyclerView> </androidx.recyclerview.widget.RecyclerView>
...@@ -46,4 +46,25 @@ ...@@ -46,4 +46,25 @@
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/editTextSearch" app:layout_constraintStart_toEndOf="@+id/editTextSearch"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
<Spinner
android:id="@+id/spinnerOptions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="@+id/searchButton"
app:layout_constraintTop_toBottomOf="@+id/searchButton" />
<TextView
android:id="@+id/textView19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginEnd="16dp"
android:text="Sort By:"
android:textSize="16sp"
app:layout_constraintEnd_toStartOf="@+id/spinnerOptions"
app:layout_constraintTop_toTopOf="@+id/spinnerOptions" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
<string name="supplierFound">Zusteller existiert schon! Bitte loggen sie sich ein!</string> <string name="supplierFound">Zusteller existiert schon! Bitte loggen sie sich ein!</string>
<string name="add">Hinzufügen</string> <string name="add">Hinzufügen</string>
<string name="restamount_dp">Restmenge: </string> <string name="restamount_dp">Restmenge: </string>
<string name="restamount">Restmenge</string>
<string name="supplier_dp">Lieferant: </string> <string name="supplier_dp">Lieferant: </string>
<string name="buyamount_dp">Kaufmenge:</string> <string name="buyamount_dp">Kaufmenge:</string>
<string name="price_dp">Preis: </string> <string name="price_dp">Preis: </string>
...@@ -64,4 +65,5 @@ ...@@ -64,4 +65,5 @@
<string name="noemptyinput">Keine leere eingaben</string> <string name="noemptyinput">Keine leere eingaben</string>
<string name="item_already_exists">Item existiert bereits</string> <string name="item_already_exists">Item existiert bereits</string>
<string name="added_to_shopping_cart">Zum Einkaufswagen hinzugefügt</string> <string name="added_to_shopping_cart">Zum Einkaufswagen hinzugefügt</string>
<string name="name">name</string>
</resources> </resources>
\ No newline at end of file
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
<string name="supplierFound">Person not found! Please login!</string> <string name="supplierFound">Person not found! Please login!</string>
<string name="add">add</string> <string name="add">add</string>
<string name="restamount_dp">Restamount: </string> <string name="restamount_dp">Restamount: </string>
<string name="restamount">Restamount</string>
<string name="supplier_dp">Supplier: </string> <string name="supplier_dp">Supplier: </string>
<string name="buyamount_dp">Buyamount:</string> <string name="buyamount_dp">Buyamount:</string>
<string name="price_dp">Price: </string> <string name="price_dp">Price: </string>
...@@ -63,5 +64,5 @@ ...@@ -63,5 +64,5 @@
<string name="noemptyinput">noEmptyInput</string> <string name="noemptyinput">noEmptyInput</string>
<string name="item_already_exists">Item already exists</string> <string name="item_already_exists">Item already exists</string>
<string name="added_to_shopping_cart">Added to Shopping Cart</string> <string name="added_to_shopping_cart">Added to Shopping Cart</string>
<string name="name">Name</string>
</resources> </resources>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment