In my activity I want to capture user's latitude and longitude and save it to the database. User's latitude and longitude is captured when checked with a debugger but it is not inserting in the database. It is saved as null in database. When user clicks save button saveData() is called and values should insert in db, other values except latitude and longitude are inserting. The fields declared are PHARMACY_LATITUDE + " TEXT NOT NULL, " + PHARMACY_LONGITUDE + " TEXT NOT NULL, " +How do I fix this? Please provide a solution.
public class ParentFragmentChemist extends FragmentActivity implements OnClickListener {
LocationListener locationListener;
LocationManager locationManager;
Double pharmacy_lat,pharmacy_lon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parent_fragment_chemist);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(@NonNull Location location) {
pharmacy_lat = location.getLatitude();
pharmacy_lon = location.getLongitude();
locationManager.removeUpdates(locationListener);
System.out.println("pharmacy_lat "+pharmacy_lat + "pharmacy_lon" +pharmacy_lon);
}
};
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},101);
}else{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if((requestCode == 101) && (grantResults.length > 0)){
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
}
}
}
}
saveData(){
pcount = pcount + 1;
String where = Queryclass.DAYACTIVITY_DATE_ID + " = " + id;
ContentValues cvphcount = new ContentValues();
cvphcount.put(Queryclass.DAYACTIVITY_DCR, "1");
cvphcount.put(Queryclass.DAYACTIVITY_PHARMACYCOUNT, Integer.toString(pcount));
sd.update(Queryclass.TABLE_DAYACTIVITY, cvphcount, where);
ContentValues cvDoctor = new ContentValues();
cvDoctor.put(Queryclass.PHARMACY_DATE_ID, id);
cvDoctor.put(Queryclass.PHARMACY_DCRTYPE, Datafields.dcrtype);
cvDoctor.put(Queryclass.PHARMACY_CONTACTID, Datafields.contactid);
cvDoctor.put(Queryclass.PHARMACY_CONTACTNAME, Datafields.contactname);
cvDoctor.put(Queryclass.PHARMACY_BRICKID, Datafields.pharmacyBrickId);
Calendar instance = Calendar.getInstance();
String hr = String.valueOf(instance.get(Calendar.HOUR));
String mn = String.valueOf(instance.get(Calendar.MINUTE));
cvDoctor.put(Queryclass.PHARMACY_VISITTIME, hr+":"+ mn);
cvDoctor.put(Queryclass.PHARMACY_JOINTWORK, Datafields.jointwork);
cvDoctor.put(Queryclass.PHARMACY_AVAILABILITY, Datafields.availability);
cvDoctor.put(Queryclass.PHARMACY_POB, Datafields.pob);
cvDoctor.put(Queryclass.PHARMACY_NOUNITS, Datafields.nou);
cvDoctor.put(Queryclass.PHARMACY_CALLTYPE, Datafields.chemistCallTypeData);
cvDoctor.put(Queryclass.PHARMACY_CAMPAIGNCALL, Datafields.chemistCampaignCallData);
cvDoctor.put(Queryclass.PHARMACY_RCPA, Datafields.RCPA_DATA);
cvDoctor.put(Queryclass.PHARMACY_REMARKS, Datafields.Customer_remark);
cvDoctor.put(Queryclass.PHARMACY_DATE_TIME, CommonUtils.getDateTimeStamp(dateTimeStampFormat));
cvDoctor.put(Queryclass.PHARMACY_LATITUDE, pharmacy_lat);
cvDoctor.put(Queryclass.PHARMACY_LONGITUDE, pharmacy_lon);
boolean result=sd.isExist(Queryclass.TABLE_PHARMA,id,CommonUtils.getDateTimeStamp(dateTesting),Queryclass.PHARMACY_DATE_TIME,Queryclass.PHARMACY_CONTACTID,Queryclass.PHARMACY_DATE_ID);
if(result){
}else{
sd.insert(Queryclass.TABLE_PHARMA, cvDoctor);
}
The only possible issue here would be that before the pharmacy_lat and pharmacy_lon is being fetched the saveData() function is called.
Since you have not initilized the values of pharmacy_lon & pharmacy_lat and they are not fetched also thus in Database they are saved as null.
To fix this issue you can do below things:
In the above code we will make sure that fetching part is completed and pharmacy_lat and pharmacy_lon actually have some values which is greater than 0.
Hope this will resolve your query and Keep Coding...