我目前正在创建一个谷歌地图应用程序,并试图在用户单击列表视图中的项目后刷新它。但是,由于 listviewadapter 位于代码的不同部分,这并没有按计划进行。我尝试了多种方法,包括将代码的某些部分转换为static
但这只会破坏谷歌地图代码。
这是地图所在的主要活动中的代码:
public class MainActivity extends ActionBarActivity {
GoogleMap googleMap;
// products JSONArray
JSONArray locations = null;
ListView townList;
ListViewAdapter townAdapter;
String[] townID;
String[] townName;
ArrayList<TownSelector> arraylist = new ArrayList<TownSelector>();
public static String currentLocationID;
public String getLocationID()
{
return this.currentLocationID;
}
public static String setLocationID(String l)
{
return currentLocationID = l;
}
public static boolean refreshMap = false;
public boolean getRefreshMap()
{
return this.refreshMap;
}
public static boolean setRefreshMap(Boolean m)
{
return refreshMap = m;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//setContentView(R.layout.search);
// Locate the ListView in listview_main.xml
townList = (ListView) findViewById(R.id.listview);
}
private void setUpMap() {
// enable MyLocation Layer of the Map
googleMap.setMyLocationEnabled(true);
// get LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Create a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Get the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Get current Location
Location myLocation = locationManager.getLastKnownLocation(provider);
//set map type
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// Get lattitude of the current location
double latitude = myLocation.getLatitude();
//get longitude of the current location
double longitude = myLocation.getLongitude();
//Create a LatLong object for the current location
LatLng latLng = new LatLng(latitude,longitude);
// show the current location in google map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(13));
//googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude,longitude)).title("Location").snippet("You're here"));
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllInfo extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading Bars. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> paramsLocations = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject jsonLocations = jParser.makeHttpRequest(url_all_locations, "GET", paramsLocations);
//Get all Locations
if(jsonLocations != null)
{
// Check your log cat for JSON reponse
Log.d("All Locations: ", jsonLocations.toString());
try {
// Checking for SUCCESS TAG
int success = jsonLocations.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Locations
locations = jsonLocations.getJSONArray(TAG_LOCATIONS);
// looping through All Offers
for (int i = 0; i < locations.length(); i++) {
JSONObject c = locations.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_LID);
String locationName = c.getString(TAG_LNAME);
// creating new HashMap
HashMap<String, String> locationsListMap = new HashMap<String, String>();
// adding each child node to HashMap key => value
locationsListMap.put(TAG_LID, id);
locationsListMap.put(TAG_LNAME, locationName);
// adding HashList to ArrayList
locationList.add(locationsListMap);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
}
}
}
下面是 ListViewAdapter 活动的代码:
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
MainActivity.setLocationID(locationlist.get(position).getID());
MainActivity.setRefreshMap(true);
}
});
return view;
}
有什么建议么?我很想添加一个更新功能,当用户单击列表项时,它将检查静态布尔值是否从 false 变为 true,但是我相信应该有比这更简单的方法
编辑:我setOnItemClickListener
在主要活动中添加了 并输入了一个 Log.d 字符串只是为了查看它是否有效,并且在点击列表中的四个城镇名称中的任何一个后我没有收到任何东西
townList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
Log.d("Hello","Why?");
}
});