Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

java - Declaring float value, get float value

I would like to ask If I've problem trying to change my code for lat and lng into float, can anyone guide me with it. previously, it is in string, but now i would like to decalre it as float. I've done some, but I not sure if it is correctly done.

 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);

            }
        });



    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since you've cluttered your problem with all of your code it's not easy to identify all of the problems with your code, however this seems obvious:

You will need to replace

float lat=Float.parseFloat(KEY_LATITUDE);

with

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...