簡體   English   中英

如何從自動完成文本視圖中刪除以前選擇的項目

[英]How to remove previous selected item from autocompletetextview

我有幾個地方autocompletetextview,允許用戶 select 多個位置。 然后通過按下下一步按鈕將這些位置傳遞到下一個 java 頁面,以從用戶選擇的位置繪制路線。 現在,如果用戶單擊“后退”按鈕並從其中一個自動完成文本視圖框中刪除文本並再次單擊 NEXT 按鈕,則該 textview 框中的地址不會被刪除,我仍然會在繪制路線的 java 頁面中選擇上一個位置. 我想我應該使用 TextWatcher 來獲取 onTextChanged 或 afterTextChanged 以了解用戶何時從 textview 框中刪除文本。所以,我的問題是:當用戶從 textview 搜索框中刪除文本時,如何刪除或清除先前選擇的位置?

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

這是我的適配器頁面代碼:

 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 實時數據庫中刪除地址。我仍然認為使用 Places 自動完成比 Google Autocomplete 更好,因為這樣我可以檢測到文本更改. 希望這對其他人有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM