I am developing one android app in which I want to store my employee details. I am unable to load data in ListView from Database.
DatabaseHelper.java
\\import statement
public class DatabaseHelper extends SQLiteOpenHelper{
public static final String EMPLOYEE_ID = "EmployeeId";
public static final String EMPLOYEE_NAME = "Name";
public static final String EMPLOYEE_USERNAME = "Username";
public static final String EMPLOYEE_PASSWORD = "Password";
public static final String EMPLOYEE_MANGER_ID = "ManagerId";
public static final String CREATE_TABLE_EMPLOYEE = "CREATE TABLE "+TABLE_NAME_EMPLOYEE+" ("+EMPLOYEE_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+EMPLOYEE_NAME+" varchar(30), "+EMPLOYEE_USERNAME+" varchar(8), "+EMPLOYEE_PASSWORD+" varchar(10), "+EMPLOYEE_MANGER_ID+" Int);";
public static final String DROP_TABLE_EMPLOYEE = "DROP TABLE "+TABLE_NAME_EMPLOYEE+" IF EXISTS";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(CREATE_TABLE_EMPLOYEE);
} catch (SQLException e) {
//toast message
}
//toast message
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try {
db.execSQL(DROP_TABLE_EMPLOYEE);
onCreate(db);
} catch (SQLException e) {
//toast message
}
//toast message
}
}
I have another class for handling only employeetable such as inserting, deleting, etc i.e. EmployeeDatabaseHelper derived from DatabaseHelper.
EmployeeDatabaseHelper.java
\\import statement
public class EmployeeDatabaseHelper extends DatabaseHelper{
public EmployeeDatabaseHelper(Context context) {
super(context);
}
public Cursor getAllEmployee(){
ArrayList<Employee> employeeArrayList = new ArrayList<Employee>();
String sql = "select * from tblEmployee";
SQLiteDatabase db = getWritableDatabase();
Cursor cursor = db.rawQuery(sql, null);
if (cursor.moveToFirst()) {
do{
Employee e = new Employee();
e.name = cursor.getString(cursor.getColumnIndex(EMPLOYEE_NAME));
employeeArrayList.add(e);
} while (cursor.moveToNext());
}
return cursor;
}
}
EmployeeFragment in this section my code is wrong It is giving null pointer Exception. My application close when I open Employeefragment.
EmployeeFragment.java
public class EmployeeFragment extends Fragment {
EmployeeDatabaseHelper dbHelper;
ListView employeeList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_employee, container, false);
employeeList = (ListView) rootView.findViewById(R.id.employeeList);
populateEmployeeList();
return rootView;
}
public void populateEmployeeList(){
Cursor cursor = dbHelper.getAllEmployee();
String[] data = new String[] {dbHelper.EMPLOYEE_ID, dbHelper.EMPLOYEE_NAME};
int[] id = new int[] {R.id.mainTextView, R.id.subTextView};
SimpleCursorAdapter myAdapter = new SimpleCursorAdapter(getActivity(), R.layout.row, cursor, data, id);
employeeList.setAdapter(myAdapter);
}
}
And Employee.java is my another class
public class Employee {
public String username;
public String name;
public String password;
}
In row.xml I have nothing great only two textView. (ie mainTextView,subTextView) and In fragment_employee.xml I have one button to add new employee. and one list to view all employee. I stucked here. if I comment this populateEmployeeList() class then my program works. otherwise it get closed unexpectedly.
LOG cat
FATAL EXCEPTION: main
Process: com.threepin.deepakcorporation, PID: 9393
java.lang.IllegalArgumentException: column '_id' does not exist
at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
at android.widget.CursorAdapter.init(CursorAdapter.java:172)
at android.widget.CursorAdapter.<init>(CursorAdapter.java:120)
at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:52)
at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:78)
at com.threepin.deepakcorporation.EmployeeFragment.populateEmployeeList(EmployeeFragment.java:61)
at com.threepin.deepakcorporation.EmployeeFragment.onActivityCreated(EmployeeFragment.java:52)
at android.app.Fragment.performActivityCreated(Fragment.java:1708)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:908)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:684)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5127)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
at dalvik.system.NativeStart.main(Native Method)