自动完成的textview检测文本中的更改

时间:2018-07-16 14:41:30

标签: java android

因此,基本上我已经在Google地图上放置了标记。当我在列表中选择一个项目时,这些标记将被清除。

如果用户说要删除其在autocompletetextview处的输入或更改其输入,我希望再次放置标记(再次调用initsearch,这意味着用户是否在之后选择了输入输入更改/删除,标记将再次清除)

我尝试了textwatcher并选择了onNothing,但是它们没有按照我想要的方式工作,或者我将它们错误地实现了。

需要有关如何实现此目标的建议。

private void init(){
    RequestQueue queue = Volley.newRequestQueue(this);
    JsonArrayRequest arrayRequest = new JsonArrayRequest(Request.Method.GET,
            URL + "all", new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            for (int i = 0; i<response.length(); i++){
                try {
                    JSONObject plantObject = response.getJSONObject(i);
                    plantLocation plantLoc = new plantLocation();
                    plantLoc.setPlantName(plantObject.getString("plant"));
                    plantLoc.setLatitude(plantObject.getDouble("latitude"));
                    plantLoc.setLongitude(plantObject.getDouble("longitude"));
                    LatLng latLng =  new LatLng(plantLoc.latitude,plantLoc.longitude);
                        mMap.addMarker(new MarkerOptions()
                            .position(latLng)
                            .title(plantLoc.plantName));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d("Error", error.getMessage());
        }
    });
    queue.add(arrayRequest);
}

private void initSearch() {
    init();
    ArrayAdapter<String> adapter = new ArrayAdapter<String>
            (this, android.R.layout.select_dialog_item, plantList);

    AutoCompleteTextView actv = findViewById(R.id.input_search);
    actv.setThreshold(1);
    actv.setAdapter(adapter);

    actv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            String selectedItem = (String) adapterView.getItemAtPosition(i);
            Log.d("your selected item",selectedItem);
            mMap.clear();
        }
    });
}

1 个答案:

答案 0 :(得分:0)

您应该能够通过对autoCompleteTextView使用侦听器来做到这一点:

yourAutoCompleteTextViewRef.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) 
    {
        //add call to method here
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int 
    after) {                

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
});