MainActivity.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. package com.baidu.lbs.duanzu;
  2. import java.io.UnsupportedEncodingException;
  3. import java.net.URLEncoder;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import org.json.JSONArray;
  7. import org.json.JSONException;
  8. import org.json.JSONObject;
  9. import android.app.AlertDialog;
  10. import android.app.TabActivity;
  11. import android.content.Context;
  12. import android.content.DialogInterface;
  13. import android.content.Intent;
  14. import android.content.res.Resources.NotFoundException;
  15. import android.location.Location;
  16. import android.os.Bundle;
  17. import android.os.Handler;
  18. import android.os.Message;
  19. import android.view.KeyEvent;
  20. import android.view.View;
  21. import android.widget.AdapterView;
  22. import android.widget.AdapterView.OnItemSelectedListener;
  23. import android.widget.ArrayAdapter;
  24. import android.widget.HorizontalScrollView;
  25. import android.widget.RadioButton;
  26. import android.widget.RadioGroup;
  27. import android.widget.RelativeLayout;
  28. import android.widget.Spinner;
  29. import android.widget.TabHost;
  30. import android.widget.TabWidget;
  31. import android.widget.TextView;
  32. import android.widget.Toast;
  33. import android.widget.ToggleButton;
  34. import com.baidu.lbs.duanzu.LBSListActivity.ContentAdapter;
  35. import com.baidu.lbs.duanzu.bdapi.LBSCloudSearch;
  36. import com.baidu.lbs.duanzu.bdapi.LBSMapActivity;
  37. /**
  38. * 小猪短租首页tab类
  39. *
  40. * @author Lu.Jian
  41. *
  42. */
  43. public class MainActivity extends TabActivity {
  44. private Context context;
  45. public static final int MSG_NET_TIMEOUT = 100;
  46. public static final int MSG_NET_STATUS_ERROR = 200;
  47. public static final int MSG_NET_SUCC = 1;
  48. private boolean initSearchFlag = false;
  49. private RelativeLayout progress;
  50. /*
  51. * 处理网络请求
  52. */
  53. private final Handler mHandler = new Handler() {
  54. @Override
  55. public void handleMessage(Message msg) {
  56. progress.setVisibility(View.INVISIBLE);
  57. switch (msg.what) {
  58. case MSG_NET_TIMEOUT:
  59. break;
  60. case MSG_NET_STATUS_ERROR:
  61. break;
  62. case MSG_NET_SUCC:
  63. initSearchFlag = true;
  64. String result = msg.obj.toString();
  65. try {
  66. JSONObject json = new JSONObject(result);
  67. parser(json);
  68. } catch (JSONException e) {
  69. // TODO Auto-generated catch block
  70. e.printStackTrace();
  71. }
  72. break;
  73. }
  74. }
  75. };
  76. @Override
  77. protected void onCreate(Bundle savedInstanceState) {
  78. super.onCreate(savedInstanceState);
  79. context = this;
  80. setContentView(R.layout.activity_main);
  81. progress = (RelativeLayout) findViewById(R.id.progress);
  82. // 初始化tab页(列表,附近)
  83. initTabHost();
  84. // 初始化筛选条件(区域,价格,类型)
  85. initSpinner();
  86. // 初始化切换按钮(筛选,附近)
  87. initToggleButton();
  88. // 初始化附近范围(1000米,3000米,5000米)
  89. initRadioGroup();
  90. // 发起搜索请求
  91. search();
  92. DemoApplication.getInstance().setHandler(mHandler);
  93. }
  94. @Override
  95. protected void onStart() {
  96. // TODO Auto-generated method stub
  97. super.onStart();
  98. }
  99. public void onDestroy() {
  100. // TODO Auto-generated method stub
  101. super.onDestroy();
  102. }
  103. /*
  104. * 添加对back按钮的处理,点击提示退出
  105. * (non-Javadoc)
  106. * @see android.app.Activity#dispatchKeyEvent(android.view.KeyEvent)
  107. */
  108. @Override
  109. public boolean dispatchKeyEvent(KeyEvent event) {
  110. if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
  111. && event.getAction() != 1) {
  112. exit();
  113. return true;
  114. }
  115. return super.dispatchKeyEvent(event);
  116. }
  117. private void initRadioGroup() {
  118. RadioGroup filter2 = (RadioGroup) findViewById(R.id.filter2);
  119. // 附近搜索 选择不同距离的点击事件
  120. filter2.setOnCheckedChangeListener(new android.widget.RadioGroup.OnCheckedChangeListener() {
  121. @Override
  122. public void onCheckedChanged(android.widget.RadioGroup arg0,
  123. int arg1) {
  124. search(LBSCloudSearch.SEARCH_TYPE_NEARBY);
  125. }
  126. });
  127. }
  128. private void initToggleButton() {
  129. ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton1);
  130. // 添加toggle button点击事件
  131. toggleButton
  132. .setOnCheckedChangeListener(new android.widget.CompoundButton.OnCheckedChangeListener() {
  133. @Override
  134. public void onCheckedChanged(
  135. android.widget.CompoundButton arg0, boolean arg1) {
  136. TextView textView1 = (TextView) findViewById(R.id.toggle_text1);
  137. TextView textView2 = (TextView) findViewById(R.id.toggle_text2);
  138. HorizontalScrollView filter1 = (HorizontalScrollView) findViewById(R.id.filter1);
  139. RadioGroup filter2 = (RadioGroup) findViewById(R.id.filter2);
  140. if (arg1) {
  141. // 显示附近
  142. textView1.setVisibility(View.INVISIBLE);
  143. filter1.setVisibility(View.INVISIBLE);
  144. textView2.setVisibility(View.VISIBLE);
  145. filter2.setVisibility(View.VISIBLE);
  146. search(LBSCloudSearch.SEARCH_TYPE_NEARBY);
  147. } else {
  148. // 显示筛选
  149. textView1.setVisibility(View.VISIBLE);
  150. filter1.setVisibility(View.VISIBLE);
  151. textView2.setVisibility(View.INVISIBLE);
  152. filter2.setVisibility(View.INVISIBLE);
  153. search();
  154. }
  155. }
  156. });
  157. }
  158. private void initTabHost() {
  159. final TabHost tabHost = getTabHost();
  160. // 添加列表tab和地图tab
  161. tabHost.addTab(tabHost.newTabSpec("tab1")
  162. .setIndicator(getString(R.string.tab_1_text))
  163. .setContent(new Intent(this, LBSListActivity.class)));
  164. tabHost.addTab(tabHost.newTabSpec("tab2")
  165. .setIndicator(getString(R.string.tab_2_text))
  166. .setContent(new Intent(this, LBSMapActivity.class)));
  167. TabWidget tabWidget = tabHost.getTabWidget();
  168. // 将tab的图文组合改为文字显示并调整tab高度
  169. for (int i = 0; i < tabWidget.getChildCount(); i++) {
  170. View child = tabWidget.getChildAt(i);
  171. final TextView tv = (TextView) child
  172. .findViewById(android.R.id.title);
  173. RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) tv
  174. .getLayoutParams();
  175. params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 0); // 取消文字底边对齐
  176. params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); // 设置文字居中对齐
  177. child.getLayoutParams().height = 60; // hard code
  178. }
  179. }
  180. private void initSpinner() {
  181. final Spinner s1 = (Spinner) findViewById(R.id.spinner1);
  182. final Spinner s2 = (Spinner) findViewById(R.id.spinner2);
  183. final Spinner s3 = (Spinner) findViewById(R.id.spinner3);
  184. // 设置区域过滤下拉框
  185. ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
  186. this, R.array.region, android.R.layout.simple_spinner_item);
  187. adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  188. s1.setAdapter(adapter);
  189. s1.setOnItemSelectedListener(new OnItemSelectedListener() {
  190. public void onItemSelected(AdapterView<?> parent, View view,
  191. int position, long id) {
  192. if (initSearchFlag) {
  193. search();
  194. }
  195. }
  196. public void onNothingSelected(AdapterView<?> parent) {
  197. }
  198. });
  199. // 设置价格过滤下拉框
  200. adapter = ArrayAdapter.createFromResource(this, R.array.price,
  201. android.R.layout.simple_spinner_item);
  202. adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  203. s2.setAdapter(adapter);
  204. s2.setOnItemSelectedListener(new OnItemSelectedListener() {
  205. public void onItemSelected(AdapterView<?> parent, View view,
  206. int position, long id) {
  207. if (initSearchFlag) {
  208. search();
  209. }
  210. }
  211. public void onNothingSelected(AdapterView<?> parent) {
  212. }
  213. });
  214. // 设置短租类型下拉框
  215. adapter = ArrayAdapter.createFromResource(this, R.array.category,
  216. android.R.layout.simple_spinner_item);
  217. adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  218. s3.setAdapter(adapter);
  219. s3.setOnItemSelectedListener(new OnItemSelectedListener() {
  220. public void onItemSelected(AdapterView<?> parent, View view,
  221. int position, long id) {
  222. if (initSearchFlag) {
  223. search();
  224. }
  225. }
  226. public void onNothingSelected(AdapterView<?> parent) {
  227. }
  228. });
  229. }
  230. /*
  231. * 云检索发起
  232. */
  233. private void search() {
  234. search(LBSCloudSearch.SEARCH_TYPE_LOCAL);
  235. }
  236. /*
  237. * 根据搜索类型发起检索
  238. */
  239. private void search(int searchType){
  240. progress.setVisibility(View.VISIBLE);
  241. DemoApplication app = DemoApplication.getInstance();
  242. app.getList().clear(); // 搜索钱清空列表
  243. app.getListActivity().loadMoreView.setVisibility(View.INVISIBLE);
  244. if (app.getListActivity().getListView().getFooterViewsCount() == 0) {
  245. // 点击查看更多按钮添加
  246. app.getListActivity().getListView()
  247. .addFooterView(app.getListActivity().loadMoreView);
  248. }
  249. app.getListActivity().getListView().setAdapter(app.getAdapter());
  250. // 云检索发起
  251. LBSCloudSearch.request(searchType, getRequestParams(), mHandler,
  252. DemoApplication.networkType);
  253. }
  254. /*
  255. * 获取云检索参数
  256. */
  257. private HashMap<String, String> getRequestParams() {
  258. HashMap<String, String> map = new HashMap<String, String>();
  259. Spinner s1 = (Spinner) findViewById(R.id.spinner1);
  260. Spinner s2 = (Spinner) findViewById(R.id.spinner2);
  261. Spinner s3 = (Spinner) findViewById(R.id.spinner3);
  262. ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton1);
  263. try {
  264. map.put("region", URLEncoder.encode("北京", "utf-8"));
  265. String filter = "";
  266. if (toggleButton.isChecked()) {
  267. // 附件,周边搜索
  268. RadioGroup filter2 = (RadioGroup) findViewById(R.id.filter2);
  269. RadioButton rb = (RadioButton) findViewById(filter2
  270. .getCheckedRadioButtonId());
  271. String radius = rb.getText().toString();
  272. radius = radius.substring(0, radius.length() - 1);
  273. map.put("radius", radius);
  274. DemoApplication app = DemoApplication.getInstance();
  275. if (app.currlocation != null) {
  276. map.put("location", app.currlocation.getLongitude() + ","
  277. + app.currlocation.getLatitude());
  278. } else {
  279. // 无定位数据默认北京中心
  280. double cLat = 39.909230;
  281. double cLon = 116.397428;
  282. map.put("location", cLat + "," + cLon);
  283. }
  284. } else {
  285. // 筛选,本地搜索
  286. if (s1.getSelectedItemPosition() > 0) {
  287. String s1String = s1.getSelectedItem().toString();
  288. map.put("q", URLEncoder.encode(s1String, "utf-8"));
  289. }
  290. if (s2.getSelectedItemPosition() > 0) {
  291. String s2String = s2.getSelectedItem().toString();
  292. String[] priceArray = context.getResources()
  293. .getStringArray(R.array.price);
  294. for (int i = 0; i < priceArray.length; i++) {
  295. if (s2String.equals(priceArray[i])) {
  296. if (i == priceArray.length - 1) {
  297. filter = filter
  298. + URLEncoder.encode("|", "utf-8")
  299. + "dayprice:" + (i - 1) * 50
  300. + URLEncoder.encode(",", "utf-8")
  301. + 10000;
  302. } else {
  303. filter = filter
  304. + URLEncoder.encode("|", "utf-8")
  305. + "dayprice:" + (i - 1) * 50
  306. + URLEncoder.encode(",", "utf-8") + i
  307. * 50;
  308. }
  309. break;
  310. }
  311. }
  312. }
  313. if (s3.getSelectedItemPosition() > 0) {
  314. String s3String = s3.getSelectedItem().toString();
  315. String[] categoryArray = context.getResources()
  316. .getStringArray(R.array.category);
  317. for (int i = 0; i < categoryArray.length; i++) {
  318. if (s3String.equals(categoryArray[i])) {
  319. filter = filter + URLEncoder.encode("|", "utf-8")
  320. + "leasetype:" + i
  321. + URLEncoder.encode(",", "utf-8") + i;
  322. break;
  323. }
  324. }
  325. }
  326. }
  327. map.put("filter", filter);
  328. } catch (UnsupportedEncodingException e) {
  329. // TODO Auto-generated catch block
  330. e.printStackTrace();
  331. } catch (NotFoundException e) {
  332. // TODO Auto-generated catch block
  333. e.printStackTrace();
  334. }
  335. DemoApplication.getInstance().setFilterParams(map);
  336. return map;
  337. }
  338. /*
  339. * 解析返回数据
  340. */
  341. private void parser(JSONObject json) {
  342. DemoApplication app = (DemoApplication) getApplication();
  343. List<ContentModel> list = app.getList();
  344. try {
  345. app.getListActivity().totalItem = json.getInt("total");
  346. JSONArray jsonArray = json.getJSONArray("contents");
  347. if (jsonArray != null && jsonArray.length() <= 0) {
  348. Toast.makeText(context, "没有符合要求的数据", Toast.LENGTH_SHORT).show();
  349. } else {
  350. for (int i = 0; i < jsonArray.length(); i++) {
  351. JSONObject jsonObject2 = (JSONObject) jsonArray.opt(i);
  352. ContentModel content = new ContentModel();
  353. content.setName(jsonObject2.getString("title"));
  354. content.setAddr(jsonObject2.getString("address"));
  355. content.setDistance(jsonObject2.getString("distance") + "米");
  356. // double latitude = jsonObject2.getDouble("latitude");
  357. // double longitude = jsonObject2.getDouble("longitude");
  358. JSONArray locArray = jsonObject2.getJSONArray("location");
  359. double latitude = locArray.getDouble(1);
  360. double longitude = locArray.getDouble(0);
  361. content.setLatitude(latitude);
  362. content.setLongitude(longitude);
  363. float results[] = new float[1];
  364. if (app.currlocation != null) {
  365. Location.distanceBetween(
  366. app.currlocation.getLatitude(),
  367. app.currlocation.getLongitude(), latitude,
  368. longitude, results);
  369. }
  370. content.setDistance((int) results[0] + "米");
  371. // JSONObject jsonObject3 = jsonObject2.getJSONObject("ext");
  372. // content.setPrice(jsonObject3.getString("dayprice"));
  373. // content.setImageurl(jsonObject3.getString("mainimage"));
  374. // content.setWebUrl(jsonObject3.getString("roomurl"));
  375. // int leasetype = jsonObject3.getInt("leasetype");
  376. content.setPrice(jsonObject2.getString("dayprice"));
  377. content.setImageurl(jsonObject2.getString("mainimage"));
  378. content.setWebUrl(jsonObject2.getString("roomurl"));
  379. int leasetype = jsonObject2.getInt("leasetype");
  380. String[] categoryArray = context.getResources()
  381. .getStringArray(R.array.category);
  382. if(leasetype > categoryArray.length -1){
  383. leasetype = 1;
  384. }
  385. content.setLeaseType(categoryArray[leasetype]);
  386. list.add(content);
  387. }
  388. }
  389. if (list.size() < 10) {
  390. app.getListActivity().getListView()
  391. .removeFooterView(app.getListActivity().loadMoreView);
  392. }
  393. } catch (JSONException e) {
  394. // TODO Auto-generated catch block
  395. e.printStackTrace();
  396. }
  397. ContentAdapter adapter = ((DemoApplication) getApplication())
  398. .getAdapter();
  399. if (adapter != null) {
  400. adapter.notifyDataSetChanged();
  401. app.getListActivity().loadMoreView.setVisibility(View.VISIBLE);
  402. app.getListActivity().progressBar.setVisibility(View.INVISIBLE);
  403. }
  404. if (app.getMapActivity() != null) {
  405. app.getMapActivity().removeAllMarker();
  406. app.getMapActivity().addAllMarker();
  407. }
  408. }
  409. /*
  410. * 退出应用程序
  411. */
  412. private void exit() {
  413. new AlertDialog.Builder(MainActivity.this)
  414. .setMessage(R.string.exit_confirm)
  415. .setPositiveButton(R.string.button_ok,
  416. new DialogInterface.OnClickListener() {
  417. public void onClick(DialogInterface dialog,
  418. int whichButton) {
  419. finish();
  420. android.os.Process
  421. .killProcess(android.os.Process.myPid());
  422. }
  423. }).setNegativeButton(R.string.button_cancel, null)
  424. .show();
  425. }
  426. }