简体   繁体   中英

android change autocompletetextview adapter inside TextWatcher

I am using AutoCompleteTextView control due to the huge data the AutoCompleteTextView adapter gets filled via web service inside TextWatcher onTextChanged event.

But this solution does not really work fine, as the dropdown sometimes shows and sometimes not, and sometimes crash. I tried to change to afterTextChanged event but same results

I have seen some answers but they talk about filling the adapter from within the app or SQLite

any Idea how fix these issues i am having

thanks

 actvCentre = (AutoCompleteTextView) findViewById(R.id.actvCentre);
        actvCentre.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                FillCityAuto(charSequence.toString());
                actvCentre.showDropDown();
            }

            @Override
            public void afterTextChanged(Editable editable) {


            }
        });

//--------------------------------------------------------

private void FillCityAuto(String CityIni)
{
    String sXML="";

    // Get XML from Web Service
    try
    {
        SelectWSTask selectWSTask = new SelectWSTask();
        sXML = selectWSTask.execute(CityIni).get();

    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }

    /** The parsing of the xml data is done in a non-ui thread */
    CityAutoLoaderTask caLoaderTask = new CityAutoLoaderTask();

    caLoaderTask.execute(sXML);

}

//-------------------------------------------------------------------------

private class SelectWSTask extends AsyncTask<String, String, String> {

    private String resp;

    private String CallSelectWS(String CityIni) throws IOException, XmlPullParserException {

        WebserviceCall com = new WebserviceCall();

        String aResponse = com.CityAutoComplete("CityAutoComplete", CityIni);

        return aResponse;
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            String CityIni = params[0];

            resp = CallSelectWS(CityIni);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
        return resp;
    }

    @Override
    protected void onPostExecute(String result) {

    }

    @Override
    protected void onPreExecute() {

    }


    @Override
    protected void onProgressUpdate(String... text) {

    }
}

//--------------------------------------------------------

private class CityAutoLoaderTask extends AsyncTask<String, Void, List<HashMap<String, String>>>{

    /** Doing the parsing of xml data in a non-ui thread */
    @Override
    protected List<HashMap<String, String>> doInBackground(String... xmlData) {
        StringReader reader = new StringReader(xmlData[0]);


        MsgsXmlParser msgsXmlParser = new MsgsXmlParser();



            /** Getting the parsed data as a List construct */
        List<HashMap<String, String>> CAuto = null;
        try {
            CAuto = msgsXmlParser.parseCityAuto(reader);
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        return CAuto;
    }

    @Override
    protected void onPostExecute(List<HashMap<String, String>> list) {

        ArrayList<String> arrayList = new ArrayList<String>();
        int lsize = ((ArrayList) list).size();
        for (int i=0; i<lsize;i++) {
            HashMap<String, String> firstMap = list.get(i);
            String City = firstMap.get("City");
            arrayList.add(City);
        }
       //String a = "";
        arrayAdapter = null;
        arrayAdapter = new ArrayAdapter<String>(ListFilter.this, android.R.layout.simple_list_item_1, arrayList);
        arrayAdapter.notifyDataSetChanged();
        actvCentre.setAdapter(arrayAdapter);

        // save index and top position


    }
}

Call actvCentre.showDropDown(); in onPostExecute()

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