-2

我想问一下,如果我在尝试将 lat 和 lng 的代码更改为 float 时遇到问题,谁能指导我。以前,它是字符串,但现在我想将它贴花为浮点数。我做了一些,但我不确定它是否正确完成。

 public class AndroidXMLParsingActivity extends ListActivity {
        static final String KEY = "weatherFilter";
        String URL = "";

        // XML node keys
        static final String KEY_EVENT = "event"; // parent node
        static final String KEY_TITLE = "title";
        static final String KEY_URL = "url";
        static final String KEY_DESC = "description";
        static final String KEY_START_TIME = "start_time";
        static final String KEY_STOP_TIME = "stop_time";
        static final String KEY_VENUE_NAME = "venue_name";
        static final String KEY_COUNTRY_NAME = "country_name";
        static final String KEY_VENUE_ADDRESS = "venue_address";
        static final String KEY_VENUE = "venue";
        static final float KEY_LATITUDE = "latitude";
        static final float KEY_LONGITUDE = "longitude";
            public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Intent in = getIntent();
        URL = in.getStringExtra(KEY);
        final ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_EVENT);
        // looping through all item nodes <item>
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            // adding each child node to HashMap key => value
            map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
            map.put(KEY_URL, parser.getValue(e, KEY_URL));
            map.put(KEY_DESC, "Description: " + parser.getValue(e, KEY_DESC));
            map.put(KEY_START_TIME, parser.getValue(e, KEY_START_TIME));
            map.put(KEY_STOP_TIME, parser.getValue(e, KEY_STOP_TIME));
            map.put(KEY_VENUE_NAME, parser.getValue(e, KEY_VENUE_NAME));
            map.put(KEY_COUNTRY_NAME, parser.getValue(e, KEY_COUNTRY_NAME));
            map.put(KEY_VENUE_ADDRESS, parser.getValue(e, KEY_VENUE_ADDRESS));

            float lat=Float.parseFloat(KEY_LATITUDE);
                   float lng=Float.parseFloat(KEY_LONGITUDE);
            map.put(KEY_VENUE, parser.getValue(e, KEY_VENUE_NAME) + ", " + parser.getValue(e, KEY_VENUE_ADDRESS));

            // adding HashList to ArrayList

            menuItems.add(map);


        }



        // Adding menuItems to ListView
        ListAdapter adapter = new SimpleAdapter(this, menuItems,
                R.layout.list_item, new String[] { KEY_TITLE, KEY_DESC,  KEY_COUNTRY_NAME,
                        KEY_VENUE , KEY_LATITUDE,KEY_LONGITUDE, KEY_START_TIME, }, new int[] {
                        R.id.title, R.id.description, R.id.countryName, R.id.venueName, R.id.lat,R.id.lng,
                        R.id.startTime });

        setListAdapter(adapter);

        // selecting single ListView item
        ListView lv = getListView();

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // getting values from selected ListItem
                String title = ((TextView) view.findViewById(R.id.title))
                        .getText().toString();
                String description = ((TextView) view.findViewById(R.id.description))
                        .getText().toString();
                String venue = ((TextView) view.findViewById(R.id.venueName))
                        .getText().toString();
                String lat = ((TextView) view.findViewById(R.id.lat))
                        .getText().toString();

                String lng = ((TextView) view.findViewById(R.id.lng  ))
                        .getText().toString();

                // Starting new intent
                Intent in = new Intent(getApplicationContext(),
                        SingleMenuItemActivity.class);
                in.putExtra(KEY_TITLE, title);
                in.putExtra(KEY_DESC, description);
                in.putExtra(KEY_VENUE, venue);
                in.putExtra(KEY_LATITUDE, lat);
                in.putExtra(KEY_LONGITUDE, lng);
                startActivity(in);

            }
        });



    }
}
4

2 回答 2

1

由于您将所有代码的问题都弄乱了,因此识别代码的所有问题并不容易,但这似乎很明显:

您将需要更换

float lat=Float.parseFloat(KEY_LATITUDE);

float lat=Float.parseFloat((String) map.get(KEY_LATITUDE));
于 2013-08-14T04:05:08.580 回答
0

你似乎很困惑。您需要将 XML 解析器中的值解析为浮点数。改变这个:

float lat=Float.parseFloat(KEY_LATITUDE);
float lng=Float.parseFloat(KEY_LONGITUDE);

至:

float lat=Float.parseFloat(parser.getValue(KEY_LATITUDE));
float lng=Float.parseFloat(parser.getValue(KEY_LONGITUDE));

否则,您只是将“经度”和“纬度”解析为显然行不通的浮点数。

于 2013-08-14T04:11:48.470 回答