当前位置:嗨网首页>书籍在线阅读

05-查找附近

  
选择背景色: 黄橙 洋红 淡粉 水蓝 草绿 白色 选择字体: 宋体 黑体 微软雅黑 楷体 选择字体大小: 恢复默认

20.2.2 查找附近

查找附近功能提供给用户搜索所在位置周围的餐厅、学校、银行。该功能通过PoiActivity.java文件实现,关键代码如下。

public class PoiActivity extends Activity{ String resultString; ListView resultListView; private ResultListAdapter resultListAdapter; ProgressDialog progressDialog; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.searchmain); progressDialog = new ProgressDialog(PoiActivity.this); progressDialog.setMessage("Loading..."); resultListView = (ListView) findViewById(R.id.resultList); adapterListener(resultListView); } public void toUniversity(View view) { progressDialog.show(); new GetMessageFromServer().execute("2000,university"); } public void toPark(View view) { progressDialog.show(); new GetMessageFromServer().execute("2000,park"); } public void toFood(View view) { progressDialog.show(); new GetMessageFromServer().execute("2000,food"); } public void toATM(View view) { progressDialog.show(); new GetMessageFromServer().execute("2000,atm"); } public void adapterListener(ListView listView) { listView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { try { String refranceString = Tools.formatJsonTOBean(resultString).get(position).getReference(); Intent intent = new Intent(PoiActivity.this,PlaceDetailActivity.class); intent.putExtra(Tools.PLACE_REFRANCE, refranceString); startActivity(intent); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } private class GetMessageFromServer extends AsyncTask<String, Void, String> { protected String doInBackground(String...parameters) { try { resultString = MapsHttpUtil.getGetRoundPlace(Tools.getLocation(get ApplicationContext()),parameters[0].split(",")[0],parameters[0].split(",")[1]); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return resultString; } protected void onPostExecute(String result) { // try { resultListAdapter = new ResultListAdapter(Tools.formatJsonTOBean (resultString),getApplicationContext()); resultListView.setAdapter(resultListAdapter); progressDialog.dismiss(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } super.onPostExecute(result); } } }