Trying to Set up TextSpinner

2019-08-30 11:47发布

问题:

I'm trying to set up a simple text spinner that displays my products' sizes, but my VM keep shutting down. I've done a detailed break point analysis so please don't even go there. What are your thoughts on why this is not working?

11-20 01:07:23.333  11588-11588/com.app.store D/AndroidRuntime﹕ Shutting down VM

I'll just post my entire Fragment. You'll notice I'm trying to feed a String[] array into the Adapter. I have one that is explicitly defined and one commented out which I'm pulling from another Class. Neither method works.

public class BuyFragment extends Fragment {

    private View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_buy, container, false);
        view = rootView;

        return rootView;
    }

    public int GetProductSizeCode() {
        return ((Spinner) view.findViewById(R.id.spinner_sizes)).getSelectedItemPosition();
    }

    public void DisplayProduct(Product product) throws ParseException {

        String productName = product.getName();
        String productCost = "USD $" + product.getCost();
        String productDescription = product.getDescription();
        String careInstructions = product.getCareInstructions();

//        String [] productSizes = product.getSizes();
        String [] productSizes = new String[] {"1", "2", "3"};
        setUpSpinner(productSizes);

        ((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 void setUpSpinner(String[] productSizes) {
        Spinner spnr;
        spnr = (Spinner)view.findViewById(R.id.spinner_sizes);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                getActivity(),
                R.layout.custom_spinner,
                R.id.spinner_sizes,
                productSizes);
        spnr.setAdapter(adapter);
    }

}

This is my custom spinner but you probably don't need that:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="vertical"
    android:background="@color/elgamigrey">
    <TextView
        android:id="@+id/text_main_seen"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Select your size"
        android:fontFamily="sans-serif-light"
        android:textSize="18sp"
        android:textColor="#fff"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp" />
</LinearLayout>

Anybody have any idea what's going on? Despite anything else that might be going on, is the code at least correct?

Update:

I had the resource ID and the layout in the wrong order. It should have been like this when setting up the spinner:

 R.layout.custom_spinner,
 R.id.spinner_sizes,

回答1:

According to the documentation, when you create your ArrayAdapter, the third parameter should be the id of the TextView where you want to render the Spinner content. Thus, looking at your XML layout, the id is R.id.text_main_seen, so the creation of your adapter should be something like:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(
    getActivity(),
    R.layout.custom_spinner,
    R.id.text_main_seen,
    productSizes);