mirror of
https://github.com/ClaytonWWilson/Listify.git
synced 2025-12-16 02:38:47 +00:00
Search results layout fixes and list sorting function
This commit is contained in:
parent
c30819535b
commit
9d41a3078c
@ -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<Product> productList = new ArrayList<>();
|
||||
private List<Product> resultsProductList = new ArrayList<>();
|
||||
private List<Product> 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<Product>() {
|
||||
@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<Product>() {
|
||||
@Override
|
||||
public int compare(Product a, Product b) {
|
||||
return (int)(a.getPrice() - b.getPrice());
|
||||
}
|
||||
});
|
||||
break;
|
||||
|
||||
case 2:
|
||||
resultsProductListFiltered.sort(new Comparator<Product>() {
|
||||
@Override
|
||||
public int compare(Product a, Product b) {
|
||||
return a.getChainName().compareToIgnoreCase(b.getChainName());
|
||||
}
|
||||
});
|
||||
break;
|
||||
|
||||
case 3:
|
||||
resultsProductListFiltered.sort(new Comparator<Product>() {
|
||||
@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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -44,21 +44,22 @@ public class SearchResultsListAdapter extends BaseAdapter {
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
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;
|
||||
}
|
||||
|
||||
@ -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<String, Void, Bitmap> {
|
||||
// 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);
|
||||
// }
|
||||
}
|
||||
|
||||
@ -18,7 +18,5 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:divider="@color/list_divider"
|
||||
android:dividerHeight="1dp"/>
|
||||
<!-- android:listSelector="@drawable/list_row_selector"-->
|
||||
<!-- />-->
|
||||
|
||||
</RelativeLayout>
|
||||
@ -9,64 +9,64 @@
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/item_image"
|
||||
android:layout_width="60dp"
|
||||
android:layout_height="40dp"
|
||||
android:layout_width="80dp"
|
||||
android:layout_height="80dp"
|
||||
android:src="@drawable/ic_baseline_broken_image_600"
|
||||
android:contentDescription="@string/item_image_desc"
|
||||
android:layout_alignTop="@+id/linearLayout"
|
||||
android:layout_alignParentLeft="true"
|
||||
android:layout_alignParentStart="true"/>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
android:layout_alignParentEnd="true"
|
||||
android:layout_alignParentTop="true"
|
||||
android:id="@+id/linearLayout">
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
<TextView
|
||||
android:id="@+id/item_name"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textAllCaps="true"
|
||||
android:layout_centerHorizontal="true"
|
||||
android:text=""
|
||||
android:textSize="15dp"
|
||||
android:layout_centerInParent="true"
|
||||
android:gravity="center"/>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
android:layout_alignParentRight="true"
|
||||
android:layout_alignParentTop="true"
|
||||
android:id="@+id/linearLayout">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/item_desc"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textSize="12sp"
|
||||
android:layout_marginStart="14dp"
|
||||
android:layout_toEndOf="@+id/item_image"
|
||||
android:textStyle="bold"
|
||||
android:textSize="15sp"
|
||||
android:text="" />
|
||||
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
android:layout_alignParentRight="true"
|
||||
android:layout_alignParentBottom="true">
|
||||
<TextView
|
||||
android:id="@+id/item_price_label"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Price: "
|
||||
android:textSize="12sp"
|
||||
android:textStyle="bold" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/item_price"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="14dp"
|
||||
android:paddingTop="20dp"
|
||||
android:textSize="12sp"
|
||||
android:layout_toEndOf="@+id/item_image"
|
||||
android:text=""/>
|
||||
|
||||
<!-- Only displays if the item is cheaper at another store -->
|
||||
<TextView
|
||||
android:id="@+id/cheaper_price_alert"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textSize="12sp"
|
||||
android:textColor="#FF0000"
|
||||
android:layout_marginStart="14dp"
|
||||
android:paddingTop="35dp"
|
||||
android:text=""
|
||||
android:layout_toEndOf="@+id/item_image"
|
||||
/>
|
||||
|
||||
</LinearLayout>
|
||||
<TextView
|
||||
android:id="@+id/item_store"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text=""
|
||||
android:textSize="12sp"
|
||||
android:textStyle="bold"
|
||||
android:layout_alignParentEnd="true"
|
||||
android:layout_alignParentBottom="true"/>
|
||||
|
||||
</RelativeLayout>
|
||||
@ -14,6 +14,8 @@
|
||||
<string name="app_label">Listify</string>
|
||||
<string name="search_hint">Search Test</string>
|
||||
<string name="title_activity_search">SearchActivity</string>
|
||||
<string name="item_image_desc">Product Image</string>
|
||||
|
||||
<!-- Strings used for fragments for navigation -->
|
||||
<string name="first_fragment_label">First Fragment</string>
|
||||
<string name="second_fragment_label">Second Fragment</string>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user