Java Android将数据从rawQuery获取到另一个表单
我想从光标到Java Android将数据从rawQuery获取到另一个表单,java,android,sqlite,Java,Android,Sqlite,我想从光标到EmployeeDetails表单中获取Id,但没有结果。这是我的代码: public class EmployeeList extends ListActivity { protected EditText searchText; protected SQLiteDatabase db; protected Cursor cursor; protected ListAdapter adapter; /** Called when the activi
EmployeeDetails
表单中获取Id
,但没有结果。这是我的代码:
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);
}
}
为了从游标对象获取记录值,您需要迭代它并从中检索数据
if(c.getCount()>0){ if (c.moveToFirst()){ c.getLong(0); }}
其中c是光标对象。。。还要检查光标是否不为null…如果firstName是字符串,请使用firstName,如“XYZ”