从Listview适配器刷新Android上的Google Map

时间:2014-09-27 05:48:22

标签: android google-maps refresh reload listview-adapter

我目前正在创建一个谷歌地图应用,我试图在用户点击列表视图中的项目后刷新它。但是,如果listviewadapter位于代码的不同部分,则不按计划进行。我尝试了多种方法,包括将代码的某些部分转换为static,但这只会破坏Google地图代码。

以下是我的主要活动中代码的代码:

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?");            
       } 
    });

1 个答案:

答案 0 :(得分:0)

感谢Chitrang上面的评论,我得到了解决方案。首先我补充道:

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

然后当他提到我需要删除适配器中的一些焦点时,我意识到我没有删除我在listviewadapter代码中留下的setOnClickListener