android更改TextWatcher中的autocompletetextview适配器

时间:2014-07-10 17:02:15

标签: java android web-services autocompletetextview

我正在使用AutoCompleteTextView控件 由于数据庞大,AutoCompleteTextView适配器通过TextWatcher onTextChanged事件中的Web服务填充。

但是这个解决方案并没有真正起作用,因为下拉有时显示,有时不显示,有时会崩溃。 我尝试更改为afterTextChanged事件但结果相同

我已经看到了一些答案,但他们谈到从app或SQLite中填充适配器

任何想法如何解决我遇到的这些问题

感谢

 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


    }
}

1 个答案:

答案 0 :(得分:1)

actvCentre.showDropDown();

中致电onPostExecute()