Android Intent doesn't seem to be working - help required!
So my situation is this. I have two tables, e.g. employee and department. When 开发者_如何学运维a new employee is made, there is a spinner which is populated from the departments database, when this is selected it inserts it in to a field in the employee table.
What I am trying to do is have a list of departments and once the department is clicked, it shows all employee's that belong to that department.
So heres what I have so far, the first code is from my class which displays the department list:
protected void onListItemClick(ListView l, View v, int position, long empDept) {
super.onListItemClick(l, v, position, empDept);
Intent i = new Intent(this, EmployeeActivity.class);
i.putExtra(DatabaseAdapter.DEPT_NAME, empDept);
startActivityForResult(i, ACTIVITY_START);
}
I am passing this to the employee list activity, I have tested the code by just displaying everything so it is working to some extent but when I am trying to view by the selected dept name it returns nothing, here is the section of the class that receives the intent:
private void fillData() {
Bundle extras = getIntent().getExtras();
long result = extras.getLong("empDept");
Cursor empCursor= DbHelper.getEmpByDept(result);
startManagingCursor(empCursor);
And here is my database query:
public Cursor getEmpByDept(long empDept)
{
return mDb.query(EMP_TABLE, new String[] { EMP_ID, EMP_TITLE,
EMP_NOTES, EMP_DATE_TIME, EMP_DEPT }, EMP_DEPT + "=" + empDept, null,
null, null, null, null);
}
So what I'm trying to do is just show the employees when the department clicked matches what is in the employees EMP_DEPT field.
Any help is greatly appreciated.
in extras.getLong()
the input is the key of the bundle, that is DatabaseAdapter.DEPT_NAME
and not "empDept"
. Also make sure that DatabaseAdapter.DEPT_NAME
, the key, includes a proper package prefix as indicated in the doc.
btw.: you don't need the last null in the query which is optional and refers to LIMIT (if you don't have one)
精彩评论