4

フラグメントでスピナーにアイテムを追加しようとしています。しかし、私は文脈に問題があります。フラグメントにはコンテキストがないためです。ここで私はどのようにやっていますか

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
4

3 に答える 3

-1

次のようなことをしてください:

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


//  
}
于 2012-09-05T06:48:03.550 に答える