1

我有一个习惯Listview,在每个项目上都有一个图像按钮,它应该给我指示到建筑物的方向。按下方向按钮时,谷歌地图打开,但坐标填写错误。到目的地它只给我一个逗号。有人可以帮我找出我做错了什么吗?

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();
            }
        }
    }
}
4

1 回答 1

1

你的代码看起来不错。

您是否在以下位置设置了调试点:

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

我的猜测是 myLat 和 myLng 是空的,因此只留下一个“,”(逗号)。

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

myLat += new String(input);

myLng += new String(input);

这是曾经被击中吗?是抛出异常吗?

编辑

这周围:

// Filling the View

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

于 2013-06-10T03:42:57.137 回答