声明浮点值,获取浮点值

时间:2013-08-14 04:02:27

标签: java android string floating-point

我想问一下 如果我在尝试将lat和lng的代码更改为float时遇到问题,  任何人都可以用它来指导我。 以前,它是在字符串中,但现在我想将它decalre为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);

            }
        });



    }
}

2 个答案:

答案 0 :(得分:1)

由于您的所有代码都存在问题,因此很难识别代码中的所有问题,但这似乎很明显:

您需要更换

float lat=Float.parseFloat(KEY_LATITUDE);

float lat=Float.parseFloat((String) map.get(KEY_LATITUDE));

答案 1 :(得分: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));

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