複数の日を入れれるように書き直しました。
大きさは今後直せますが、取り敢えず今い作ったコードでちゃんと動くのか試してください。
大きさは変わり次第報告します
This commit is contained in:
oKikuti 2024-07-08 15:32:50 +09:00
parent 5599ff7b1d
commit bc3c802e03
2 changed files with 45 additions and 20 deletions

View File

@ -32,8 +32,9 @@ import java.io.InputStreamReader;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Map;
public class Maps extends FragmentActivity implements OnMapReadyCallback, View.OnClickListener, GoogleMap.OnMarkerClickListener { public class Maps extends FragmentActivity implements OnMapReadyCallback, View.OnClickListener, GoogleMap.OnMarkerClickListener {
@ -41,7 +42,7 @@ public class Maps extends FragmentActivity implements OnMapReadyCallback, View.O
private GoogleMap mMap; private GoogleMap mMap;
private MapsBinding binding; private MapsBinding binding;
private LinearLayout locationsName; private LinearLayout locationsName;
private Spinner date; private Spinner dateSpinner;
private static final int[] COLORS = new int[]{ private static final int[] COLORS = new int[]{
Color.BLUE, Color.RED, Color.GREEN, Color.MAGENTA, Color.CYAN, Color.YELLOW Color.BLUE, Color.RED, Color.GREEN, Color.MAGENTA, Color.CYAN, Color.YELLOW
}; };
@ -50,6 +51,8 @@ public class Maps extends FragmentActivity implements OnMapReadyCallback, View.O
private List<String> nameList = new ArrayList<>(); private List<String> nameList = new ArrayList<>();
private List<Integer> colorList = new ArrayList<>(); private List<Integer> colorList = new ArrayList<>();
private Map<String, Runnable> dateMap = new HashMap<>();
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -66,24 +69,34 @@ public class Maps extends FragmentActivity implements OnMapReadyCallback, View.O
locationsName = findViewById(R.id.locationsName); locationsName = findViewById(R.id.locationsName);
// 日付を入れる処理
String dateData = "6月3日/6月4日/6月5日/6月6日/6月7日";
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item); ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter.add("6月3日"); String[] dates = dateData.split("/");
adapter.add("6月4日"); for (String date : dates) {
adapter.add("6月5日"); adapter.add(date);
date = findViewById(R.id.date); }
date.setAdapter(adapter);
date.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { // 各日付に対応するRunnableを設定する
dateMap.put("6月3日", this::firstMapAndNames);
dateMap.put("6月4日", this::secondMapAndNames);
dateMap.put("6月5日", this::thirdMapAndNames);
// 必要に応じて追加の日付に対応するメソッドをここに追加
dateMap.put("6月6日", this::additionalMapAndNames);
dateMap.put("6月7日", this::additionalMapAndNames2);
dateSpinner = findViewById(R.id.date);
dateSpinner.setAdapter(adapter);
dateSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override @Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedItem = (String) parent.getItemAtPosition(position); String selectedItem = (String) parent.getItemAtPosition(position);
if (selectedItem.equals("6月4日")) { Runnable mapLoader = dateMap.get(selectedItem);
secondMapAndNames(); if (mapLoader != null) {
} else if (selectedItem.equals("6月5日")) { mapLoader.run();
thildMapAndNames();
} else {
firstMapAndNames();
} }
} }
@ -98,7 +111,7 @@ public class Maps extends FragmentActivity implements OnMapReadyCallback, View.O
public void onMapReady(GoogleMap googleMap) { public void onMapReady(GoogleMap googleMap) {
mMap = googleMap; mMap = googleMap;
mMap.setOnMarkerClickListener(this); mMap.setOnMarkerClickListener(this);
firstMapAndNames(); firstMapAndNames(); // 最初に表示するデータセットをロード
} }
private void firstMapAndNames() { private void firstMapAndNames() {
@ -113,12 +126,25 @@ public class Maps extends FragmentActivity implements OnMapReadyCallback, View.O
loadMapAndNames(locationData2, nameData2); loadMapAndNames(locationData2, nameData2);
} }
private void thildMapAndNames() { private void thirdMapAndNames() {
String locationData3 = "35.09050879999539,136.87845379325216/35.091950716938875,136.8826598363985"; String locationData3 = "35.09050879999539,136.87845379325216/35.091950716938875,136.8826598363985";
String nameData3 = "名古屋港水族館/2番目"; String nameData3 = "名古屋港水族館/2番目";
loadMapAndNames(locationData3, nameData3); loadMapAndNames(locationData3, nameData3);
} }
// 追加の日付に対応するメソッド
private void additionalMapAndNames() {
String locationData4 = "35.09050879999539,136.87845379325216/35.091950716938875,136.8826598363985/35.09300000000000,136.88300000000000";
String nameData4 = "名古屋港水族館/2番目/3番目";
loadMapAndNames(locationData4, nameData4);
}
private void additionalMapAndNames2() {
String locationData5 = "35.09050879999539,136.87845379325216/35.091950716938875,136.8826598363985/35.09400000000000,136.88400000000000";
String nameData5 = "名古屋港水族館/2番目/3番目";
loadMapAndNames(locationData5, nameData5);
}
private void loadMapAndNames(String locationData, String nameData) { private void loadMapAndNames(String locationData, String nameData) {
try { try {
latLngList.clear(); latLngList.clear();

View File

@ -20,14 +20,13 @@
android:layout_width="40dp" android:layout_width="40dp"
android:layout_height="90dp" android:layout_height="90dp"
android:src="@drawable/back_button" android:src="@drawable/back_button"
android:layout_weight="4" /> android:layout_weight="3" />
<Spinner <Spinner
android:id="@+id/date" android:id="@+id/date"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="60dp" android:layout_height="90dp"
android:layout_weight="5" android:layout_weight="7" />
android:textSize="50dp" />
</LinearLayout> </LinearLayout>
<fragment <fragment