简体   繁体   中英

How to remove previous selected item from autocompletetextview

I've got a couple of placesautocompletetextview that allow the user to select multiple locations. 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. 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. 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?

this is one of the autocompletetextview searchbox code:

 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. 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. Hope this help others.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM