问题是我设置后无法更新adapter
。对于第一个字符,它工作正常,但如果我想要更改它adapter
保持查看相同的建议。如何更新adapter
?
这是我的代码。
public class UzActivity extends Activity {
private static final String DEBUG_TAG = "HttpExample";
List<String> responseList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_uz);
responseList = new ArrayList<String>();
final String url = "http://booking.uz.gov.ua/purchase/station/";
final AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.autoCompleteTextView1);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, responseList);
textView.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) {
}
@Override
public void afterTextChanged(Editable s) {
if (s.length()>0 && responseList.isEmpty()){
try {
new FetchStationTask().execute(url + URLEncoder.encode(s.toString(), "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
textView.setAdapter(adapter);
}
else if(s.length()==0){
responseList.clear();
}
}
});
}
private class FetchStationTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... urls) {
try {
return new UzFetcher().getUrlString(urls[0]);
} catch (IOException e) {
return "Unable to retrieve web page. URL may be invalid.";
}
}
@Override
protected void onPostExecute(String result){
try {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
StationResponse st = objectMapper.readValue(result, StationResponse.class);
for (int i = 0; i<st.mStations.size(); i++){
responseList.add(st.mStations.get(i).getTitle());
}
Log.i(DEBUG_TAG, responseList.get(0));
} catch (IOException e) {
e.printStackTrace();
}
Log.i(DEBUG_TAG, result);
}
}
答案 0 :(得分:1)
onPostExecute(String result){ adapter.notifyDataSetChanged(); }
答案 1 :(得分:0)
你试过notifyDataSetChanged吗? adapter.notifyDataSetChanged()
代替textView.setAdapter(adapter)
。
你也有行if (s.length()>0 && responseList.isEmpty())
但是当你输入第一个数字时,适配器可能不是空的,第二个条件if(s.length()==0)
也是假的