当按下ListViewItem时,谷歌地图的坐标错误



我有一个自定义的Listview,在每个项目上都有一个imagebutton,应该给我一个建筑物的方向。当按下方向按钮,谷歌地图打开,但坐标填写错误。对于目的地,它只给我一个逗号。有人能帮我查一下我哪里做错了吗?

public class buildingList extends Activity {
    String myLat = "";
    String myLng = "";
    private List<buildingObject> buildingItem = new ArrayList<buildingObject>();
    ViewHolder holder = new ViewHolder();
    ListView list;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getLocations();
        setContentView(R.layout.building_list);
        new GetbuildingsTask().execute(myLat, myLng);
    }
    private void populateListview() {
        ArrayAdapter<buildingObject> adapter = new MyListAdapter();
        list = (ListView) findViewById(R.id.buildingList);
        list.setAdapter(adapter);
    }
    private class MyListAdapter extends ArrayAdapter<buildingObject> {
        public MyListAdapter() {
            super(buildingList.this, R.layout.building_row, buildingItem);
        }
        @Override
        public View getView(final int position, View convertView, final ViewGroup parent) {
            //Null possible, so checking
            View itemView = convertView;
            if (itemView == null) {
                itemView = getLayoutInflater().inflate(R.layout.building_row, parent, false);
            }
            // Find the item to work with
            buildingObject currentItem = buildingItem.get(position);
            // Filling the View         
            holder.thumbnail = (ImageButton) itemView.findViewById(R.id.btnbuildingListThumbnail);
            holder.thumbnail.setBackgroundResource(currentItem.getThumbnail());
            holder.name = (TextView) itemView.findViewById(R.id.lblbuildingListItemName);
            holder.name.setText(currentItem.getName());
            holder.btnDirections = (ImageButton) itemView.findViewById(R.id.btnbuildingListDirections);
            holder.thumbnail = (ImageButton) itemView.findViewById(R.id.btnbuildingListThumbnail);
            holder.btnShare = (ImageButton) itemView.findViewById(R.id.btnbuildingListShare);
            holder.btnDirections.setOnClickListener(new OnClickListener() {             
                @Override
                public void onClick(View v) {               
                    TextView txtLat = (TextView) parent.getChildAt(position - list.getFirstVisiblePosition()).findViewById(R.id.lblbuildingListlatitude);
                    TextView txtLng = (TextView) parent.getChildAt(position - list.getFirstVisiblePosition()).findViewById(R.id.lblbuildingListlongitude);
                    String lat = txtLat.getText().toString();
                    String lng = txtLng.getText().toString();
                    Log.v("Lat ", "Lat of selected building is " + lat);
                    Log.v("Lng ", "Lng of selected building is " + lng);
                    Uri uri = Uri.parse("http://maps.google.com/maps?&saddr=" + myLat + "," + myLng + "&daddr=" + lat + "," + lng);
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);  
                }
            });
            holder.thumbnail.setOnClickListener(new OnClickListener(){
                public void onClick(View v) {                       
                    String btnmsg = "ThumbNail";
                    Toast.makeText(buildingList.this, btnmsg, Toast.LENGTH_LONG).show();                        
                }
            });
            holder.btnShare.setOnClickListener(new OnClickListener() {              
                @Override
                public void onClick(View v) {               
                    String btnmsg = "Share";
                    Toast.makeText(buildingList.this, btnmsg, Toast.LENGTH_LONG).show();                        
                }
            });
            return itemView;
        }
    }
    static class ViewHolder {
        ImageButton thumbnail;
        TextView name;
        ImageButton btnDirections;
        ImageButton btnShare;
    }
    private void getLocations() {
        String[] locations = getApplicationContext().fileList();
        FileInputStream fis;
        for (int i = 0; i < locations.length; i++) {
            if (locations[i].equals("my_latitude")) {
                try {
                    fis = openFileInput(locations[i]);
                    byte[] input = new byte[fis.available()];
                    while (fis.read(input) != -1) {
                        myLat += new String(input);
                    }
                    fis.close();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (locations[i].equals("my_longitude")) {
                try {
                    fis = openFileInput(locations[i]);
                    byte[] input = new byte[fis.available()];
                    while (fis.read(input) != -1) {
                        myLng += new String(input);
                    }
                    fis.close();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    class GetbuildingsTask extends AsyncTask<String, Void, JSONArray> {
        @Override
        protected JSONArray doInBackground(String... data) {
            String lat = data[0];
            String lng = data[1];
            JSONParser jParser = new JSONParser();
            JSONArray json = jParser
                    .getJSONFromUrl("http://www.mysite.eu/index.php/building/searchbuildingsJSON?lat=" + lat + "&lng=" + lng + "&radius=10");
            return json;
        }
        protected void onPostExecute(JSONArray result) {
            if (result != null) {
                try {
                    Log.v(result.getString(0), result.getString(0));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Toast toast = Toast.makeText(getApplicationContext(), "Done", Toast.LENGTH_SHORT);
                toast.show();
                Log.d("whut", "" + result.length());
                String[] buildings = new String[result.length()];
                buildingObject[] building = new buildingObject[result.length()];
                try {
                    for (int i = 0; i < result.length(); i++) {
                        JSONObject row;
                        row = result.getJSONObject(i);
                        buildings[i] = row.getString("Name");
                        buildings[i] += "" + row.getDouble("Latitude");
                        buildings[i] += "" + row.getDouble("Longitude");
                        buildings[i] += ". Distance: " + row.getDouble("distance") + "km";

                        String Name = row.getString("Name");
                        float lat = (float) row.getDouble("Latitude");
                        float lng = (float) row.getDouble("Longitude");
                        float dist = (float) row.getDouble("distance");
                        //building[i] = new buildingObject(Name, lat, lng, dist);
                        buildingItem.add(new buildingObject(Name,lat, lng, dist));
                    }
                } catch (JSONException e) {
                    Log.v("Noooooooooooo", "You were the chosen one");
                    Log.e("Noooooooooooo", "Dis iz die erreur: " + e.toString());
                }
                populateListview();
            } else {
                Toast toast = Toast.makeText(getApplicationContext(),
                        "Please enable internet", Toast.LENGTH_LONG);
                toast.show();
            }
        }
    }
}

您的代码看起来不错。

是否设置了一个调试点:

Uri uri = Uri.parse("http://maps.google.com/maps?&saddr=" + myLat + "," + myLng + "&daddr=" + lat + "," + lng)

我猜mylar和myLng是空的,因此只留下一个","(逗号)。

如果它实际上为空或null,那么我将在下一个位置放置调试点在(或之前):

myLat += new String(input);

myLng += new String(input);

这个被击中了吗?是否会抛出异常?

编辑

这里的

:

// Filling the View

你在哪里填充lblbuildingListlatitude和长文本视图?

最新更新