我创建了一个适配器,并使用Google Places API显示地址。我打电话给notifyDataSetChanged();
获取更新列表。当我输入TextView
时,列表不会更改。
Activity
public class SelectAddressActivity extends Activity implements SelectAddressAdapter.OnClickInAdapter {
ArrayList<AddressList> addressList;
SelectAddressAdapter adapter;
EditText addresseEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_address);
final ListView addressListView = (ListView) findViewById(R.id.addressListView);
addresseEditText = (EditText) findViewById(R.id.addressEditText);
//Adapter
adapter = new SelectAddressAdapter(this, android.R.layout.simple_list_item_1, addressList);
addressListView.setAdapter(adapter);
addresseEditText.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(final Editable input) {
scheduler= Executors.newScheduledThreadPool(1);
scheduler.schedule(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
GoogleAPIRequestHandler handler = new GoogleAPIRequestHandler();
String date= handler.execute(input).get();
addressList = new ArrayList<AddressList>();
JSONObject jsonObj = new JSONObject(date);
JSONArray predsJsonArray = jsonObj.getJSONArray("predictions");
for (int i = 0; i < predsJsonArray.length(); i++) {
AddressList a = new AddressList();
String addressJson = predsJsonArray.getJSONObject(i).toString();
a.Deserialize(addressJson);
addressList.add(a);
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
adapter.notifyDataSetChanged();;
}
});
}
}, 300, TimeUnit.MILLISECONDS);
}
});
}
答案 0 :(得分:0)
您没有在适配器中设置更新的地址列表。它仍然有旧数据,所以当你调用notifyDataSetChanged()方法它没有任何影响。