Android 如何为 Fragment 中的微调器设置 ArrayAdapter

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12275678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:17:12  来源:igfitidea点击:

How to set ArrayAdapter for spinner in Fragment

androidandroid-fragmentsandroid-fragmentactivity

提问by Basit

I am trying to add items to spinner in fragment. But i am having problem with the context. Because in fragment there is no context. Here how i am doing

我正在尝试将项目添加到片段中的微调器。但我对上下文有问题。因为在片段中没有上下文。在这里我是怎么做的

public class DetailFrag extends Fragment {

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

        View scrollView = inflater.inflate(R.layout.myscrollview , container, false);       
        LinearLayout linearLayout = (LinearLayout) scrollView.findViewById(R.id.mylayout1);

        for (int i=0; i<questionList.size(); i++) {

            View verticalLinearLayout = inflater.inflate(R.layout.mylistrow,  null);
            View horizontalLInearLaoyout = verticalLinearLayout.findViewById(R.id.questionRow);
            TextView tv = (TextView) horizontalLInearLaoyout.findViewById(R.id.question);
            Spinner spinner = (Spinner) horizontalLInearLaoyout.findViewById(R.id.spinner);

            //Problem: how to define this in fragment createFromResource(this,...)
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                    this, R.array.options_array, android.R.layout.simple_spinner_item);

            EditText editText = (EditText) verticalLinearLayout.findViewById(R.id.txtMultiLine);

            String question = questionList.get(i).question;

            tv.setId(i);
            tv.setText(i + question);
            spinner.setId(i);
            editText.setId(i);

            linearLayout.addView(verticalLinearLayout);          
        }       
        return scrollView;  
    } //end of onCreateView()
} //end of class DetailFrag

回答by Ewoks

In fragments, context is not available the way you expected in your code. Instead of this, use the following:

在片段中,上下文无法按照您在代码中预期的方式使用。代替this,使用以下内容:

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
    getActivity().getBaseContext(),
    R.array.options_array,
    android.R.layout.simple_spinner_item);

回答by Marco Sanchez

Since there we are in a Fragment,'this', won't work thus: Using getActivity().getBaseContext() should be used to replace 'this';

由于我们在一个 Fragment 中,'this' 将不起作用:使用 getActivity().getBaseContext() 应该用于替换 'this';

    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String> 
   (getActivity().getBaseContext(),
                android.R.layout.simple_spinner_dropdown_item,
                listItems);

Note on older versions of Android there is getContext() which will return context of the fragment as well, but for new versions it might not return context. Thus using getActivity() you are turned a Activity which is a context.

请注意,在旧版本的 Android 上, getContext() 也将返回片段的上下文,但对于新版本,它可能不会返回上下文。因此,使用 getActivity() 你变成了一个 Activity,它是一个上下文。

We use getActivity() which gets the current activity you are in followed by getBaseContext() since there can be multiple Context initiated in the Activity. We want to get the one that the Activity itself uses.

我们使用 getActivity() 获取您所在的当前活动,然后是 getBaseContext(),因为活动中可以启动多个上下文。我们想要获得 Activity 本身使用的那个。

回答by Habib Zare

do something like :

做类似的事情:

private Context myContext = null;

public DetailFrag(Context ctx){
    myContext = ctx;
}

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

//...
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                myContext, R.array.options_array, android.R.layout.simple_spinner_item);


//  
}