Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am working on an app in which I need to get destination list from server and set it in autocomplete text view. I am using text watcher and on its onTextChanged event I am fetching destinations data using async task. My problem is I am not sure that am I doing it in a right way. Please review my code below

FragmentHotels.java

public class FragmentHotels extends Fragment implements TextWatcher {    

    final Handler mHandler=new Handler();

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

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_hotels, container, false);

        etDestination = (AutoCompleteTextView) rootView.findViewById(R.id.et_destination);

        etDestination.addTextChangedListener(this);        

        return rootView;
    }




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

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        Log.i(TAG, "TEXT CHANGED TO: " + s.toString());
        if(!TextUtils.isEmpty(s.toString())) {
            getDestinationsAsync(s.toString());
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    private void getDestinationsAsync(final String term){
        new AsyncTask<Void,Void,Void>(){

            final String t=term;

            final Map<String,String> postParams=new HashMap<>();

            @Override
            protected Void doInBackground(Void... params) {
                Log.i(TAG,"do in background in getDestinationAsync");
                postParams.put("term",t);
                new PostHandler(TAG,2,2000).doPostRequest("http://www.bhagwatiholidays.com/admin/webservice/destination_name.php",
                        postParams,
                        new PostHandler.ResponseCallback() {
                            @Override
                            public void response(int status, String response) {
                                Log.i(TAG,"GOT RESPONSE SUCCESSFULLY");
                                try {
                                    Log.i(TAG,"PARSING JSON");
                                    JSONArray array = new JSONArray(response);
                                    Log.i(TAG,"JSON ARRAY SIZE: "+array.length());
                                    final String[] destinations=new String[array.length()];
                                    for(int i=0;i<array.length();i++){
                                        Log.i(TAG,"LABEL: "+array.getJSONObject(i).getString("label"));
                                        destinations[i]=array.getJSONObject(i).getString("label");
                                    }
                                    Log.i(TAG, "SETTING ADAPTER NOW");
                                    mHandler.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            FragmentHotels.this.etDestination.setAdapter(new ArrayAdapter<String>(FragmentHotels.this.getActivity(),
                                                    android.R.layout.simple_list_item_1, destinations));
                                        }
                                    });

                                }
                                catch (JSONException e){
                                    e.printStackTrace();
                                }
                            }
                        });
                return null;
            }
        }.execute();
    }

}
share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.