在两个自动完成文本视图之间进行通信

Communicating between two autocompletetextviews

我是 Android 的新手,我正在使用 Android Studio 进行开发。

我正在开发一个与 SQL 服务器通信并将数据检索到 Android 并根据用户请求显示它们的应用程序。

我目前 运行 遇到了一个错误。我应该做的是,有一个 AutoCompleteTextView 字段,我正在为其检索数据并显示以供用户选择(例如,Organization/Company 名称)。现在,在此字段上选择一个选项时,我必须发送一个带有此选项的查询(Organization/Company 名称)并从数据库中检索与此选项相关的数据(例如,所选 Organization/Company) 并将此数据显示为第二个 AutoCompleteTextView 字段上的选项。

我在 OnCreate 方法中使用 ArrayAdapter 执行此操作,但应用程序一直崩溃,现在我意识到这是因为第二个 AutoCompleteTextView 字段的值在 OnCreate 期间不可用。

我需要能够在选择第一个 AutoCompleteTextView 字段的值时动态更改第二个 AutoCompleteTextView 字段。

关于我如何克服这个问题有什么建议吗?

无需尝试在onCreate()内的第二个AutoCompleteTextView设置结果。您可以在室外完成任务,完成后,将 AsyncTask 中的值设置为 it.Check,这可能非常有用。

您可以从这段代码中获得启发并使用它:

txtSearch = (TextView) findViewById(R.id.txtSearch);
txtSearch.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence value, int start, int count, int after) {
        if(value.length() > 0){
            notes.clear();
            cursorS = sql.rawQuery("SELECT CategoryID,Title from WebSite_CategoryDB WHERE ParentID = 0 AND Title LIKE + '" + "%" + value + "%" + "'",null);
            try {
                if (cursorS != null) {
                    if (cursorS.moveToFirst()) {
                        do {
                            StartActivity_Entity nte = new StartActivity_Entity();
                            nte._CategoryID = cursorS.getInt(cursorS.getColumnIndex("CategoryID"));
                            nte._Title = cursorS.getString(cursorS.getColumnIndex("Title"));
                            notes.add(nte);
                        } while (cursorS.moveToNext());
                    }
                    adapter.notifyDataSetChanged();
                }
            } catch (Exception e) {
            } finally {
                cursorS.close();
            }
        }else if(value.length() == 0){
            populateListView();
        }
    }

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

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
});

好看