前提・実現したいこと
android studio でマップアプリを作っています。
アプリでは、ユーザーに位置情報とその場所に関する情報を登録してもらうため、firebaseのrealtime databaseを用いてデータ管理をしています。
firebaseに登録されているデータを取り出し、マップ画面に表示をしたいのですが、そのデータの取り出しがうまくいきません。
発生している問題・エラーメッセージ
com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String
該当のソースコード
java
<Post.java>
public class Post { public String name; public String title; public String detail; public String image; public double latitude; public double longitude; public String comment; public String othercomment; public String userId; public Post(){ } public String getName(){ return name; } public void setName(String name){ this.name=name; } public String getDetail(){ return detail; } public void setDetail(String detail){ this.detail=detail; } public String getTitle(){ return title; } public void setTitle(String title){ this.title=title; } public String getImage(){return image;} public void setImage(String image){ this.image=image; } public double getLatitude(){return latitude;} public void setLatitude(double latitude){ this.latitude=latitude; } public double getLongitude(){return longitude;} public void setLongitude(double longitude){ this.longitude=longitude; } public String getComment(){return comment;} public void setComment(String comment){ this.comment=comment; } /*public String getOtherComment(){return othercomment;} public void setOtherComment(String othercomment){ this.othercomment=othercomment; }*/ public String getuserId(){return userId;} public void setUserId(String userId){ this.userId=userId; } }
<MyMapActivity.java>
public class MyPostMapsActivity extends FragmentActivity implements OnMapReadyCallback { private GoogleMap mMap; private int i=1; private long maxId; private double platitude; private double plongitude; private DatabaseReference reff,mImageDataref; private TextView tvmapTitle,tvmapDetail,tvmapComment,tvInfoOther; private ImageView ivMapCamera; private String commentList[],otherList[]; private Marker markerList[]; private String imageList[]; Post post; String title; String detail; LatLng location; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_post_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); tvmapTitle=findViewById(R.id.tvmapTitle); tvmapDetail=findViewById(R.id.tvmapDetail); tvmapComment=findViewById(R.id.tvmapComment); tvInfoOther=findViewById(R.id.tvInfoOther2); ivMapCamera=findViewById(R.id.ivMapCamera); Intent intent=getIntent(); maxId=intent.getLongExtra("maxId",0); } public void setMarker(String title,String detail,LatLng location){ mMap.setInfoWindowAdapter(new CustomInfoAdapter()); MarkerOptions options=new MarkerOptions(); options.position(location); options.title(title); options.snippet(detail); Marker marker=mMap.addMarker(options); markerList[i-1]=marker; marker.showInfoWindow(); } private class CustomInfoAdapter implements GoogleMap.InfoWindowAdapter{ private final View mWindow; public CustomInfoAdapter(){ mWindow=getLayoutInflater().inflate(R.layout.info_window_layout,null); } @Override public View getInfoWindow(Marker marker){ render(marker,mWindow); return mWindow; } @Override public View getInfoContents(Marker marker){ return null; } private void render(Marker marker,View view){ //省略 } } /** * Manipulates the map once available. * This callback is triggered when the map is ready to be used. * This is where we can add markers or lines, add listeners or move the camera. In this case, * we just add a marker near Sydney, Australia. * If Google Play services is not installed on the device, the user will be prompted to install * it inside the SupportMapFragment. This method will only be triggered once the user has * installed Google Play services and returned to the app. */ @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; LatLng present=new LatLng(platitude,plongitude); LatLng test=new LatLng(35.68944,139.69167); // Add a marker in Sydney and move the camera //mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(test,7)); //データベース取得処理 reff=FirebaseDatabase.getInstance().getReference("Post"); reff.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) { for(DataSnapshot data: snapshot.getChildren()){ post=data.getValue(Post.class);//エラー箇所 title=post.getTitle(); detail=post.getDetail(); //String image=post.getImage(); double latitude=post.getLatitude(); double longitude=post.getLongitude(); location=new LatLng(latitude,longitude); String comment=post.getComment(); setMarker(title,detail,location); } } @Override public void onCancelled(@NonNull @NotNull DatabaseError error) { //データ取得失敗 Toast.makeText(MyPostMapsActivity.this,"データ取得に失敗しました",Toast.LENGTH_SHORT).show(); } }); //Storageから画像取得処理 mImageDataref =FirebaseDatabase.getInstance().getReference("uploads"); mImageDataref.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) { for(DataSnapshot postSnapshot : snapshot.getChildren()){ Upload upload=postSnapshot.getValue(Upload.class); //リストにuploadを入れる } } @Override public void onCancelled(@NonNull @NotNull DatabaseError error) { Toast.makeText(MyPostMapsActivity.this,"画像の取得に失敗しました",Toast.LENGTH_SHORT).show(); } }); } /*@Override public void onInfoWindowClick(Marker marker){ //投稿編集画面へ移動 }*/ }
試したこと
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー