0

我正在尝试将 Google Maps View 和 ListView 放入一项活动中。MapsView 占上侧的 2/3,列表占下侧的 1/3(纵向)。

以这种方式假设它是肖像: 在此处输入图像描述

MapsView 和 ListView 都将收到相同的 JSON 信息,它们将获得 onCreate()。我已经能够在屏幕的 2/3 上放置 MapsView,但 ListView 不会接收任何数据。有人可以告诉我,当超类不是时如何正确处理 ListView ListActivity

这是我的代码:

// url to make request
    private static String url = ""; //here pass the url

 // JSON Node names
    private static final String TAG_LOCATION = "location";
    private static final String TAG_LOCATION1= "location";
    private static final String TAG_LOCATION_ID = "LocationID";
    private static final String TAG_NAME = "Name";
    private static final String TAG_PHONE = "Phone";
    private static final String TAG_FORMATTED_PHONE = "FormattedPhone";
    private static final String TAG_ADDRESS = "Address";
    private static final String TAG_CROSS_STREET = "CrossStreet";
    private static final String TAG_LAT = "Lat";
    private static final String TAG_LNG = "Lng";
    private static final String TAG_DISTANCE= "Distance";
    private static final String TAG_POSTAL_CODE = "PostalCode";
    private static final String TAG_CITY = "City";
    private static final String TAG_STATE = "State";
    private static final String TAG_COUNTRY= "Country";

 // contacts JSONArray
    JSONArray location = null;
    JSONObject location1=null;

    ListView locationList;
    private MapView mapView;
    LocationManager lm;
    LocationListener locationListener;
    MapController mapController;

    private ListAdapter adapter;

    private static final double lat = 11.9333;
    private static final double lng = 108.417;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_map);

        locationList=( ListView)findViewById(R.id.list_Surroundings);
        locationList.setAdapter(adapter);


        // Hashmap for ListView
        ArrayList<HashMap<String, String>> locationList = new ArrayList<HashMap<String, String>>();

     // Creating JSON Parser instance
        JSONParser jParser = new JSONParser();

     // getting JSON string from URL
        JSONObject json = jParser.getJSONFromUrl(url);

        try {
         // Getting Array of Category
        location=json.getJSONArray(TAG_LOCATION);

        //looping through all categories
        for(int i = 0; i < location.length(); i++){
            JSONObject c = location.getJSONObject(i);
            JSONObject c1=c.getJSONObject(TAG_LOCATION1);

            // Storing each json item in variable
                String LocationID = c1.getString(TAG_LOCATION_ID);
                String Name = c1.getString(TAG_NAME);
                String Phone = c1.getString(TAG_PHONE);
                String FormattedPhone = c1.getString(TAG_FORMATTED_PHONE);
                String Address = c1.getString(TAG_ADDRESS);
                String CrossStreet = c1.getString(TAG_CROSS_STREET);
                String Lat = c1.getString(TAG_LAT);
                String Lng = c1.getString(TAG_LNG);
                String Distance = c1.getString(TAG_DISTANCE);
                String PostalCode = c1.getString(TAG_POSTAL_CODE);
                String City = c1.getString(TAG_CITY);
                String State = c1.getString(TAG_STATE);
                String Country = c1.getString(TAG_COUNTRY);

            // creating new HashMap
               HashMap<String, String> map = new HashMap<String, String>();

            // adding each child node to HashMap key => value
               map.put(TAG_LOCATION_ID, LocationID);
               map.put(TAG_NAME, Name);
               map.put(TAG_PHONE, Phone);
               map.put(TAG_FORMATTED_PHONE, FormattedPhone);
               map.put(TAG_ADDRESS, Address);
               map.put(TAG_CROSS_STREET, CrossStreet);
               map.put(TAG_LAT, Lat);
               map.put(TAG_LNG, Lng);
               map.put(TAG_DISTANCE, Distance); 
               map.put(TAG_POSTAL_CODE, PostalCode);
               map.put(TAG_CITY, City);
               map.put(TAG_STATE, State);
               map.put(TAG_COUNTRY, Country);

               // adding HashList to ArrayList
               locationList.add(map);
        }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        /**
         * Updating parsed JSON data into ListView
         * */

        adapter = new SimpleAdapter(this, locationList,
                R.layout.list_item,
                new String[] { TAG_NAME }, new int[] {
                        R.id.name});


        mapView = (MapView) findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);

        List mapOverlays = mapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(
                R.drawable.map_pin_red);
        CustomItemizedOverlay itemizedOverlay = new CustomItemizedOverlay(
                drawable, this);

        GeoPoint point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
        OverlayItem overlayitem = new OverlayItem(point, "Hello",
                "I'm in Athens, Greece!");

        itemizedOverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedOverlay);

        mapController = mapView.getController();

        mapController.animateTo(point);
        mapController.setZoom(10);

        lm=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        locationListener= new MylocationListener();
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    }

    public class MylocationListener implements LocationListener{


        @Override
        public void onLocationChanged(Location loc) {
            // TODO Auto-generated method stub
            if(loc!=null){
                Toast.makeText(getBaseContext(), "Location Changed : Lat:" +loc.getLatitude() + "Lng: " + loc.getLongitude(), Toast.LENGTH_SHORT).show();
            }
            GeoPoint point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
            mapController.animateTo(point);
            mapController.setZoom(10);
        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

}
4

1 回答 1

0

您必须adapter在将其设置为ListView. 此外,您有两个名为 的变量LocationList,这通常是不好的做法。尝试:

public void onCreate(Bundle savedInstanceState) {
     // Hashmap for ListView
    ArrayList<HashMap<String, String>> listOfLocations = new ArrayList<HashMap<String, String>>();

    ... // existing code here

    adapter = new SimpleAdapter(this, listOfLocations,
        R.layout.list_item,
        new String[] { TAG_NAME }, new int[] {
               R.id.name});
    locationList.setAdapter(adapter);    
}
于 2013-04-04T05:57:19.527 回答