I have a Fragment that shows the user the product they have selected from the previous screen. I am loading different products into it via a separate CustomProduct class that basically assigns values to the product displayed in this Fragment. It will display things like name, title, cost, description, etc.
The part I'm having trouble with is retrieving the String[] array for the product sizes, i.e. Small, Medium, Large, etc. All of this works with an explicit String[] array, i.e. String[] spinnerValues = {1, 2, 3}; but as soon as I try to make it dynamic, i.e. String[] spinnerValues = product.getSizes(); and I try to open the fragment, my virtual machine shuts down. It doesn't give me an error or anything.
public void DisplayProduct(CustomProduct product) throws ParseException {
String productName = product.getName();
String productCost = "USD $" + product.getCost();
String productDescription = product.getDescription();
String careInstructions = product.getCareInstructions();
String[] spinnerValues = product.getSizes();
Spinner mySpinner = (Spinner) view.findViewById(R.id.spinner_sizes);
mySpinner.setAdapter(new MyAdapter(this, R.layout.custom_spinner, R.id.text_main_seen, spinnerValues));
((TextView) view.findViewById(R.id.productTitle)).setText(productName);
((TextView) view.findViewById(R.id.productCost)).setText(productCost);
((TextView) view.findViewById(R.id.productDescription)).setText(productDescription);
((TextView) view.findViewById(R.id.careInstructions)).setText(careInstructions);
((ImageView) view.findViewById(R.id.productImage)).setImageResource(product.getTemplateDrawableOverlay());
}
public class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(BuyFragment ctx, int custom_spinner, int txtViewResourceId, String[] spinnerValues) {
super(ctx.getActivity(), txtViewResourceId, custom_spinner, spinnerValues);
}
public View getDropDownView(int position, View cnvtView, ViewGroup prnt, String[] spinnerValues) {
return getCustomView(position, cnvtView, prnt, spinnerValues);
}
public View getView(int pos, View cnvtView, ViewGroup prnt, String[] spinnerValues) {
return getCustomView(pos, cnvtView, prnt, spinnerValues);
}
public View getCustomView(int position, View convertView, ViewGroup parent, String[] spinnerValues) {
LayoutInflater inflater = getLayoutInflater(getArguments());
View mySpinner = inflater.inflate(R.layout.custom_spinner, parent, false);
TextView main_text = (TextView)mySpinner.findViewById(R.id.text_main_seen);
main_text.setText(spinnerValues[position]);
return mySpinner;
}
}
I have a separate Class, let's call it "Shirt", that is used to retrieve the String[] array that holds the sizes for the given product.
@Override
public String[] getSizes() {
return new String[] { "XS", "S", "M", "L", "XL", "2XL" };
}
You can see where I'm going with this. I'll be getting different size parameters depending on the product type, and these happen to be separate classes which extend CustomProduct, i.e. the sizing portion of "Leggings" below:
@Override
public String[] getSizes() {
return new String[] { "XS", "S", "M", "L" };
}
Anyone know what would be going on? The problem is definitely in MyAdapter somewhere.
Edit: Logcat
11-19 22:26:59.366 12576-12576/com.app.store D/AndroidRuntime﹕ Shutting down VM
Set a breakpoint at mySpinner.setAdapter(new MyAdapter(this, R.layout.custom_spinner, R.id.text_main_seen));
This is what I got:
this = {com.app.customizer.BuyFragment@5075}"BuyFragment{aedb146 #3 id=0x7f0b0091 android:switcher:2131427473:3}"
product = {com.app.products.Shirt@5084}
productName = {java.lang.String@5085}"American Apparel Unisex All-Over T-Shirt"
productCost = {java.lang.String@5086}"USD $36"
productDescription = {java.lang.String@5087}"Our all-over t-shirts are printed on the highest quality Unisex Polyester Jersey we could get our hands on (100% Polyester). Your all-over t-shirt will be printed identically on both sides. Made in USA."
careInstructions = {java.lang.String@5088}"Machine wash cold, gentle cycle with like colors."
mySpinner = {android.support.v7.widget.AppCompatSpinner@5089}"android.support.v7.widget.AppCompatSpinner{67ee51b V.ED..C.. ......ID 48,726-1032,726 #7f0b0103 app:id/spinner_sizes}"
view = {android.widget.ScrollView@5090}"android.widget.ScrollView{bb7f6b8 VFED.V... ........ 2160,0-3240,1554}"
A Solution:
I set up a spinner spinner and it seemed to work. Just had to make sure my resource ID and layout xml were in the correct order:
Trying to Set up TextSpinner