简体   繁体   中英

Communicating between two autocompletetextviews

I'm fairly new to Android, and I'm using Android Studio for development.

I'm developing an app which communicates with SQL Server and retrieves data to Android and displays them on user request.

I'm currently running into an error. What I'm supposed to do is, there is an AutoCompleteTextView field for which I'm retrieving data and displaying for user selection (say, Organization/Company Names). Now, on selecting an option on this field, I have to send a query with this option (the Organization/Company Name) and retrieve data pertaining to this option from the database (say, the Contact Person Names in the selected Organization/Company) and display this data as options on the second AutoCompleteTextView field.

I did this within the OnCreate method using an ArrayAdapter, but the app kept crashing and now I realized that it's because the values for the second AutoCompleteTextView field are not available during OnCreate.

I need to be able to dynamically change the second AutoCompleteTextView field as and when the value for the first AutoCompleteTextView field is selected.

Any suggestions on how I could overcome this?

No need to try to set the result in the second AutoCompleteTextView inside onCreate() . You can do your task outside and when it's done, you set the values to it.Check out the AsyncTask , it might be so useful.

You can inspired with this code and use that :

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) {

    }
});

Good look.

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