gpt4 book ai didi

android - 从 afterTextChanged() 更新适配器

转载 作者:行者123 更新时间:2023-11-30 02:14:02 25 4
gpt4 key购买 nike

问题是 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);

}
}

最佳答案

你试过了吗notifyDataSetChangedadapter.notifyDataSetChanged() 而不是 textView.setAdapter(adapter)

你也有行 if (s.length()>0 && responseList.isEmpty()) 但是当你输入第一个数字时,适配器可能不为空,第二个条件 if (s.length()==0) 为假

关于android - 从 afterTextChanged() 更新适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29668934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com