Android将视图添加到片段并将适配器绑定到视图

时间:2013-11-21 10:45:44

标签: android android-fragments

你好我现在正在玩片段,我似乎找不到解决这个问题的方法..

这是我到目前为止所尝试的:

FragmentReceivingStocksHeader.class

    public class FragmentReceivingStocksHeader extends Fragment {

EditText etVanTransferDocument, etDocumentNumber;
Spinner spLocationFrom, spLocationTo;
Button btExit, btProcess;

ArrayAdapter<String> adapterFrom, adapterTo;
String[] locationsFromArray, locationsToArray;

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

    initControls();

    View rootView = 
            inflater.inflate(R.layout.fragment_receivingstocks_transactionheader, container, false);
    return rootView;



}

private void initControls() {
    // TODO Auto-generated method stub
    spLocationFrom = (Spinner) findViewById (R.id.spLocationFrom);
    spLocationFrom.setEnabled(false);
    spLocationTo = (Spinner) findViewById (R.id.spLocationTo);
    spLocationTo.setEnabled(false);

    locationsFromArray = getResources().getStringArray(R.array.locations);
    locationsToArray = getResources().getStringArray(R.array.locationsto);

    adapterFrom = new ArrayAdapter<String>(this, 
            android.R.layout.simple_spinner_dropdown_item, locationsFromArray);
    adapterTo = new ArrayAdapter<String>(this, 
            android.R.layout.simple_spinner_dropdown_item, locationsToArray);

    spLocationFrom.setAdapter(adapterFrom);

    spLocationFrom.setSelection(1);

    spLocationTo.setAdapter(adapterTo);
    spLocationTo.setSelection(0); 
}

但我在这一行上有错误:

  spLocationFrom = (Spinner) findViewById (R.id.spLocationFrom);

错误消息:

   The method findViewById(int) is undefined for the type FragmentReceivingStocksHeader

还有以下几行:

    adapterFrom = new ArrayAdapter<String>(this, 
            android.R.layout.simple_spinner_dropdown_item, locationsFromArray);
    adapterTo = new ArrayAdapter<String>(this, 
            android.R.layout.simple_spinner_dropdown_item, locationsToArray);

错误消息:

   The constructor ArrayAdapter<String>(FragmentReceivingStocksHeader, int, String[]) is undefined

我在这里做错了什么?我需要你的帮助。感谢。

2 个答案:

答案 0 :(得分:1)

问题是,Fragment类没有类似findViewById的方法,就像Activity一样。您需要使用夸大的rootView.findViewById

对于您的适配器,类似地,构造函数不接受Fragment类型,但是ContextActivity类扩展了Context,您可以使用Fragment方法从getActivity()访问您的活动。

请参阅下面更新的代码:

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


    View rootView = 
            inflater.inflate(R.layout.fragment_receivingstocks_transactionheader, container, false);
    initControls(rootView);
    return rootView;



}

private void initControls(View view) {
    // TODO Auto-generated method stub
    spLocationFrom = (Spinner) view.findViewById (R.id.spLocationFrom);
    spLocationFrom.setEnabled(false);
    spLocationTo = (Spinner) view.findViewById (R.id.spLocationTo);
    spLocationTo.setEnabled(false);

    locationsFromArray = getResources().getStringArray(R.array.locations);
    locationsToArray = getResources().getStringArray(R.array.locationsto);

    adapterFrom = new ArrayAdapter<String>(getActivity(), 
        android.R.layout.simple_spinner_dropdown_item, locationsFromArray);
    adapterTo = new ArrayAdapter<String>(getActivity(), 
        android.R.layout.simple_spinner_dropdown_item, locationsToArray);

    spLocationFrom.setAdapter(adapterFrom);

    spLocationFrom.setSelection(1);

    spLocationTo.setAdapter(adapterTo);
    spLocationTo.setSelection(0); 
}

答案 1 :(得分:1)

在initControls方法中,进行以下修改:

spLocationFrom = (Spinner) view.findViewById (R.id.spLocationFrom);
spLocationTo = (Spinner) view.findViewById (R.id.spLocationTo);

adapterFrom = new ArrayAdapter<String>(getActivity(), 
        android.R.layout.simple_spinner_dropdown_item, locationsFromArray);
adapterTo = new ArrayAdapter<String>(getActivity(), 
        android.R.layout.simple_spinner_dropdown_item, locationsToArray);

说明:在Fragment中,findViewById不像它在Activity中那样可用,因此您需要使用View.findViewById方法。 构造函数或ArrayAdapter将Context作为第一个参数,因此Activity工作但不是Fragment。