简体   繁体   English

如何从自动完成文本视图中删除以前选择的项目

[英]How to remove previous selected item from autocompletetextview

I've got a couple of placesautocompletetextview that allow the user to select multiple locations.我有几个地方autocompletetextview,允许用户 select 多个位置。 Those locations are then passed to the next java page by pressing Next button to draw a route from the locations selected by the user.然后通过按下下一步按钮将这些位置传递到下一个 java 页面,以从用户选择的位置绘制路线。 Now, if the user clicks "back" button and deletes the text from one of the autocompletetextview boxes and clicks NEXT button again the address from that textview box is not removed and i still get the previous location selected in the java page that draws the route.现在,如果用户单击“后退”按钮并从其中一个自动完成文本视图框中删除文本并再次单击 NEXT 按钮,则该 textview 框中的地址不会被删除,我仍然会在绘制路线的 java 页面中选择上一个位置. I think i should use TextWatcher to get onTextChanged or afterTextChanged to know when user removes the text from one of the textview boxes.So, my question is: how to remove or clear the previous selected location when user removes text from the textview searchbox?我想我应该使用 TextWatcher 来获取 onTextChanged 或 afterTextChanged 以了解用户何时从 textview 框中删除文本。所以,我的问题是:当用户从 textview 搜索框中删除文本时,如何删除或清除先前选择的位置?

this is one of the autocompletetextview searchbox code:这是 autocompletetextview 搜索框代码之一:

 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"); } } });

this is my adapter page code:这是我的适配器页面代码:

 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(); } } }; } }

SOLUTION: i was looking at this the wrong way.解决方案:我看错了。 actually the way to do it is to save the place selected from autocomplete in firebase realtime database.实际上这样做的方法是将自动完成中选择的位置保存在 firebase 实时数据库中。 if user goes back and deletes the text then i use TextWatcher to detect text change and i set it to delete the address from firebase realtime database.i still think that is better to use Places autocomplete then Google Autocomplete because this way i can detect text change.如果用户返回并删除文本,那么我使用 TextWatcher 来检测文本更改,并将其设置为从 firebase 实时数据库中删除地址。我仍然认为使用 Places 自动完成比 Google Autocomplete 更好,因为这样我可以检测到文本更改. Hope this help others.希望这对其他人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果未选择 AutoCompleteTextView 中的任何项目,则显示错误 - Displaying Error if no item from a AutoCompleteTextView is selected 如何从列表视图中删除所选项? - How to remove a selected item from a listview? 如何通过按下按钮重置 MDC 微调器(AutoCompleteTextview/ExposedDropdownMenu)中的选定项目 - How to reset selected item in MDC spinner (AutoCompleteTextview/ ExposedDropdownMenu) with press of a button 我如何知道Android中AutoCompleteTextView所选项目的位置? - How can I know the position of the selected item of an AutoCompleteTextView in Android? 从AutoCompleteTextView删除字母 - Remove letter from AutoCompleteTextView 在AutoCompleteTextView的CustomAdapter中访问所选项目的值 - accessing values of selected item in AutoCompleteTextView's CustomAdapter 在recyclerview中选择项目后,如何从Firebase中删除值? - How to remove values from Firebase when item is selected in recyclerview? 如何从Arraylist中删除所选项目 <HashMap<String, String> &gt;在android中 - How to remove the selected item from Arraylist<HashMap<String, String>> in android 如何从dataTable内部的selectOneMenu中删除所选项目? - How to remove a selected item from a selectOneMenu inside a dataTable? 如何使用CursorAdapter从列表视图中删除所选项目 - how to remove selected item from listview using CursorAdapter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM