私は、2つのチェックボックス(より正確には5個)を表示する警告ダイアログを持っています。チェックボックスフィルタ5は、マップ上に表示されるマーカーのリスト、時間間隔でグループ化されたマーカーです。私はonSaveInstanceStateにコードを書くべきであることがわからないという事実を除いて、すべてが機能するように管理していますので、チェックボックスの値は画面の回転後も保持されます。ここでは、私はそれが質問に関連していると思うコードの部分です。助けをあらかじめありがとう!onSaveInstanceStateでチェックボックス状態を保存する方法
public void filterTheMarkers(View view) {
if (dialog == null) {
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
@SuppressLint("InflateParams") View checkBoxView = inflater.inflate(R.layout.markers_filtering, null);
builder.setView(checkBoxView);
cbAllDay = (CheckBox) checkBoxView.findViewById(R.id.checkBox1);
cbBefore12 = (CheckBox) checkBoxView.findViewById(R.id.checkBox2);
cbBetween1216 = (CheckBox) checkBoxView.findViewById(R.id.checkBox3);
cbBetween1620 = (CheckBox) checkBoxView.findViewById(R.id.checkBox4);
ccbAfter20 = (CheckBox) checkBoxView.findViewById(R.id.checkBox5);
dialog = builder.create();
}
dialog.show();
}
public void displaySelectedMarkers(View view) {
dialog.dismiss();
Log.i("TAG", "All Day " + cbAllDay.isChecked() + " Before 12 " + cbBefore12.isChecked() + " Between 12-16 " + cbBetween1216.isChecked() + " Between 16-20" + cbBetween1620.isChecked() + " After 20 " + ccbAfter20.isChecked());
//according these check boxes status execute your code to show/hide markers
if (cbAllDay.isChecked() && cbBefore12.isChecked() && cbBetween1216.isChecked() && cbBetween1620.isChecked() && ccbAfter20.isChecked()) {
// show all markers
for (Marker marker : allDayList) {
marker.setVisible(true);
}
for (Marker marker : before12List) {
marker.setVisible(true);
}
for (Marker marker : between1216List) {
marker.setVisible(true);
}
for (Marker marker : between1620List) {
marker.setVisible(true);
}
for (Marker marker : after20List) {
marker.setVisible(true);
}
} else if (cbAllDay.isChecked() && !cbBefore12.isChecked() && !cbBetween1216.isChecked() && !cbBetween1620.isChecked() && !ccbAfter20.isChecked()) {
// show only All Day Markers
for (Marker marker : allDayList) {
marker.setVisible(true);
}
for (Marker marker : before12List) {
marker.setVisible(false);
}
for (Marker marker : between1216List) {
marker.setVisible(false);
}
for (Marker marker : between1620List) {
marker.setVisible(false);
}
for (Marker marker : after20List) {
marker.setVisible(false);
} // and it goes like this forever covering every possible interval
.
.
.
}
}
public void doNothing(View view) {
dialog.dismiss();
}
UPDATE
。 。 。
Boolean keepMarkerFiltersCB1;
Boolean keepMarkerFiltersCB2;
Boolean keepMarkerFiltersCB3;
Boolean keepMarkerFiltersCB4;
Boolean keepMarkerFiltersCB5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
if (savedInstanceState == null){
mapTypeSelected = GoogleMap.MAP_TYPE_NORMAL;
mSetCameraPosition = true;
mapFragment.setRetainInstance(true);
initialMarkers = true;
} else {
mapTypeSelected = savedInstanceState.getInt("the_map_type", GoogleMap.MAP_TYPE_NORMAL);
mSetCameraPosition = false;
initialMarkers = false;
keepMarkerFiltersCB1 = savedInstanceState.getBoolean("checkBox1");
keepMarkerFiltersCB2 = savedInstanceState.getBoolean("checkBox2");
keepMarkerFiltersCB3 = savedInstanceState.getBoolean("checkBox3");
keepMarkerFiltersCB4 = savedInstanceState.getBoolean("checkBox4");
keepMarkerFiltersCB5 = savedInstanceState.getBoolean("checkBox5");
}
public void filterTheMarkers(View view) {
if (dialog == null) {
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
@SuppressLint("InflateParams") View checkBoxView = inflater.inflate(R.layout.markers_filtering, null);
builder.setView(checkBoxView);
cbAllDay = (CheckBox) checkBoxView.findViewById(R.id.checkBox1);
cbBefore12 = (CheckBox) checkBoxView.findViewById(R.id.checkBox2);
cbBetween1216 = (CheckBox) checkBoxView.findViewById(R.id.checkBox3);
cbBetween1620 = (CheckBox) checkBoxView.findViewById(R.id.checkBox4);
ccbAfter20 = (CheckBox) checkBoxView.findViewById(R.id.checkBox5);
keepMarkerFiltersCB1 = cbAllDay.isChecked();
keepMarkerFiltersCB2 = cbBefore12.isChecked();
keepMarkerFiltersCB3 = cbBetween1216.isChecked();
keepMarkerFiltersCB4 = cbBetween1620.isChecked();
keepMarkerFiltersCB5 = ccbAfter20.isChecked();
dialog = builder.create();
}
dialog.show();
}
public void displaySelectedMarkers(View view) {
dialog.dismiss();
Log.i("TAG", "All Day " + cbAllDay.isChecked() + " Before 12 " + cbBefore12.isChecked() + " Between 12-16 " + cbBetween1216.isChecked() + " Between 16-20" + cbBetween1620.isChecked() + " After 20 " + ccbAfter20.isChecked());
//according these check boxes status execute your code to show/hide markers
if (cbAllDay.isChecked() && cbBefore12.isChecked() && cbBetween1216.isChecked() && cbBetween1620.isChecked() && ccbAfter20.isChecked()) {
// show all markers
for (Marker marker : allDayList) {
marker.setVisible(true);
}
for (Marker marker : before12List) {
marker.setVisible(true);
}
for (Marker marker : between1216List) {
marker.setVisible(true);
}
for (Marker marker : between1620List) {
marker.setVisible(true);
}
for (Marker marker : after20List) {
marker.setVisible(true);
}
} else if (cbAllDay.isChecked() && !cbBefore12.isChecked() && !cbBetween1216.isChecked() && !cbBetween1620.isChecked() && !ccbAfter20.isChecked()) {
// show only All Day Markers
for (Marker marker : allDayList) {
marker.setVisible(true);
}
for (Marker marker : before12List) {
marker.setVisible(false);
}
for (Marker marker : between1216List) {
marker.setVisible(false);
}
for (Marker marker : between1620List) {
marker.setVisible(false);
}
for (Marker marker : after20List) {
marker.setVisible(false);
}
} else if
.
.
.
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("the_map_type", mapTypeSelected);
outState.putBoolean("checkBox1", keepMarkerFiltersCB1);
outState.putBoolean("checkBox2", keepMarkerFiltersCB2);
outState.putBoolean("checkBox3", keepMarkerFiltersCB3);
outState.putBoolean("checkBox4", keepMarkerFiltersCB4);
outState.putBoolean("checkbox5", keepMarkerFiltersCB5);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
savedInstanceState.get("the_map_type");
savedInstanceState.get("checkBox1");
savedInstanceState.get("checkBox2");
savedInstanceState.get("checkBox3");
savedInstanceState.get("checkBox4");
savedInstanceState.get("checkBox5");
}
private void initialLocation(double lat, double lng, float zoom){
LatLng latLng = new LatLng(lat, lng);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(latLng, zoom);
mMap.moveCamera(update);
お手伝いすれば回答を受け入れてください。 – notsopopularguy
私は以前このアイデアを試してみましたが、残念ながらそれは機能しません。私はこのコードを追加していますが、同じことが回転すると、チェックボックスがリセットされ、前回のチェックを保持していないだけでなく、画面の回転後に動作していない(何もフィルタリングしていません)... –
onCreateメソッドのバンドルから読み取ってチェックボックスの値を取得しますか? – notsopopularguy