我有一個問題,我找不到爲什麼發生這種情況......我是新手!活動生命週期不起作用
我想每一次當我打開我MainActivity2
出現在谷歌地圖我的最後OnItemLongClick
choisse(我指的是和moveCamera
,我最後choisse的)
然而
1)每次我啓動我的應用程序,我不得不再次從listview
中選擇一個項目,然後按下模擬器上的後退按鈕,然後再次打開MainActivity2
以實現此目的。
2)和我的第二個錯誤是當我選擇一個項目OnItemLongClick
我的地圖不改變和moveCamera
。
我必須做些什麼才能使這項工作正確?
p.s.我Toast
我的最後choisse OnItemLongClick
可以正確地顯示,所以favoritekafe
它正確地存儲在Preferences
(對不起,我的英語不是我的母語)
我MainActivity2
是:
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.content.SharedPreferences;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.StreetViewPanoramaCamera;
public class MainActivity2 extends FragmentActivity implements OnMapReadyCallback {
ListView kafeteries;
String favoritekafe;
GoogleMap mMap;
String i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.coffee);
// 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);
i = getSharedPreferences("pref1", MODE_PRIVATE).getString("data1", null);
//emfanisi toast gia to poio einai to agapimeno */
favoritekafe = getSharedPreferences("pref", MODE_PRIVATE).getString("data", null);
if (favoritekafe==null) {
Toast.makeText(getApplicationContext(), "you don't have a favorite caffe yet", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), favoritekafe,
Toast.LENGTH_SHORT).show();
}
/* oi kafeteries se lista */
kafeteries = (ListView) findViewById(R.id.kafeteries);
ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(MainActivity2.this,
android.R.layout.simple_list_item_1,
getResources().getStringArray(R.array.kafeteries_syros));
kafeteries.setAdapter(mAdapter);
// mnmta TOAST otan kanw click kapoio stixeio tis listas */
kafeteries.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String sMessage = "";
switch(position) {
case 0: sMessage = "Coffee and drinks with hospitable locals\nArea:Ermoupoli\nPhone:2281083354"; break;
case 1: sMessage = "Coffee in the narrow streets of the city\nArea:Ermoupoli\nPhone:2281079225"; break;
case 2: sMessage = "The smallest and most adorable coffee in town\nArea:Ermoupoli\nPhone:2281300880"; break;
case 3: sMessage = "Coffee and snacks at the city's harbor\nArea:Ermoupoli\nPhone:2281076144"; break;
case 4: sMessage = "The city's most famous café\nArea:Ermoupoli\nPhone:2281085337"; break;
}
Toast.makeText(getApplicationContext(), sMessage, Toast.LENGTH_LONG).show();
}
});
// long clik add sta favorite kai save*/
kafeteries.setLongClickable(true);
kafeteries.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int position, long id) {
String fMessage = "";
switch(position) {
case 0: fMessage = "Boheme del mar is your favorite caffe"; i="1"; break;
case 1: fMessage = "Jar is favorite caffe"; i="2"; break;
case 2: fMessage = "Kouchico is your favorite caffe"; i="3"; break;
case 3: fMessage = "Okio is your favorite caffe"; i="4"; break;
case 4: fMessage = "Plaza is your favorite caffe"; i="5"; break;
}
//save i */
SharedPreferences.Editor editor2 = getSharedPreferences("pref1", MODE_PRIVATE).edit();
editor2.putString("data1", i);
editor2.commit();
gotStringFromLongClick(fMessage);
// save fmessage */
SharedPreferences.Editor editor = getSharedPreferences("pref", MODE_PRIVATE).edit();
editor.putString("data", fMessage);
editor.commit();
Toast.makeText(getApplicationContext(), fMessage, Toast.LENGTH_SHORT).show();
return true;
}
void gotStringFromLongClick(String fMessage){
favoritekafe=fMessage;
}
});
}
// markers...
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setTrafficEnabled(true);
mMap.setIndoorEnabled(true);
mMap.setBuildingsEnabled(true);
mMap.getUiSettings().setZoomControlsEnabled(true);
// Add a markers
LatLng Boheme = new LatLng(37.442801, 24.944866);
LatLng Jar = new LatLng(37.444241, 24.943962);
LatLng Kouchico = new LatLng(37.444705, 24.944009);
LatLng Okio = new LatLng(37.443111, 24.944542);
LatLng Plaza = new LatLng(37.443517, 24.943775);
LatLng ermoupoli = new LatLng(37.443245, 24.943385);
// add marker estiasi
if (i=="1") {
mMap.addMarker(new MarkerOptions().position(Boheme).title("Boheme del Mar"));
}
else if (i=="2") {
mMap.addMarker(new MarkerOptions().position(Jar).title("Jar"));
}
else if (i=="3") {
mMap.addMarker(new MarkerOptions().position(Kouchico).title("Kouchiko"));
}
else if (i=="4") {
mMap.addMarker(new MarkerOptions().position(Okio).title("Okio"));
}
else if (i=="5") {
mMap.addMarker(new MarkerOptions().position(Plaza).title("Plaza"));
}
//estiasi marker
if (favoritekafe== "Boheme del mar is your favorite caffe"){
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Boheme,19));
}
else if (favoritekafe== "Jar is favorite caffe") {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Jar, 19));
}
else if (favoritekafe== "Kouchico is your favorite caffe") {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Kouchico, 19));
}
else if (favoritekafe== "Okio is your favorite caffe") {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Okio, 19));
}
else if (favoritekafe== "Plaza is your favorite caffe") {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Plaza, 19));
}
else {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(ermoupoli,16));
}
}
}