user3176634 user3176634 - 2 years ago 76
Android Question

How to dynamically update drop down list on AutoCompleteTextView TextChangedListener?

I want to update the drop down list onTextChanged() with name of states.
As i type an alphabet, the list should get updated with the data from JSON response.

For example, if i type "a", then the list should display all the results having "a".

If i write "del", then the list will display "Delhi".

API is working fine, as i'm able to get the response as i type a letter.
But UI is not working or working properly.

EditProfileFragment:

public class EditProfileFragment extends Fragment {
AutoCompleteTextView mAutoCompleteTextView;
private List<StateModel> mList = new ArrayList<>();
private AreaAdapter mAdapter;

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

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

mAutoCompleteTextView = (AutoCompleteTextView) view.findViewById(R.id.autoComplete);

mAutoCompleteTextView.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
getStateList(s.toString());

}

@Override
public void afterTextChanged(Editable s) {

}
});
return view;
}

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

private void getStateList(CharSequence s) {
String url = "https://example.com/search/name/" + s;
final HashMap<String, String> params = new HashMap<String, String>();

EzApp.networkController.networkCall(getActivity(), url, params, new NetworkCallController.OnResponse() {
@Override
public void onResponseListner(String response) {

Log.i("EPF STATES:--", response.toString());
try {
JSONArray jsonArray = new JSONArray(response);
for (int j = 0; j < jsonArray.length(); j++) {
JSONObject obj = jsonArray.getJSONObject(j);
StateModel model = new StateModel();
model.setName(obj.getString("name"));
model.setId(obj.getString("id"));
mList.add(model);
}
mAdapter = new AreaAdapter(getActivity(),
android.R.layout.simple_spinner_item, mList,
true);
mAutoCompleteTextView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
}


AreaAdapter:

public class AreaAdapter extends ArrayAdapter<StateModel> {
Context context;
Boolean enabled;

public AreaAdapter(Context context, int resourceId,
List<StateModel> item, boolean enabled) {
super(context, resourceId, item);
this.enabled = enabled;
this.context = context;
}

/* private view holder class */
private static class ViewHolder {
private TextView txtName;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

final StateModel rowItem = getItem(position);

View row = null;
if (convertView == null) {
ViewHolder holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.area_autosuggest_row_list, null);
holder.txtName = (TextView) row.findViewById(R.id.txt_name);
row.setTag(holder);
} else {
row = convertView;
}
final ViewHolder holder = (ViewHolder) row.getTag();
holder.txtName.setText(rowItem.getName());

return row;
}
}


API Response:

1.

[{"id":"1","name":"Andaman and Nicobar"},{"id":"2","name":"Andhra Pradesh"},
{"id":"3","name":"Arunachal Pradesh"},{"id":"4","name":"Assam"},
{"id":"25","name":"Nagaland"},{"id":"24","name":"Mizoram"}]


2.

[{"id":"10","name":"Delhi"}]

Answer Source

Remove your code from onTextChanged(), and add this

  private Timer mTimer =new Timer();
  private final long DELAY = 1500;


 @Override
        public void afterTextChanged(final Editable editable) {

            mTimer.cancel();
            if (!editable.toString().equals("")) {
                mTimer = new Timer();
                mTimer.schedule(
                        new TimerTask() {
                            @Override
                            public void run() {
                                runOnUiThread(new TimerTask() {
                                    @Override
                                    public void run() {
                                         getStateList(editable.toString());
                                    }
                                });

                            }
                        },
                        DELAY
                );
            }

        }

In the response block add these lines, Clear your mList first.

@Override public void onResponseListner(String response) {

        Log.i("EPF STATES:--", response.toString());
        try {
            mList.clear();
            mList = new ArrayList<>();

            JSONArray jsonArray = new JSONArray(response);
            for (int j = 0; j < jsonArray.length(); j++) {
                JSONObject obj = jsonArray.getJSONObject(j);
                StateModel model = new StateModel();
                model.setName(obj.getString("name"));
                model.setId(obj.getString("id"));
                mList.add(model);
            }
             mAdapter = new AreaAdapter(getActivity(),
                    android.R.layout.simple_spinner_item, mList,
                    true);
            mAutoCompleteTextView.setAdapter(mAdapter);
            mAutoCompleteTextView.showDropDown();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download