我想用状态名更新onTextChanged()的下拉列表。 当我输入字母表时,列表应该使用JSON响应中的数据进行更新。
例如,如果我输入“a”,那么列表应显示所有带有“a”的结果。
如果我写“del”,那么列表将显示“Delhi”。
API工作正常,因为我能够在输入字母时获得响应。 但UI无法正常工作或正常工作。
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响应:
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"}]
答案 0 :(得分:0)
从onTextChanged()中删除您的代码,并添加此
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
);
}
}
在响应块中添加以下行,首先清除mList。
@覆盖 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();
}
}