Question: Hello can you please fix my android studio api 2 7 gps code?. The problem seems to be is that databasehelper class is never used

Hello can you please fix my android studio api 27 gps code?. The problem seems to be is that databasehelper class is never used and so it cant be resolved. Can you please fix it so that the class is used?There are more errors too. I will give you two java classes which are mainactivity.java and databasehelper.java because those seem to have the errors. Please give me the full codes for each.
Mainactivity.java:
package com.example.gpslocationapp;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private LocationManager locationManager;
private DatabaseHelper dbHelper;
private TextView tvLocationDetails;
private ListView locationListView;
private ArrayList locationList;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize views and database helper
tvLocationDetails = findViewById(R.id.tvLocationDetails);
Button btnRecordLocation = findViewById(R.id.btnRecordLocation);
locationListView = findViewById(R.id.locationListView);
dbHelper = new DatabaseHelper(this);
// Setup the location list
locationList = new ArrayList>();
ArrayAdapter adapter = new ArrayAdapter>(this, android.R.layout.simple_list_item_1, locationList);
locationListView.setAdapter(adapter);
// Initialize Location Manager
locationManager =(LocationManager) getSystemService(LOCATION_SERVICE);
// Button click to record location
btnRecordLocation.setOnClickListener(v ->{
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, locationListener);
});
}
private final LocationListener locationListener = new LocationListener(){
@SuppressLint("SetTextI18n")
@Override
public void onLocationChanged(Location location){
double latitude = location.getLatitude();
double longitude = location.getLongitude();
// Show the coordinates
tvLocationDetails.setText("Lat: "+ latitude +", Lon: "+ longitude);
// Save to the database
dbHelper.insertLocation(latitude, longitude);
// Add to the location list (for display)
locationList.add("Lat: "+ latitude +", Lon: "+ longitude);
((ArrayAdapter) locationListView.getAdapter()).notifyDataSetChanged();
Toast.makeText(MainActivity.this, "Location Recorded", Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras){}
@Override
public void onProviderEnabled(@NonNull String provider){}
@Override
public void onProviderDisabled(@NonNull String provider){}
};
}
Databasehelper.java
package com.example.gpslocationapp;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "locations.db";
private static final int DATABASE_VERSION =1;
public static final String TABLE_LOCATIONS = "locations";
public static final String COLUMN_ID ="_id";
public static final String COLUMN_LAT = "latitude";
public static final String COLUMN_LON = "longitude";
public DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db){
String CREATE_TABLE = "CREATE TABLE "+ TABLE_LOCATIONS +"("+
COLUMN_ID +" INTEGER PRIMARY KEY AUTOINCREMENT, "+
COLUMN_LAT +" REAL, "+
COLUMN_LON +" REAL)";
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
db.execSQL("DROP TABLE IF EXISTS "+ TABLE_LOCATIONS);
onCreate(db);
}
public void insertLocation(double latitude, double longitude){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_LAT, latitude);
values.put(COLUMN_LON, longitude);
db.insert(TABLE_LOCATIONS, null, values);
db.close();
}
public Cursor getAllLocations(){
SQLiteDatabase db = this.getReadableDatabase();
return db.query(TABLE_LOCATIONS, null, null, null, null, null, null);
}
}
Hello can you please fix my android studio api 2

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!