在Fragment中指定AutoCompleteTextView适配器

时间:2016-02-19 23:35:00

标签: android android-fragments android-arrayadapter autocompletetextview

几乎让它运行起来。 所以我的静态PlaceholderFragment看起来像这样:

public static class PlaceholderFragment extends Fragment {
    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {

    }

    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        // Get a reference to the AutoCompleteTextView in the layout
        AutoCompleteTextView textView = (AutoCompleteTextView) rootView.findViewById(R.id.customer);
        String[] customers = getResources().getStringArray(R.array.al_customers);
        ArrayAdapter<String> adapter =  new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, customers);
        textView.setAdapter(adapter);

        return rootView;
    }
}

所以我的问题是这一行:

ArrayAdapter<String> adapter =  new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries);

我得到的错误如下:

Cannot resolve constructor 'ArraAdapter(com.g.h.MainActivity.PlaceholderFragment,int,java.lang.String[])'

我确实尝试将一个课程放在&#34;这个&#34;:

之前
MainActivity.this

2 个答案:

答案 0 :(得分:1)

Nolly J在评论中给出的答案:

  

&#34;使用getActivity()替换此&#34;

它就像魔法一样。

答案 1 :(得分:1)

__init__.py