gpt4 book ai didi

java - Android:适配器在 ListView 问题之外更改

转载 作者:行者123 更新时间:2023-12-02 03:13:19 24 4
gpt4 key购买 nike

我正在尝试使用 Geocoder API 构建一个地址搜索器,其中用户输入位置名称,所有相关地址都会显示在 ListView 中。

运行搜索时出现此错误:

java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread.

Make sure your adapter calls notifyDataSetChanged() when its content changes.

这是我的实现:

SimpleStringAdapter adapter;
ArrayList<String> items = new ArrayList<>();
ListView addressList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_address);

addressList = (ListView) findViewById(R.id.addressList);
addressBox = (EditText) findViewById(R.id.addressBox);

//Search for locations when the user types in
addressBox.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(final CharSequence s, int start, int before, int count) {
//Search the typed location
SearchAddresses(searchText);
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {

}

@Override
public void afterTextChanged(Editable s) {

}
});
}

private void SearchAddresses(String searchText) {
Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocationName(searchText, 5);
addressList.setAdapter(null);

if (addresses.size() != 0) {
//Add the Address line to the list
items.add(addresses.get(0).getAddressLine(0));
//Populate the listview
adapter = new SimpleStringAdapter(this, items);
addressList.setAdapter(adapter);
}
} catch (Exception e) {
e.printStackTrace();
}
}

ListView Adapter 只是将文本设置为 TextView。如果您需要查看,请告诉我。

我做错了什么?

最佳答案

不要每次都在 onTextChanged() 方法中初始化适配器。使用notifyDataSetChanged()。这是您的代码示例。

只需用此替换您的代码即可。就是这样。

SimpleStringAdapter adapter;
ArrayList<String> items = new ArrayList<>();
ListView addressList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_address);

addressList = (ListView) findViewById(R.id.addressList);
addressBox = (EditText) findViewById(R.id.addressBox);

adapter = new SimpleStringAdapter(this, items);
addressList.setAdapter(adapter);

//Search for locations when the user types in
addressBox.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(final CharSequence s, int start, int before, int count) {
//Search the typed location
SearchAddresses(searchText);
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {

}

@Override
public void afterTextChanged(Editable s) {

}
});
}

private void SearchAddresses(String searchText) {
Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocationName(searchText, 5);
addressList.setAdapter(null);

if (addresses.size() != 0) {
//Add the Address line to the list
items.add(addresses.get(0).getAddressLine(0));
//Populate the listview
adapter.notifyDataSetChanged();

}
} catch (Exception e) {
e.printStackTrace();
}
}

关于java - Android:适配器在 ListView 问题之外更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40736091/

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