[Android] AutoComplexEditText不是Fragment中的建议

时间:2015-11-16 18:57:39

标签: android android-fragments

我尝试在Fragment中使用AutoComplexEdittext,但我不知道它为什么不起作用(没有logcat,只是没有显示sugesstion)。 这是我的xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <AutoCompleteTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/autoCompleteTextView"
        android:completionThreshold="1"
    />
</RelativeLayout>

这是我的AboutFragment:

public class AboutFragment extends Fragment implements TextWatcher{
    private AutoCompleteTextView atv_test;
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        atv_test = (AutoCompleteTextView)getView().findViewById(R.id.autoCompleteTextView);
        atv_test.addTextChangedListener(this);

        String a[] ={"Abhc","GA","AAA","DDSDS","DDSADSa","dsadsadsahkaz"};
        ArrayAdapter<String> arr = new ArrayAdapter<String>(getView().getContext(),android.R.layout.simple_list_item_1,a);
        atv_test.setAdapter(arr);
        //JSonCityAsyncTask j = new JSonCityAsyncTask();
        //j.execute();
    }

    public void messages(String msg) {
        new AlertDialog.Builder(getActivity()).setTitle("Notification").setMessage(msg).setNeutralButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        }).show();
    }

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


        // Inflate the layout for this fragment
        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}

请帮助我知道我的错误。感谢您阅读我的问题,对不起,如果这个问题很愚蠢。

1 个答案:

答案 0 :(得分:2)

  

AutoFmplexEditText不是片段

中的建议

因为在onActivityCreated arrCity中,ArrayList对象是empty。在arrCity方法的onPostExecute中添加项目。

AutoCompleteTextView方法中为onPostExecute设置适配器:

  @Override
    protected void onPostExecute(ArrayList<CityModel> result) {
        /// 
     arrAdapterCity =new ArrayAdapter<CityModel>(SearchFragment.this.getActivity(),
                                android.R.layout.simple_list_item_1,result);
    et_departure.setAdapter(arrAdapterCity);
    et_arrival.setAdapter(arrAdapterCity);
    }

修改

同时将android.R.layout.simple_list_item_1更改为android.R.layout.simple_dropdown_item_1line,这是ArrayAdapter

中的第二个参数