应用中的地图使用叠加,部分取决于数据的日期,默认情况下,我希望它显示当前日期的数据,用户可以点按日期以显示日期选择器。到目前为止这是有效的,但由于我设法让日期选择器工作的唯一方法是在static class
内创建MainActivity
,它无法访问外部类中的任何内容,因此可以& #39; t使用getMapAsync
。一旦用户选择了日期,该应用如何使用OnMapReady
?
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, OnMapReadyCallback, OnDateSetListener{
static Calendar calendar = Calendar.getInstance();
static SimpleDateFormat sysFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
static String sysDate = sysFormat.format(calendar.getTime());
static DateFormat userFormat = (DateFormat.getDateInstance());
static String userDate = userFormat.format(calendar.getTime());
...
@Override
protected void onCreate(Bundle savedInstanceState) {
TextView displayDate = (TextView)findViewById(R.id.displayDate);
displayDate.setText(userDate);
displayDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment datePicker = new DatePickerFragment();
datePicker.show(getSupportFragmentManager(), "datePicker");
}
});
}
...
@Override
public void onMapReady(GoogleMap googleMap) {
//sysDate used in overlay link
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
//if possible I would like this to be called rather than the one below
}
public static class DatePickerFragment extends DialogFragment implements OnDateSetListener{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
calendar.set(Calendar.YEAR,year);
calendar.set(Calendar.MONTH,monthOfYear);
calendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);
userDate = userFormat.format(calendar.getTime());
sysDate = sysFormat.format(calendar.getTime());
}
}
}
答案 0 :(得分:0)
解决了,我觉得很傻。
制作了重新加载地图的方法
import java.util.Scanner;
import java.util.ArrayList;
public class Practice {
public static void main(String[] args) {
bannerPrinter();
getNum();
int num = 0;
ArrayList<String> products = productBuilder(num);
boolean productGuess = getOrder(products);
if (productGuess) {
double price = getPrice();
double tax = getTax(price);
double total = getTotal(tax, price);
printTotal(total);
}
else {
System.out.print("Product not found.");
}
}
public static void bannerPrinter() {
System.out.println();
System.out.println("******************************************");
System.out.println("****** Welcome to my eCommerce app! ******");
System.out.println("******************************************");
System.out.println();
}
public static int getNum() {
Scanner scnr = new Scanner(System.in);
int num = 0;
System.out.print("Enter the number of products: ");
num = scnr.nextInt();
return num;
}
public static ArrayList<String> productBuilder(int num) {
Scanner scnr = new Scanner(System.in);
String arrayNames = "";
ArrayList<String> products = new ArrayList();
for (int i = 0; i <= num; i++) {
System.out.print("Enter the products: ");
products.add(arrayNames);
}
return products;
}
public static boolean getOrder(ArrayList<String> products) {
Scanner scnr = new Scanner(System.in);
String guess = "";
boolean productName = products.contains(guess);
System.out.print("Enter a product: ");
guess = scnr.nextLine();
if(productName) {
System.out.println();
System.out.print("This product has been found.");
System.out.println();
}
else {
System.out.println();
}
return productName;
}
public static double getPrice() {
double price = 0.0;
price = (int)(Math.random() * 100);
return price;
}
public static double getTax(double price) {
double tax = 0.0;
tax = price * 0.10;
return tax;
}
public static double getTotal(double price, double tax) {
double saleTotal = 0.0;
saleTotal = price + tax;
return saleTotal;
}
public static void printTotal(double saleTotal) {
System.out.println("You total is $" + saleTotal + "0");
}
}
然后使用public void reloadMap(){
SupportMapFragment supportMapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
supportMapFragment.getMapAsync(this);
}
getActivity()