0

I try using AutoComplexEdittext in Fragment but I do not know why it's not working (Don't have logcat, just not show sugesstion). Here is my xml file:

<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>

Here is my 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) {

    }
}

Please help me know what is my wrong. Thanks you for reading my question and sorry if this question is stupid.

Xiao King
  • 369
  • 1
  • 13

1 Answers1

2

AutoComplexEditText is not suggestion in Fragment

Because in onActivityCreated arrCity ArrayList object is empty. adding items in arrCity in onPostExecute method.

Set adapter for both AutoCompleteTextView in onPostExecute method :

  @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);
    }

EDIT:

Also change android.R.layout.simple_list_item_1 to android.R.layout.simple_dropdown_item_1line which is second parameter in ArrayAdapter

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • I make sure with you it is not empty. I will take a picture for you. So sorry, i copy wrong code – Xiao King Nov 16 '15 at 19:01
  • It's not working, when I working a first time with autocomplex, I write code like you. But it's not working, and I change it as code in my question, – Xiao King Nov 16 '15 at 19:08
  • @XiaoKing: i think you also need to create a custom class by extending `ArrayAdapter` because `arrCity` ArrayList contains objects of custom class. see following post [How to use ArrayAdapter](http://stackoverflow.com/questions/2265661/how-to-use-arrayadaptermyclass) – ρяσѕρєя K Nov 16 '15 at 19:19
  • I don't know what happen with AutoComplexEddittext, I always using this way to load data to ListView. And everything is ok until I try using AutoComplex. I think my problem does not come to Arraylis contains objects, it come to fragment or Autocomplex. I checked by breakpoint and I saw my arraylist have data. Do not know what wrong with it. :( – Xiao King Nov 16 '15 at 19:24
  • @XiaoKing: but as per my understanding you should create custom adapter class as in http://stackoverflow.com/questions/2265661/how-to-use-arrayadaptermyclass post to get your code work otherwise do a simply test moving with my answer or suggestion : create a simply String type ArrayList with some 4-5 string items then pass it to ArrayAdapter and check getting value or not – ρяσѕρєя K Nov 16 '15 at 19:27
  • Ok. I will do it, hope it work. thanks for your answers. – Xiao King Nov 16 '15 at 19:29
  • It's not working also i try create simple array. I will edit my code, and something wrong with fragment or autocomplex – Xiao King Nov 16 '15 at 19:45
  • @XiaoKing: Change `android.R.layout.simple_list_item_1` to `android.R.layout.simple_dropdown_item_1line` – ρяσѕρєя K Nov 16 '15 at 19:54
  • @XiaoKing: Following example working fine on my end :http://www.java2s.com/Code/Android/UI/FilldatatoAutoCompleteTextViewwithArrayAdapter.htm – ρяσѕρєя K Nov 16 '15 at 19:57
  • after change android.R.layout.simple_list_item_1 to android.R.layout.simple_dropdown_item_1line, it works perfect. What is wrong with simple_list_item_1 ? . – Xiao King Nov 16 '15 at 19:59
  • @XiaoKing: probably it is related to DropDown of `AutoCompleteTextView` – ρяσѕρєя K Nov 16 '15 at 20:01
  • 1
    I see on almost tutorial they always use simple_list_item_1 :(. Thanks you so much. You are my hero :) – Xiao King Nov 16 '15 at 20:04