If using a ListView, rather than trying to build multiple arrays to access stored values or utilising objects such as an Employee object, utilising a
CursorAdapter can be much simpler.
The one complication (simply overcome) is that a Cursor Adapter requires a specific column named
_id and that the column contains an integer value that should uniquely identify a row that is listed. This value is passed to the onItemClick and onItemLongClick events as the 4th parameter (*note although a value is passed for ArrayAdapters that value is the same as the position but as a long*), additionally the underlying Cursor is positioned at the respective row so all the data for the selected row is easily obtained.
SimpleCursorAdapter is as it says a CursorAdapter that is easy to use.
Even with a custom layout as the 4th a 5th parameters are the column names (as a String array) from which the data to be displayed is extracted and the respective id of the view into which the data is displayed (the example uses the name column as the source data for the list which is displayed in the layout who's id is the text1 view).
As such I'd suggest considering the following working example that is an adaptation of your code.
DeptDBHelper.java (simplified but importantly gets
_id column for Cursor Adapter)
public class DeptDPHelper extends SQLiteOpenHelper {
SQLiteDatabase EmpDept;
public DeptDPHelper(Context context) {
super(context, "EmpDept", null, 2);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL("create table department(DeptID integer primary key autoincrement , name text);");
db.execSQL("create table Employee (EmpID integer primary key autoincrement , name text not null," +
"Title text not null , phone text not null , email text not null ," +
"DeptID integer, foreign key(DeptID) references department (DeptID))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists department ");
db.execSQL("drop table if exists Employee ");
onCreate(db);
}
public void InsertEmp (String Empname, String phone, String Email, String title)
{
ContentValues roww = new ContentValues();
roww.put("name", Empname);
roww.put("Title", title);
roww.put("phone", phone);
roww.put("Email", Email);
EmpDept = getWritableDatabase();
EmpDept.insert("Employee ", null, roww);
}
public void InsertDept(String Deptname)
{
ContentValues row = new ContentValues();
row.put("name", Deptname);
EmpDept = getWritableDatabase();
EmpDept.insert("department ", null, row);
}
public Cursor getEmployeeData(String namee) {
EmpDept = this.getWritableDatabase();
return EmpDept.query(
"employee",
new String[]{
"*",
"EmpID AS " + BaseColumns._ID
},
"name LIKE ?",
new String[]{"%" + namee + "%"},
null,null,null
);
}
public Cursor getDeptName(Integer departmentID)
{
EmpDept = getReadableDatabase();
Integer[] deptRow = {departmentID};
return EmpDept.rawQuery("Select name from department where DeptID like ?",new String[]{departmentID.toString()});
}
}
MainActivity.java (uses SimpleCursorAdapter)
public class MainActivity extends Activity {
EditText name;
SimpleCursorAdapter nameAdapter;
DeptDPHelper Emp;
Cursor csr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Emp = new DeptDPHelper(this);
addDataIfNone();
final Button Search = (Button) findViewById(R.id.Search);
name = (EditText) findViewById(R.id.name);
final ListView namelist = (ListView) findViewById(R.id.LV);
csr = Emp.getEmployeeData(name.getText().toString());
nameAdapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2,csr,new String[]{"name"},new int[]{android.R.id.text1},0);
namelist.setAdapter(nameAdapter);
namelist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String empName = csr.getString(csr.getColumnIndex("name"));
String empTitle = csr.getString(csr.getColumnIndex("Title"));
String empPhone = csr.getString(csr.getColumnIndex("phone"));
String empEmail = csr.getString(csr.getColumnIndex("email"));
long empDept = csr.getLong(csr.getColumnIndex("DeptID"));
Toast.makeText(view.getContext(),"You clicked on the Employee with an ID of " + String.valueOf(id) +
" Name is " + empName + " Title is " + empTitle + " Phone is " + empPhone + " Email is " + empEmail + " DeptID is " + String.valueOf(empDept),
Toast.LENGTH_SHORT).show();
}
});
Search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
csr = Emp.getEmployeeData(name.getText().toString());
nameAdapter.swapCursor(csr);
}
});
}
private void addDataIfNone() {
if (DatabaseUtils.queryNumEntries(Emp.getWritableDatabase(),"employee") > 0) return;
Emp.InsertEmp("AhmedMhmmd", "010224455", "gg@jj.com", "xx");
Emp.InsertEmp("MhmmdMhmoud", "010224455", "gg@jj.com", "xx");
Emp.InsertEmp("Ahmedxxx", "010224455", "gg@jj.com", "xx");
Emp.InsertDept("Finance");
Emp.InsertDept("Sales");
}
}
When first run, the App list's the 3 items.
Clicking on an item Toast's all the details EmpID, name, title, phone, email, DeptID.
Typing in the Search and clicking the search button refreshes the list according to the text entered (e.g. x will just display 1 row, q will show no rows, A or a will show 2 rows).