我一直在学习使用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);
}
}
答案 0 :(得分:1)
仅弃用构造函数,而不是整个类。
标准构造函数未被弃用,但显然,您不必使用flags = FLAG_AUTO_REQUERY!
最后,如果您使用带有CursorLoader
的SimpleCursorAdapter,正如文档所示,则不需要此标志,您可以传递0。
请查看此链接