尝试设置TextSpinner

尝试设置TextSpinner

本文介绍了尝试设置TextSpinner的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个简单的文本微调器,以显示产品的尺寸,但是我的VM一直关闭.我已经完成了详细的断点分析,所以甚至不要去那里.您对为什么这种方法不起作用有何看法?

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

我将发布整个片段.您会注意到我正在尝试将String []数组提供给适配器.我有一个明确定义的对象,一个已注释掉的对象是我要从另一个类中提取的.两种方法均无效.

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?

更新:

我的资源ID和布局顺序错误.设置微调框时应该是这样的:

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,

推荐答案

根据文档,当您创建ArrayAdapter时,第三个参数应为TextView要呈现Spinner内容的位置.因此,查看您的XML布局,id为R.id.text_main_seen,因此适配器的创建应类似于:

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);

这篇关于尝试设置TextSpinner的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:34