【问题标题】:How to use findViewById for an autocomplete textview inside a fragment如何将 findViewById 用于片段内的自动完成文本视图
【发布时间】:2016-10-19 02:57:45
【问题描述】:

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

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)

}

}

【问题讨论】:

    标签: android-studio android-fragments autocompletetextview


    【解决方案1】:
    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;
    
        }
    }
    

    【讨论】:

    • 新的错误是cannot resolve constructor ArrayAdapter
    • ArrayAdapter 适配器 = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, words);我将编辑我的答案
    猜你喜欢
    • 2021-02-12
    • 2020-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多