我遇到了问题,我无法理解为什么会这样......我是新手!
我希望每当我打开MainActivity2
时,我的最后一个OnItemLongClick
choisse(我的意思是marker
和moveCamera
,就是我最后一次选择)
然而
1)每次启动我的应用程序时,我必须再次从listview
中选择一项,然后按下模拟器上的后退按钮,然后再次打开MainActivity2
以实现它。
2)我的第二个错误是,当我选择OnItemLongClick
marker
项时,我的地图不会更改moveCamera
和Toast
。
为了使这项工作正常,我必须做些什么改变?
P.S。关于我上一次选择OnItemLongClick
的{{1}}可以正确显示,因此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));
}
}
}
答案 0 :(得分:0)
您需要将适配器数组保存到String []变量。然后使用favoritekafe从String []中查找先前所选选项的位置。然后在点击侦听器后添加以下行:
kafeteries.getChildAt(previousSelectedPosition).performClick();
或者您可以直接保存位置而不是名称并添加以上行:)