如何从自动完成文本视图中删除以前选择的项目
How to remove previous selected item from autocompletetextview
我有几个 placeautocompletetextview 允许用户 select 多个位置。然后,通过按下一步按钮从用户编辑的位置 select 绘制路线,这些位置将传递到下一个 java 页面。现在,如果用户单击 "back" 按钮并从其中一个自动完成文本视图框中删除文本并再次单击下一步按钮,该文本视图框中的地址不会被删除,我仍然会得到之前的位置 selected java 绘制路线的页面。我想我应该使用 TextWatcher 获取 onTextChanged 或 afterTextChanged 以了解用户何时从其中一个文本视图 boxes.So 中删除文本,我的问题是:如何在用户删除时删除或清除先前的 selected 位置来自文本视图搜索框的文本?
这是自动完成文本视图搜索框代码之一:
final AutoCompleteTextView location3 = findViewById(R.id.autocomplete3);
location3.setAdapter(new PlaceAutoSuggestAdapter(AddstopPage.this, android.R.layout.simple_dropdown_item_1line));
location3.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) {
}
});
clear_text3 = findViewById(R.id.clear_text3);
clear_text3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
location3.getText().clear();
}
});
location3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
location3.setCursorVisible(true);
}
});
location3.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("Address : ", location3.getText().toString());
value = parent.getItemAtPosition(position).toString();
LatLng latLng = getLatLngFromAddress(location3.getText().toString());
if (latLng != null) {
latlng3 = String.valueOf(latLng);
Log.d("Lat Lng : ", " " + latLng.latitude + " " + latLng.longitude);
Address address = getAddressFromLatLng(latLng);
if (address != null) {
Log.d("Address : ", "" + address.toString());
Log.d("Address Line : ", "" + address.getAddressLine(0));
Log.d("Phone : ", "" + address.getPhone());
Log.d("Pin Code : ", "" + address.getPostalCode());
Log.d("Feature : ", "" + address.getFeatureName());
Log.d("More : ", "" + address.getLocality());
} else {
Log.d("Adddress", "Address Not Found");
}
} else {
Log.d("Lat Lng", "Lat Lng Not Found");
}
}
});
这是我的适配器页面代码:
public class PlaceAutoSuggestAdapter extends ArrayAdapter implements Filterable {
private ArrayList<String> results;
private PlaceApi placeApi=new PlaceApi();
public PlaceAutoSuggestAdapter(Context context,int resId){
super(context,resId);
}
@Override
public int getCount(){
return results.size();
}
@Override
public String getItem(int pos){
return results.get(pos);
}
@NotNull
@Override
public Filter getFilter(){
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults=new FilterResults();
if(constraint!=null){
results=placeApi.autoComplete(constraint.toString());
filterResults.values=results;
filterResults.count=results.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results1) {
if(results1 !=null && results1.count>0){
notifyDataSetChanged();
}
else{
notifyDataSetInvalidated();
}
}
};
}
}
解决方案:我看错了。实际上,这样做的方法是将从自动完成中选择的位置保存在 firebase 实时数据库中。如果用户返回并删除文本,那么我使用 TextWatcher 检测文本更改,并将其设置为从 firebase 实时删除地址 database.i 仍然认为使用 Places 自动完成比 Google 自动完成更好,因为这我可以检测文本更改的方式。希望这对其他人有帮助。
我有几个 placeautocompletetextview 允许用户 select 多个位置。然后,通过按下一步按钮从用户编辑的位置 select 绘制路线,这些位置将传递到下一个 java 页面。现在,如果用户单击 "back" 按钮并从其中一个自动完成文本视图框中删除文本并再次单击下一步按钮,该文本视图框中的地址不会被删除,我仍然会得到之前的位置 selected java 绘制路线的页面。我想我应该使用 TextWatcher 获取 onTextChanged 或 afterTextChanged 以了解用户何时从其中一个文本视图 boxes.So 中删除文本,我的问题是:如何在用户删除时删除或清除先前的 selected 位置来自文本视图搜索框的文本?
这是自动完成文本视图搜索框代码之一:
final AutoCompleteTextView location3 = findViewById(R.id.autocomplete3);
location3.setAdapter(new PlaceAutoSuggestAdapter(AddstopPage.this, android.R.layout.simple_dropdown_item_1line));
location3.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) {
}
});
clear_text3 = findViewById(R.id.clear_text3);
clear_text3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
location3.getText().clear();
}
});
location3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
location3.setCursorVisible(true);
}
});
location3.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("Address : ", location3.getText().toString());
value = parent.getItemAtPosition(position).toString();
LatLng latLng = getLatLngFromAddress(location3.getText().toString());
if (latLng != null) {
latlng3 = String.valueOf(latLng);
Log.d("Lat Lng : ", " " + latLng.latitude + " " + latLng.longitude);
Address address = getAddressFromLatLng(latLng);
if (address != null) {
Log.d("Address : ", "" + address.toString());
Log.d("Address Line : ", "" + address.getAddressLine(0));
Log.d("Phone : ", "" + address.getPhone());
Log.d("Pin Code : ", "" + address.getPostalCode());
Log.d("Feature : ", "" + address.getFeatureName());
Log.d("More : ", "" + address.getLocality());
} else {
Log.d("Adddress", "Address Not Found");
}
} else {
Log.d("Lat Lng", "Lat Lng Not Found");
}
}
});
这是我的适配器页面代码:
public class PlaceAutoSuggestAdapter extends ArrayAdapter implements Filterable {
private ArrayList<String> results;
private PlaceApi placeApi=new PlaceApi();
public PlaceAutoSuggestAdapter(Context context,int resId){
super(context,resId);
}
@Override
public int getCount(){
return results.size();
}
@Override
public String getItem(int pos){
return results.get(pos);
}
@NotNull
@Override
public Filter getFilter(){
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults=new FilterResults();
if(constraint!=null){
results=placeApi.autoComplete(constraint.toString());
filterResults.values=results;
filterResults.count=results.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results1) {
if(results1 !=null && results1.count>0){
notifyDataSetChanged();
}
else{
notifyDataSetInvalidated();
}
}
};
}
}
解决方案:我看错了。实际上,这样做的方法是将从自动完成中选择的位置保存在 firebase 实时数据库中。如果用户返回并删除文本,那么我使用 TextWatcher 检测文本更改,并将其设置为从 firebase 实时删除地址 database.i 仍然认为使用 Places 自动完成比 Google 自动完成更好,因为这我可以检测文本更改的方式。希望这对其他人有帮助。