2014-09-27 34 views
0

我目前正在创建一个谷歌地图应用程序,我试图让它在用户点击一个列表视图中的项目后进行刷新。但是,当listviewadapter处于代码的不同部分时,这不会按计划进行。我已经尝试了多种方法,包括将代码的某些部分转换为static,但这只会打破谷歌地图代码。从ListviewAdapter刷新Android上的GoogleMap

这里是我的主要活动代码,其中地图是:

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; 
} 

有什么建议?我试图添加一个更新功能,它将检查静态布尔值,当用户点击一个列表项时,它会检查一个静态布尔值,不过我相信应该有一个比这更简单的方法。在主要活动增加了setOnItemClickListener,并已进入了一个Log.d字符串只是为了看看它的工作和我击中任何四点明山镇的名字我已经在列表中

townList.setOnItemClickListener(new OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) { 
      Log.d("Hello","Why?");    
     } 
    }); 
+0

你可以用布尔标志检查。 – Piyush 2014-09-27 05:58:02

+0

我该怎么做?在lisviewadapter的OnClick函数中? – TaurusDroid 2014-09-27 06:02:39

+0

在地图活动中取一个公共静态布尔变量,并在刷新地图时将其设置为true。当你点击lisviewadapter中的视图时,检查它是否为真,然后刷新你的地图。 – Piyush 2014-09-27 06:04:29

回答

0

我以后不会收到任何东西谢谢Chitrang上面的评论。首先我说:

townList.setOnItemClickListener(new OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) { 
     Log.d("Hello","Why?");    
    } 
}); 

然后当他提到我需要删除一些重点,我意识到我没有删除以前setOnClickListener我在listviewadapter代码已经离开了适配器。