简体   繁体   中英

SimpleCursorAdapter is undefined?

Below is the sample of code giving me grief. The simpleCursorAdapter works if I put it outside of the textchangedlistener but not in I keep getting the message

The constructor SimpleCursorAdapter(new TextWatcher(){}, int, Cursor, String, int, null) is undefined

txt.addTextChangedListener(new TextWatcher() {

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //onTextChanged
            }

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

            }

            public void afterTextChanged(Editable s) {
                //afterTextChanged
                typedText = s.toString();

                Cursor cur = db.rawQuery("SELECT * FROM " +
                        DB_TABLE +" where field LIKE '%" + typedText + "%'" , null);
                String displayFields = "field";
                int displayViews = R.id.bmark_visits;
                setListAdapter(new SimpleCursorAdapter(this, 
                                R.layout.testlist, cur, 
                                displayFields, displayViews, null
                ));
            }
        });

The first parameter to the SimpleCursorAdapter constructor should be your activity. However, inside of your anonymous TextWatcher inner class, this is the instance of the anonymous TextWatcher inner class. Use MyActivity.this instead, where MyActivity is the name of your activity.

You should call activity's manage cursor method. Otherwise, they will leak out.

activity.startManagingCursor( cursor );

Stéphane

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