Android从rawQuery获取数据到另一种形式
我想从光标获取 Id
到 EmployeeDetails
表单,但没有得到结果。这是我的代码:
public class EmployeeList extends ListActivity {
protected EditText searchText;
protected SQLiteDatabase db;
protected Cursor cursor;
protected ListAdapter adapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
db = (new DatabaseHelper(this)).getWritableDatabase();
searchText = (EditText) findViewById (R.id.searchText);
}
public void search(View view) {
// || is the concatenation operation in SQLite
cursor = db.rawQuery("SELECT _id FROM employee WHERE firstName=?",
new String[]{searchText.getText().toString()});
Intent intent = new Intent(this, EmployeeDetails.class);
long id = cursor.getLong(cursor.getColumnIndex("_id"));
intent.putExtra("EMPLOYEE_ID", id);
startActivity(intent);
}
}
I want get Id
from the cursor to the EmployeeDetails
form, but I get no result. This is my code:
public class EmployeeList extends ListActivity {
protected EditText searchText;
protected SQLiteDatabase db;
protected Cursor cursor;
protected ListAdapter adapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
db = (new DatabaseHelper(this)).getWritableDatabase();
searchText = (EditText) findViewById (R.id.searchText);
}
public void search(View view) {
// || is the concatenation operation in SQLite
cursor = db.rawQuery("SELECT _id FROM employee WHERE firstName=?",
new String[]{searchText.getText().toString()});
Intent intent = new Intent(this, EmployeeDetails.class);
long id = cursor.getLong(cursor.getColumnIndex("_id"));
intent.putExtra("EMPLOYEE_ID", id);
startActivity(intent);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了从游标对象获取记录值,您需要迭代它并从中检索数据...
其中 c 是游标对象...还要检查游标是否不为空...
For getting the record values from cursor object you need to iterate it and retrieve the data from it...
where c is your cursor object... Also check whether cursor is not null...
如果firstName是字符串,则使用firstName,如'XYZ'
If firstName is String use firstName like 'XYZ'