SimpleCursorAdapter已弃用,现在该怎么办?

时间:2015-03-20 07:12:18

标签: android sqlite

我一直在学习使用Androids SQLite数据库的教程。可在此处找到该教程:http://coenraets.org/blog/androidtutorial/

我坚持第三步,android studio中的所有内容都显示没有错误。但我似乎无法查询数据库,当我按下搜索按钮时它不会显示结果。是因为这个SimpleCursorAdapter被弃用了吗?如果是这样,我将如何使这个数据库工作。

**MAIN ACTIVITY(EMPLOYEELIST.java)**
    import android.app.Activity;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import android.widget.SimpleCursorAdapter;

    public class MainActivity extends Activity {

        protected EditText searchText;
        protected SQLiteDatabase db;
        protected Cursor cursor;
        protected ListAdapter adapter;
        protected ListView employeeList;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            db = (new DatabaseHelper(this)).getWritableDatabase();
            searchText = (EditText) findViewById (R.id.searchText);
            employeeList = (ListView) findViewById (R.id.list);
        }

        public void search(View view) {
            // || is the concatenation operation in SQLite
            cursor = db.rawQuery("SELECT _id, firstName, lastName, title FROM employee WHERE firstName || ' ' || lastName LIKE ?",
                    new String[]{"%" + searchText.getText().toString() + "%"});
            adapter = new SimpleCursorAdapter(
                    this,
                    R.layout.employee_list_item,
                    cursor,
                    new String[] {"firstName", "lastName", "title"},
                    new int[] {R.id.firstName, R.id.lastName, R.id.title});
            employeeList.setAdapter(adapter);
        }

    }

enter image description here

1 个答案:

答案 0 :(得分:1)

仅弃用构造函数,而不是整个类。

标准构造函数未被弃用,但显然,您不必使用flags = FLAG_AUTO_REQUERY!

来调用它

最后,如果您使用带有CursorLoader的SimpleCursorAdapter,正如文档所示,则不需要此标志,您可以传递0。

请查看此链接

SimpleCursorAdapter deprecated in API version 15?