I had created an android app (Employee Directory) in flash builder with SQLite database connection and having 4 tabs (view). I Just want that when I click on the Admin tab the Employee details from the database should be loaded. Below is my code.
EmployeeDirectory.mxml
<s:TabbedViewNavigatorApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Style>
s|ActionBar
{
chromeColor: #D3D3D3;
}
s|ActionBar #titleDisplay
{
color: #161616;
fontSize: 20;
fontWeight: normal;
fontFamily: "Microsoft Sans Serif";
}
s|TabbedViewNavigator #tabBar
{
chromeColor: #D3D3D3;
color: #161616;
fontFamily: "Microsoft Sans Serif";
iconPlacement:top;
}
</fx:Style>
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
protected function button1_clickHandler(event:MouseEvent):void {
// Switch to the first section in the application.
tabbedNavigator.selectedIndex = 0;
// Switch to the first view in the section.
ViewNavigator(tabbedNavigator.selectedNavigator).popToFirstView();
}
protected function BackBtn_clickHandler(event:MouseEvent):void {
// Switch to the first view in the section.
ViewNavigator(tabbedNavigator.selectedNavigator).popView();
}
]]>
</fx:Script>
<s:navigators>
<s:ViewNavigator label="Home" width="100%" height="100%"
firstView="views.HomeView" icon="@Embed('assets/Home1.png')">
</s:ViewNavigator>
<s:ViewNavigator label="Admin" width="100%" height="100%"
firstView="views.AdminView" icon="@Embed('assets/admin.png')">
<s:navigationContent>
<s:Button icon="@Embed('assets/home.png')"
click="button1_clickHandler(event)"/>
<s:Button icon="@Embed('assets/back2.png')"
click="BackBtn_clickHandler(event)"/>
</s:navigationContent>
</s:ViewNavigator>
<s:ViewNavigator label="Consultants" width="100%" height="100%"
firstView="views.ConsultantsView" icon="@Embed('assets/doctor.jpg')">
<s:navigationContent>
<s:Button icon="@Embed('assets/home.png')"
click="button1_clickHandler(event)"/>
</s:navigationContent>
</s:ViewNavigator>
<s:ViewNavigator label="Extn." width="100%" height="100%"
firstView="views.ExtnView" icon="@Embed('assets/ext.png')">
<s:navigationContent>
<s:Button icon="@Embed('assets/home.png')"
click="button1_clickHandler(event)"/>
<s:Button label="Back"
click="BackBtn_clickHandler(event)"/>
</s:navigationContent>
</s:ViewNavigator>
</s:navigators>
<fx:Declarations>
</fx:Declarations>
</s:TabbedViewNavigatorApplication>
AdminView
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" title="Admin" xmlns:dao="dao.*">
<fx:Declarations>
<dao:EmployeeDAO id="srv"/>
</fx:Declarations>
<s:titleContent>
<s:TextInput id="key" width="100%"/>
</s:titleContent>
<s:actionContent>
<s:Button icon="@Embed('assets/search.png')"
click="data=srv.findByName(key.text)"/>
</s:actionContent>
<s:List id="list" top="0" bottom="0" left="0" right="0"
dataProvider="{data}"
change="navigator.pushView(Employeedetails, list.selectedItem)">
<s:itemRenderer>
<fx:Component>
<s:IconItemRenderer
label="{data.firstName} {data.lastName}"
messageField="title"/>
</fx:Component>
</s:itemRenderer>
</s:List>
</s:View>
EmployeeDAO
package dao
{
import flash.data.SQLConnection;
import flash.data.SQLStatement;
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import mx.collections.ArrayCollection;
public class EmployeeDAO
{
public function getItem(id:int):Employee
{
var sql:String = "SELECT id, firstName, lastName, title, department,
city, email, officePhone, cellPhone, managerId, picture FROM employee WHERE
id=?";
var stmt:SQLStatement = new SQLStatement();
stmt.sqlConnection = sqlConnection;
stmt.text = sql;
stmt.parameters[0] = id;
stmt.execute();
var result:Array = stmt.getResult().data;
if (result && result.length == 1)
return processRow(result[0]);
else
return null;
}
public function findByManager(managerId:int):ArrayCollection
{
var sql:String = "SELECT * FROM employee WHERE managerId=? ORDER BY
lastName, firstName";
var stmt:SQLStatement = new SQLStatement();
stmt.sqlConnection = sqlConnection;
stmt.text = sql;
stmt.parameters[0] = managerId;
stmt.execute();
var result:Array = stmt.getResult().data;
if (result)
{
var list:ArrayCollection = new ArrayCollection();
for (var i:int=0; i<result.length; i++)
{
list.addItem(processRow(result[i]));
}
return list;
}
else
{
return null;
}
}
public function findByName(searchKey:String):ArrayCollection
{
var sql:String = "SELECT * FROM employee WHERE firstName || ' ' ||
lastName || ' ' || department LIKE ? and managerId in ('1','2') ORDER BY id,
managerId, firstName, lastName";
var stmt:SQLStatement = new SQLStatement();
stmt.sqlConnection = sqlConnection;
stmt.text = sql;
stmt.parameters[0] = "%" + searchKey + "%";
stmt.execute();
var result:Array = stmt.getResult().data;
if (result)
{
var list:ArrayCollection = new ArrayCollection();
for (var i:int=0; i<result.length; i++)
{
list.addItem(processRow(result[i]));
}
return list;
}
else
{
return null;
}
}
public function create(employee:Employee):void
{
trace(employee.firstName);
if (employee.manager) trace(employee.manager.id);
var sql:String =
"INSERT INTO employee (id, firstName, lastName, title,
department, managerId, city, officePhone, cellPhone, email, picture) " +
"VALUES (?,?,?,?,?,?,?,?,?,?,?)";
var stmt:SQLStatement = new SQLStatement();
stmt.sqlConnection = sqlConnection;
stmt.text = sql;
stmt.parameters[0] = employee.id;
stmt.parameters[1] = employee.firstName;
stmt.parameters[2] = employee.lastName;
stmt.parameters[3] = employee.title;
stmt.parameters[4] = employee.department;
stmt.parameters[5] = employee.manager ? employee.manager.id : null;
stmt.parameters[6] = employee.city;
stmt.parameters[7] = employee.officePhone;
stmt.parameters[8] = employee.cellPhone;
stmt.parameters[9] = employee.email;
stmt.parameters[10] = employee.picture;
stmt.execute();
employee.loaded = true;
}
protected function processRow(o:Object):Employee
{
var employee:Employee = new Employee();
employee.id = o.id;
employee.firstName = o.firstName == null ? "" : o.firstName;
employee.lastName = o.lastName == null ? "" : o.lastName;
employee.title = o.title == null ? "" : o.title;
employee.department = o.department == null ? "" : o.department;
employee.city = o.city == null ? "" : o.city;
employee.email = o.email == null ? "" : o.email;
employee.officePhone = o.officePhone == null ? "" : o.officePhone;
employee.cellPhone = o.cellPhone == null ? "" : o.cellPhone;
employee.picture = o.picture;
if (o.managerId != null)
{
var manager:Employee = new Employee();
manager.id = o.managerId;
employee.manager = manager;
}
employee.loaded = true;
return employee;
}
public static var _sqlConnection:SQLConnection;
public function get sqlConnection():SQLConnection
{
if (_sqlConnection) return _sqlConnection;
var file:File =
File.applicationDirectory.resolvePath("assets/Employee.db");
var fileExists:Boolean = file.exists;
_sqlConnection = new SQLConnection();
_sqlConnection.open(file);
if (!fileExists)
{
createDatabase();
populateDatabase();
}
return _sqlConnection;
}
protected function createDatabase():void
{
var sql:String =
"CREATE TABLE IF NOT EXISTS employee ( "+
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"firstName VARCHAR(50), " +
"lastName VARCHAR(50), " +
"title VARCHAR(50), " +
"department VARCHAR(50), " +
"managerId INTEGER, " +
"city VARCHAR(50), " +
"officePhone VARCHAR(30), " +
"cellPhone VARCHAR(30), " +
"email VARCHAR(30), " +
"picture VARCHAR(200))";
var stmt:SQLStatement = new SQLStatement();
stmt.sqlConnection = sqlConnection;
stmt.text = sql;
stmt.execute();
}
protected function populateDatabase():void
{
var file:File =
File.applicationDirectory.resolvePath("assets/employees.xml");
var stream:FileStream = new FileStream();
stream.open(file, FileMode.READ);
var xml:XML = XML(stream.readUTFBytes(stream.bytesAvailable));
stream.close();
var employeeDAO:EmployeeDAO = new EmployeeDAO();
for each (var emp:XML in xml.employee)
{
var employee:Employee = new Employee();
employee.id = emp.id;
employee.firstName = emp.firstName;
employee.lastName = emp.lastName;
employee.title = emp.title;
employee.department = emp.department;
employee.city = emp.city;
employee.officePhone = emp.officePhone;
employee.cellPhone = emp.cellPhone;
employee.email = emp.email;
employee.picture = emp.picture;
if (emp.managerId>0)
{
employee.manager = new Employee();
employee.manager.id = emp.managerId;
}
employeeDAO.create(employee);
}
}
}
}
The below is my screen
[After clicking on admin tab from home screen][2]