Search code examples
androidgoogle-mapsandroid-jsonandroid-bundle

JSON data search and bundle in Android


Sorry for my bad English.I am new android.

I have some problem for JSON in Android that

(1.)I've been to obtain information from the Internet (JSON). How can I mark all station on googlemap on particular page?

(2.1) I've been to obtain information from the Internet(JSON) and show those information in the listView. This listView is composed by two TextView(txtV1, txtV2). How can I Use the keyword_search to filter the ListView by the editText?

(2.2) If I click the option, the relevant information will be transmitted to the next page and display. Now, I can only bunndle the String data. How can I bundle the (lat,lng) and mark on google map?

This is json :

{
  "retCode": "1",
  "retVal": [
    {
      "iid": "339",
      "sno": "0001",
      "mday": "20140711003603",
      "lat": "25.0408578889",
      "lng": "121.567904444"
    },
    {
      "iid": "340",
      "sno": "0002",
      "mday": "20140711003508",
      "lat": "25.041",
      "lng": "121.556945"
    }
  ]
}

This is problem(2.1) code:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.station);
        oslist = new ArrayList<HashMap<String, String>>();
        new JSONParse().execute();
    }

    private class JSONParse extends AsyncTask<String, String, JSONObject> {

        private ProgressDialog pDialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            sna = (TextView) findViewById(R.id.sna);
            ar = (TextView) findViewById(R.id.ar);
        }

        @Override
        protected JSONObject doInBackground(String... args) {

            JSONParser jParser = new JSONParser();

            // Getting JSON from URL
            JSONObject json = jParser.getJSONFromUrl(url);
            return json;
        }

        @Override
        protected void onPostExecute(JSONObject json) {
            pDialog.dismiss();
            try {

                // Getting JSON Array from URL
                youbike = json.getJSONArray(TAG_retVal);
                for (int i = 0; i < youbike.length(); i++) {
                    JSONObject c = youbike.getJSONObject(i);

                    // Storing JSON item in a Variable
                    final String iid = c.getString(TAG_iid);
                                        final String sno = c.getString(TAG_sno);
                    final String mday = c.getString(TAG_mday);
                    final String lat = c.getString(TAG_lat);
                    final String lng = c.getString(TAG_lng);

                    // Adding value HashMap key => value

                    final HashMap<String, String> map = new HashMap<String, String>();
                    map.clear();
                    map.put(TAG_iid, iid);
                                        map.put(TAG_sno, sno);
                    map.put(TAG_mday, mday);
                    map.put(TAG_lat, lat);
                    map.put(TAG_lng, lng);

                    oslist.add(map);
                    list = (ListView) findViewById(R.id.list);

                    ListAdapter adapter = new SimpleAdapter(station.this,
                            oslist, R.layout.station_list, new String[] {
                                    TAG_iid, TAG_sno }, new int[] { R.id.sno,
                                    R.id.iid });
                    list.setAdapter(adapter);

                    // bundle to next page

                    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> parent,
                                View view, int position, long id) {

                            Intent intent = new Intent();
                            intent.setClass(station.this, station_1.class);

                            Bundle b = new Bundle();

                            b.putString("TAG_iid",
                                    oslist.get(+position).get("iid"));
                            b.putString("TAG_sno",
                                    oslist.get(+position).get("sno"));
                            b.putString("TAG_mday",
                                    oslist.get(+position).get("mday"));
                            b.putString("TAG_lat",
                                    oslist.get(+position).get("lat"));
                            b.putString("TAG_lng",
                                    oslist.get(+position).get("lng"));
                            intent.putExtras(b);
                            startActivity(intent);

                        }
                    });
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

Solution

  • To filter your results, take a look at SimpleAdapter#getFilter(). Example:

    private void filterResults(CharSequence filterText){
        Filter filter = mSimpleAdapter.getFilter();
        filter.filter(filterText);
    }
    ...
    mSearchBox.addTextChangeListener(new TextWatcher(){
        ...
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                filterResults(s);
            }
    });
    

    To pass lat/long trough Bundle, either parse them when taking from JSON by calling JSONObject#getLong or parse them in other Activity with Long#parseLong