问题描述
我试图将所有标记从sqlite数据库复制到地图上。到目前为止,我已经使数据库能够在recyclerview的第二个活动中显示坐标。现在,我需要获取坐标并将其显示在地图上。我尝试了不同的解决方案,但是由于我发现的解决方案基本上是大约6年前的stackoverflow文章,但是这些文章都没有用,所以我没有设法解决这个问题。任何帮助都将不胜感激。
这是我的活动:
Mapsactivity.java
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
FloatingActionButton location_button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
//Button to navigate to second activity displaying recyclerview with locations
location_button = findViewById(R.id.location_button);
location_button.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent( MapsActivity.this,LocationsList.class);
startActivity(intent);
}
});
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34,151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
第二个活动在recyclerview中具有位置。
LocationsList.java
RecyclerView recyclerView;
MyDatabaseHelper myDatabaseHelper;
ArrayList<String> location_id,location_title,location_latitude,location_longitude;
CustomAdapter customAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_locations_list);
recyclerView = findViewById(R.id.recyclerView);
myDatabaseHelper = new MyDatabaseHelper(LocationsList.this);
location_id = new ArrayList<>();
location_title = new ArrayList<>();
location_latitude = new ArrayList<>();
location_longitude= new ArrayList<>();
storeDataInArrays();
customAdapter = new CustomAdapter(LocationsList.this,location_id,location_longitude);
recyclerView.setAdapter(customAdapter);
recyclerView.setLayoutManager(new linearlayoutmanager(LocationsList.this));
}
void storeDataInArrays(){
Cursor cursor = myDatabaseHelper.readAllData();
if(cursor.getCount() == 0) {
Toast.makeText(this,"No data",Toast.LENGTH_SHORT).show();
}else {
while (cursor.movetoNext()) {
location_id.add(cursor.getString(0));
location_title.add(cursor.getString(1));
location_latitude.add(cursor.getString(2));
location_longitude.add(cursor.getString(3));
}
}
}
}
MyDatabaseHelper.java
private Context context;
private static final String DATABASE_NAME ="Locations.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "my_locations";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_TITLE = "location_title";
private static final String COLUMN_LATITUDE = "location_latitude";
private static final String COLUMN_LONGITUDE = "location_longitude";
public MyDatabaseHelper(@Nullable Context context) {
super(context,DATABASE_NAME,null,DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(sqliteDatabase sqliteDatabase) {
String query = "CREATE TABLE " + TABLE_NAME +
" (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
COLUMN_TITLE + " TEXT," +
COLUMN_LATITUDE + " TEXT," +
COLUMN_LONGITUDE + " TEXT) ;";
sqliteDatabase.execsql(query);
}
@Override
public void onUpgrade(sqliteDatabase sqliteDatabase,int i,int i1) {
sqliteDatabase.execsql("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(sqliteDatabase);
}
Cursor readAllData() {
String query = "SELECT * FROM " + TABLE_NAME;
sqliteDatabase sqliteDatabase = this.getReadableDatabase();
Cursor cursor = null;
if(sqliteDatabase != null) {
cursor = sqliteDatabase.rawQuery(query,null);
}
return cursor;
}
}
然后使用适配器显示坐标。 CustomAdapter.java
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
private Context context;
private ArrayList location_id,location_longitude;
CustomAdapter(Context context,ArrayList location_id,ArrayList location_title,ArrayList location_latitude,ArrayList location_longitude){
this.context= context;
this.location_id= location_id;
this.location_title = location_title;
this.location_latitude = location_latitude;
this.location_longitude = location_longitude;
}
@NonNull
@Override
public CustomAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.my_row,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull CustomAdapter.MyViewHolder holder,int position) {
holder.location_id_txt.setText(String.valueOf(location_id.get(position)));
holder.location_title_txt.setText(String.valueOf(location_title.get(position)));
holder.location_latitude_txt.setText(String.valueOf(location_latitude.get(position)));
holder.location_longitude_txt.setText(String.valueOf(location_longitude.get(position)));
}
@Override
public int getItemCount() {
return location_id.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView location_id_txt,location_title_txt,location_latitude_txt,location_longitude_txt;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
location_id_txt = itemView.findViewById(R.id.location_id_txt);
location_title_txt = itemView.findViewById(R.id.location_title_txt);
location_latitude_txt = itemView.findViewById(R.id.location_latitude_txt);
location_longitude_txt = itemView.findViewById(R.id.location_longitude_txt);
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)