From 9d41a3078c72955499914b720c5eea4af395b7ee Mon Sep 17 00:00:00 2001 From: Clayton Wilson Date: Sat, 26 Sep 2020 22:56:22 -0400 Subject: [PATCH] Search results layout fixes and list sorting function --- .../com/example/listify/SearchResults.java | 107 ++++++++++++++---- .../adapter/SearchResultsListAdapter.java | 13 ++- .../com/example/listify/model/Product.java | 55 +-------- .../res/layout/content_search_results.xml | 2 - .../src/main/res/layout/search_list_item.xml | 96 ++++++++-------- Listify/app/src/main/res/values/strings.xml | 2 + 6 files changed, 145 insertions(+), 130 deletions(-) diff --git a/Listify/app/src/main/java/com/example/listify/SearchResults.java b/Listify/app/src/main/java/com/example/listify/SearchResults.java index 4058a5e..6265894 100644 --- a/Listify/app/src/main/java/com/example/listify/SearchResults.java +++ b/Listify/app/src/main/java/com/example/listify/SearchResults.java @@ -1,5 +1,4 @@ package com.example.listify; -import android.media.Image; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; @@ -16,12 +15,14 @@ import com.example.listify.adapter.SearchResultsListAdapter; import com.example.listify.model.Product; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; public class SearchResults extends AppCompatActivity { private ListView listView; private SearchResultsListAdapter searchResultsListAdapter; - private List productList = new ArrayList<>(); + private List resultsProductList = new ArrayList<>(); + private List resultsProductListFiltered = new ArrayList<>(); @Override @@ -62,12 +63,12 @@ public class SearchResults extends AppCompatActivity { }); listView = (ListView) findViewById(R.id.search_results_list); - searchResultsListAdapter = new SearchResultsListAdapter(this, productList); + searchResultsListAdapter = new SearchResultsListAdapter(this, resultsProductListFiltered); listView.setAdapter(searchResultsListAdapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { - Toast.makeText(SearchResults.this, productList.get(position).getItemName(), Toast.LENGTH_SHORT).show(); + Toast.makeText(SearchResults.this, resultsProductListFiltered.get(position).getItemName(), Toast.LENGTH_SHORT).show(); } }); @@ -75,9 +76,7 @@ public class SearchResults extends AppCompatActivity { searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { - doSearch(query); - // TODO: Display the search results listview return false; } @@ -96,23 +95,85 @@ public class SearchResults extends AppCompatActivity { overridePendingTransition(R.anim.enter_from_right, R.anim.exit_from_right); } - // TODO: This function will handle the search operation with the database and return - // a listview to caller to be displayed - private ListView doSearch(String query) { - // Hardcode some search results... - Product a = new Product("Bottled Water", "0000", "Walmart", "0001", "0123456780", "Bro, it's water...", "Grocery", "$13.37", "9/24/2020", "1", "http://3.bp.blogspot.com/-MfroPPQVDKo/UyhUZWqGvkI/AAAAAAAAB-I/DGk622onsvc/s1600/lettuce-b-kool-cat-meme.jpg"); - Product b = new Product("Tin Foil", "0001", "Walmart", "0001", "0123456781", "Not aluminum foil", "Grocery", "$1.00", "9/24/2020", "1", "https://i.ytimg.com/vi/q9N1doYMxR0/maxresdefault.jpg"); - Product c = new Product("Lettuce", "0002", "Walmart", "0001", "0123456782", "It's still wet", "Grocery", "$0.60", "9/24/2020", "1", "https://www.cattitudedaily.com/wp-content/uploads/2019/12/white-cat-meme-feature.jpg"); - Product d = new Product("Video Game", "0003", "Walmart", "0001", "0123456783", "Fun Vidya Gaemz", "Electronics", "$60.00", "9/24/2020", "1", "https://i1.wp.com/bestlifeonline.com/wp-content/uploads/2018/06/cat-meme-67.jpg?resize=1024%2C1024&ssl=1"); - Product e = new Product("Mountain Dew", "0004", "Walmart", "0001", "0123456784", "Gamer fuel", "Grocery", "$5.87", "9/24/2020", "1", "https://memeguy.com/photos/images/gaming-cat-7680.png"); - Product f = new Product("Tire", "0005", "Walmart", "0001", "0123456785", "30 inch rims", "Automotive", "$146.97", "9/24/2020", "1", "http://cdn.sheknows.com/articles/2013/05/pet5.jpg"); - productList.add(a); - productList.add(b); - productList.add(c); - productList.add(d); - productList.add(e); - productList.add(f); + private void doSearch(String query) { + // TODO: Query Database + // TODO: Create a new Product Object for each result + // TODO: Add each result to productList - return null; + // Hardcode some search results... + for (int i = 0; i < 2; i++) { + Product a = new Product("Bottled Water", "0000", "Walmart", "0001", "0123456780", "Bro, it's water...", "Grocery", 13.37, "9/24/2020", "1", "http://3.bp.blogspot.com/-MfroPPQVDKo/UyhUZWqGvkI/AAAAAAAAB-I/DGk622onsvc/s1600/lettuce-b-kool-cat-meme.jpg"); + Product b = new Product("Tin Foil", "0001", "Walmart", "0001", "0123456781", "Not aluminum foil", "Grocery", 1.00, "9/24/2020", "1", "https://i.ytimg.com/vi/q9N1doYMxR0/maxresdefault.jpg"); + Product c = new Product("Lettuce", "0002", "Walmart", "0001", "0123456782", "Burger King foot lettuce", "Grocery", 0.60, "9/24/2020", "1", "https://www.cattitudedaily.com/wp-content/uploads/2019/12/white-cat-meme-feature.jpg"); + Product d = new Product("Video Game", "0003", "Walmart", "0001", "0123456783", "Fun Vidya Gaemz", "Electronics", 60.00, "9/24/2020", "1", "https://i1.wp.com/bestlifeonline.com/wp-content/uploads/2018/06/cat-meme-67.jpg?resize=1024%2C1024&ssl=1"); + Product e = new Product("Mountain Dew", "0004", "Walmart", "0001", "0123456784", "Gamer fuel", "Grocery", 5.87, "9/24/2020", "1", "https://memeguy.com/photos/images/gaming-cat-7680.png"); + Product f = new Product("Tire", "0005", "Walmart", "0001", "0123456785", "30 inch rims", "Automotive", 146.97, "9/24/2020", "1", "http://cdn.sheknows.com/articles/2013/05/pet5.jpg"); + resultsProductList.add(a); + resultsProductList.add(b); + resultsProductList.add(c); + resultsProductList.add(d); + resultsProductList.add(e); + resultsProductList.add(f); + } + + // Add all results to the filtered list + resultsProductListFiltered.addAll(resultsProductList); + } + + // Sorts the search results + private void sortResults(int sortMode, boolean descending) { + // Sort Modes + // 0 itemName + // 1 price + // 2 chainName + // 3 upc + + // Sort based on mode + switch (sortMode) { + case 0: + resultsProductListFiltered.sort(new Comparator() { + @Override + public int compare(Product a, Product b) { + return a.getItemName().compareToIgnoreCase(b.getItemName()); + } + }); + break; + + // TODO: May need to change this depending on if price is stored as a string or a double + case 1: + resultsProductListFiltered.sort(new Comparator() { + @Override + public int compare(Product a, Product b) { + return (int)(a.getPrice() - b.getPrice()); + } + }); + break; + + case 2: + resultsProductListFiltered.sort(new Comparator() { + @Override + public int compare(Product a, Product b) { + return a.getChainName().compareToIgnoreCase(b.getChainName()); + } + }); + break; + + case 3: + resultsProductListFiltered.sort(new Comparator() { + @Override + public int compare(Product a, Product b) { + return a.getUpc().compareToIgnoreCase(b.getUpc()); + } + }); + break; + } + + if (descending) { + for (int i = 0; i < resultsProductListFiltered.size() / 2; i++) { + Product temp = resultsProductListFiltered.get(i); + resultsProductListFiltered.set(i, resultsProductListFiltered.get(resultsProductListFiltered.size() - i - 1)); + resultsProductListFiltered.set(resultsProductListFiltered.size() - i - 1, temp); + } + } } } \ No newline at end of file diff --git a/Listify/app/src/main/java/com/example/listify/adapter/SearchResultsListAdapter.java b/Listify/app/src/main/java/com/example/listify/adapter/SearchResultsListAdapter.java index b231906..61f9473 100644 --- a/Listify/app/src/main/java/com/example/listify/adapter/SearchResultsListAdapter.java +++ b/Listify/app/src/main/java/com/example/listify/adapter/SearchResultsListAdapter.java @@ -42,23 +42,24 @@ public class SearchResultsListAdapter extends BaseAdapter { @Override public View getView(int position, View convertView, ViewGroup parent) { - if(inflater == null){ + if (inflater == null) { inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); - }if(convertView == null){ + } + if (convertView == null) { convertView = inflater.inflate(R.layout.search_list_item, null); } ImageView productImage = (ImageView) convertView.findViewById(R.id.item_image); TextView itemName = (TextView) convertView.findViewById(R.id.item_name); - TextView description = (TextView) convertView.findViewById(R.id.item_desc); TextView price = (TextView) convertView.findViewById(R.id.item_price); + TextView itemStore = (TextView) convertView.findViewById(R.id.item_store); Product product = productList.get(position); -// product.loadImageView(0, 0, productImage); + // TODO: If image url is broken, display @drawable/ic_baseline_broken_image_600.xml Glide.with(activity).load(product.getImageUrl()).into(productImage); itemName.setText(product.getItemName()); - description.setText(product.getDescription()); - price.setText(product.getPrice()); + price.setText(String.format("$%.2f", product.getPrice())); + itemStore.setText(product.getChainName()); return convertView; } diff --git a/Listify/app/src/main/java/com/example/listify/model/Product.java b/Listify/app/src/main/java/com/example/listify/model/Product.java index 63fb62e..db52386 100644 --- a/Listify/app/src/main/java/com/example/listify/model/Product.java +++ b/Listify/app/src/main/java/com/example/listify/model/Product.java @@ -1,17 +1,5 @@ package com.example.listify.model; -import android.content.Context; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.graphics.drawable.BitmapDrawable; -import android.os.AsyncTask; -import android.util.Log; -import android.widget.ImageView; - -import com.example.listify.R; - -import java.io.InputStream; - public class Product { private String itemName; private String itemId; @@ -20,7 +8,7 @@ public class Product { private String upc; private String description; private String department; - private String price; + private double price; private String retrievedDate; private String fetchCounts; private String imageUrl; @@ -28,7 +16,7 @@ public class Product { public Product() {} public Product(String itemName, String itemId, String chainName, String chainId, String upc, - String description, String department, String price, String retrievedDate, + String description, String department, double price, String retrievedDate, String fetchCounts, String imageUrl) { this.itemName = itemName; this.itemId = itemId; @@ -43,36 +31,6 @@ public class Product { this.imageUrl = imageUrl; } -// private class DownloadImageTask extends AsyncTask { -// ImageView imageView; -// -// public DownloadImageTask(ImageView imageView) { -// this.imageView = imageView; -// } -// -// protected Bitmap doInBackground(String... args) { -// String url = args[0]; -// Bitmap image = null; -// try { -// InputStream in = new java.net.URL(url).openStream(); -// image = BitmapFactory.decodeStream(in); -// } catch (Exception e) { -// Log.e("Error", e.getMessage()); -// e.printStackTrace(); -// } -// return image; -// } -// -// protected void onPostExecute(Bitmap result) { -// // Return the broken image icon as a bitmap if the url is invalid -// if (result == null) { -// imageView.setImageResource(R.drawable.ic_baseline_broken_image_600); -// } else { -// imageView.setImageBitmap(result); -// } -// } -// } - public String getItemName() { return itemName; } @@ -129,11 +87,11 @@ public class Product { this.department = department; } - public String getPrice() { + public double getPrice() { return price; } - public void setPrice(String price) { + public void setPrice(double price) { this.price = price; } @@ -160,9 +118,4 @@ public class Product { public void setImageUrl(String imageUrl) { this.imageUrl = imageUrl; } - -// // TODO: Need to implement image resizing -// public void loadImageView(int height, int width, ImageView imageView) { -// new DownloadImageTask(imageView).execute(this.imageUrl); -// } } diff --git a/Listify/app/src/main/res/layout/content_search_results.xml b/Listify/app/src/main/res/layout/content_search_results.xml index 5041fd3..cd08592 100644 --- a/Listify/app/src/main/res/layout/content_search_results.xml +++ b/Listify/app/src/main/res/layout/content_search_results.xml @@ -18,7 +18,5 @@ android:layout_height="wrap_content" android:divider="@color/list_divider" android:dividerHeight="1dp"/> - - \ No newline at end of file diff --git a/Listify/app/src/main/res/layout/search_list_item.xml b/Listify/app/src/main/res/layout/search_list_item.xml index 4bd41b7..469752d 100644 --- a/Listify/app/src/main/res/layout/search_list_item.xml +++ b/Listify/app/src/main/res/layout/search_list_item.xml @@ -9,64 +9,64 @@ + android:layout_alignParentStart="true"/> + + + + + + + + + + android:layout_toEndOf="@+id/item_image" + /> - - - - - - - - - - - - - - + android:text="" + android:textSize="12sp" + android:textStyle="bold" + android:layout_alignParentEnd="true" + android:layout_alignParentBottom="true"/> \ No newline at end of file diff --git a/Listify/app/src/main/res/values/strings.xml b/Listify/app/src/main/res/values/strings.xml index 82493fc..a969f55 100644 --- a/Listify/app/src/main/res/values/strings.xml +++ b/Listify/app/src/main/res/values/strings.xml @@ -14,6 +14,8 @@ Listify Search Test SearchActivity + Product Image + First Fragment Second Fragment