Java 使用搜索框时未刷新ListView适配器
我有一个活动CheckUserLocation.java,有两个文本视图- 选择CityTextView-从列表中选择特定城市 选择EATEXTVIEW-选择属于该城市的特定区域。 当用户按chooseCityTextView时,将创建一个对话框,其中显示城市列表。目前,我正在展示3个城市——班加罗尔、德里、诺伊达 一旦用户选择了一个城市,城市对话框将被取消。然后用户按ChooseArtExtView,创建另一个对话框,其中显示区域列表 “城市”和“地区”对话框的顶部都有一个搜索框,如果列表过长,可以使用该搜索框搜索特定的城市或地区。为此,我在两个搜索框中都使用了TextWatcher,一个用于城市,另一个用于区域,并在ContextChanged中为两个TextWatcher使用了getFilter方法 当用户选择一个特定的城市时,整个区域列表将被清除,并向其中添加新的区域。 这是代码-Java 使用搜索框时未刷新ListView适配器,java,android,Java,Android,我有一个活动CheckUserLocation.java,有两个文本视图- 选择CityTextView-从列表中选择特定城市 选择EATEXTVIEW-选择属于该城市的特定区域。 当用户按chooseCityTextView时,将创建一个对话框,其中显示城市列表。目前,我正在展示3个城市——班加罗尔、德里、诺伊达 一旦用户选择了一个城市,城市对话框将被取消。然后用户按ChooseArtExtView,创建另一个对话框,其中显示区域列表 “城市”和“地区”对话框的顶部都有一个搜索框,如果列表过长
dialog_ListView_City.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Get the city selected
selectedItem_City = parent.getItemAtPosition(position).toString();
chooseCityTextView.setText(selectedItem_City);
Log.d("Selected City -", selectedItem_City);
try {
//get JSON Data from Model class
JSONArray copyOfJSONArray = CityJSONData.getLocationJSONArray();
Log.e("fetched copy of", "JSONArray");
Log.e("size of JSON Array", "" + copyOfJSONArray.length());
for (int index = 0; index < copyOfJSONArray.length(); index++) {
Log.e("in for loop", "");
//get single JSON Object from the JSONArray
//here, each JSON Object corresponds to a CITY
JSONObject jsonObject = copyOfJSONArray.getJSONObject(index);
//each City has a CITY CODE and a CITY NAME, along with a JSON Object named "AREAS"
String cityName = jsonObject.getString("city_name");
if (cityName.equalsIgnoreCase(selectedItem_City)) {
Log.d("Match", "Found");
JSONArray areasJSONArray = jsonObject.getJSONArray("areas");
//remove previous AREAS from List before adding NEW ones
areaList.clear();
for (int areaIndex = 0; areaIndex < areasJSONArray.length(); areaIndex++) {
JSONObject areaJSONObject = null;
areaJSONObject = areasJSONArray.getJSONObject(areaIndex);
//AREAS object contains "area_code" and "area_name"
String areaName = areaJSONObject.getString("area_name");
Log.e("Area Added -", areaName);
//also add it areaList
areaList.add(areaName);
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
Collections.sort(areaList);
dataAdapterForArea.notifyDataSetChanged();
dialog_City.dismiss();
chooseAreaTextView.setText("");
chooseAreaTextView.setHint("Select Area");
}
问题在于,当用户在“区域”对话框的搜索栏中输入文本时,过滤器工作正常,但即使用户选择了其他城市,适配器也不会使用新的区域值刷新列表
我不明白为什么会发生这种情况,即使我在选择一个新城市时清除了区域列表,并成功地向其中添加了记录器正在打印的新值
这是我遇到的问题的截图-
即使记录器显示班加罗尔的新区域值-第1阶段,JP Nagar和第2阶段,JP Nagar,该对话框仍然显示Noida先前选择的区域6、18、16。您需要做的是重新将适配器分配给列表,因为这里的列表没有反映其数据更改。 像这样重新设计适配器
yourAdapterInstance = new YourAdapter(Pass here your new list. e.g areanames);
yourlistview.setAdapter(yourAdapterInstance);