如何在片段内使用findViewById作为自动完成文本视图

时间:2016-10-19 02:57:45

标签: android-studio android-fragments autocompletetextview

到目前为止,这是我的代码。我应该提到这是我第一次尝试使用Android工作室。你们都可以推荐的任何书籍都会很棒:

public class master_profile extends Fragment {

    AutoCompleteTextView autoCompleteTextView;
    public master_profile() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_master_profile, container, false);
    autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
    String[] words = getResources().getStringArray(R.array.auto_complete);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,words)

}

}

1 个答案:

答案 0 :(得分:0)

public class master_profile extends Fragment {

    AutoCompleteTextView autoCompleteTextView;

    public master_profile() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_master_profile, container, false);
        autoCompleteTextView = (AutoCompleteTextView) rootView.findViewById(R.id.autoCompleteTextView);
        String[] words = getResources().getStringArray(R.array.auto_complete);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, words);

        return rootView;

    }
}