Browse Source

地址搜索修改

huyuguo 5 years ago
parent
commit
71aeae060b

+ 25 - 339
app/src/main/java/com/electric/chargingpile/activity/SearchActivity.java

8
import android.database.Cursor;
8
import android.database.Cursor;
9
import android.os.Bundle;
9
import android.os.Bundle;
10
import android.os.Handler;
10
import android.os.Handler;
11

11
import androidx.appcompat.app.AlertDialog;
12
import androidx.appcompat.app.AlertDialog;
13

12
import android.text.Editable;
14
import android.text.Editable;
13
import android.text.TextUtils;
15
import android.text.TextUtils;
14
import android.text.TextWatcher;
16
import android.text.TextWatcher;
61
    private TextView tvSearch;
63
    private TextView tvSearch;
62
    private Toast toast = null;
64
    private Toast toast = null;
63
    private EditText etSearch;
65
    private EditText etSearch;
64
    private ImageView ivClear, iv_clear_address;
66
    private ImageView ivClear;
65
    private ListView lvContent;
67
    private ListView lvContent;
66
    private SearchAdapter adapter;
68
    private SearchAdapter adapter;
67
    private List<Map<String, String>> list = new ArrayList<Map<String, String>>();
69
    private List<Map<String, String>> list = new ArrayList<Map<String, String>>();
71
    private HistoryInfo history;
73
    private HistoryInfo history;
72

74

73
    private Map<String, String> map;
75
    private Map<String, String> map;
74
    private Map<String, String> history_map;
75

76

76
    private View convertView = null;
77
    private View convertView = null;
77

78

78
    private boolean stype = true;
79
    private boolean stype = true;
79
    InputMethodManager imm;
80
    InputMethodManager imm;
80
    private LinearLayout ll_menu, ll_address, ll_zhan;
81
    private TextView searchZhan, searchAddress;
81
    private LinearLayout ll_menu;
82
    ListView historyList;
82
    ListView historyList;
83
    private PoiResult poiResult;// poi返回的结果
83
    private PoiResult poiResult;// poi返回的结果
84
    private int currentPage = 0;// 当前页面,从0开始计数
84
    private int currentPage = 0;// 当前页面,从0开始计数
93
        super.onCreate(savedInstanceState);
93
        super.onCreate(savedInstanceState);
94
        setContentView(R.layout.activity_search);
94
        setContentView(R.layout.activity_search);
95
        BarColorUtil.initStatusBarColor(SearchActivity.this);
95
        BarColorUtil.initStatusBarColor(SearchActivity.this);
96

97
//        String jing = "116.63800787798";
98
//        String wei = "39.896712151452";
99
//        int position_jing = jing.length() - jing.indexOf(".") - 1;
100
//        int position_wei = wei.length() - wei.indexOf(".") - 1;
101
//        Log.e("1111",position_jing+"------"+position_wei);
102
//        requestManager = PoCRequestManager.from(this);
103
//        mPoiSearch = PoiSearch.newInstance();
104
//        mPoiSearch.setOnGetPoiSearchResultListener(this);
105
        stype = getIntent().getBooleanExtra("searchtype", true);
96
        stype = getIntent().getBooleanExtra("searchtype", true);
106
        initView();
97
        initView();
107
//        Test(23.3456001);
108
    }
109

110
    private void Test(double d) {
111

112
        String s = String.valueOf(d);
113

114
        s = s.substring(s.indexOf('.') + 1);
115

116
        Log.e("int===", s.length() + "");
117
    }
98
    }
118

99

119
    private void initView() {
100
    private void initView() {
123
        tvSearch.setOnClickListener(this);
104
        tvSearch.setOnClickListener(this);
124
        ivClear = (ImageView) findViewById(R.id.iv_search_clear);
105
        ivClear = (ImageView) findViewById(R.id.iv_search_clear);
125
        ivClear.setOnClickListener(this);
106
        ivClear.setOnClickListener(this);
126
//        ll_menu = (LinearLayout) findViewById(R.id.ll_menu);
127
//        ll_address = (LinearLayout) findViewById(R.id.ll_address);
128
//        ll_address.setOnClickListener(this);
129
//        ll_zhan = (LinearLayout) findViewById(R.id.ll_zhan);
130
//        ll_zhan.setOnClickListener(this);
131
//        searchAddress = (TextView) findViewById(R.id.address_search);
132
//
133
//        searchZhan = (TextView) findViewById(R.id.zhan_search);
134

107

135
        etSearch = (EditText) findViewById(R.id.et_search);
108
        etSearch = (EditText) findViewById(R.id.et_search);
136
//		if (stype) {
137
//			etSearch.setHint("输入想查找的位置");
138
//		} else {
139
//			etSearch.setHint("输入想查找的站名");
140
//		}
141
        etSearch.setOnClickListener(new OnClickListener() {
109
        etSearch.setOnClickListener(new OnClickListener() {
142
            @Override
110
            @Override
143
            public void onClick(View v) {
111
            public void onClick(View v) {
144

112

145
//                if (!TextUtils.isEmpty(etSearch.getText().toString().trim())) {
146
//                    ll_menu.setVisibility(View.VISIBLE);
147
//                }
148
                DBOpenHandler db = new DBOpenHandler(SearchActivity.this, "");
113
                DBOpenHandler db = new DBOpenHandler(SearchActivity.this, "");
149
                Cursor c = db.query();
114
                Cursor c = db.query();
150
                System.err.println(c.getCount());
115
                System.err.println(c.getCount());
158
                hList.clear();
123
                hList.clear();
159
                while (c.moveToNext()) {
124
                while (c.moveToNext()) {
160
                    history = new HistoryInfo();
125
                    history = new HistoryInfo();
161
                    history.setIds(c.getString(c
162
                            .getColumnIndex("keyword")));
163
//                    Log.e("11111111",c.getString(c
164
//                            .getColumnIndex("keyword")));
165
                    history.setJing(c.getString(c
166
                            .getColumnIndex("jing")));
167
                    history.setWei(c.getString(c
168
                            .getColumnIndex("wei")));
126
                    history.setIds(c.getString(c.getColumnIndex("keyword")));
127
                    history.setJing(c.getString(c.getColumnIndex("jing")));
128
                    history.setWei(c.getString(c.getColumnIndex("wei")));
169
                    hList.add(history);
129
                    hList.add(history);
170
                }
130
                }
171

131

172

173
                SimpleCursorAdapter adapter = new SimpleCursorAdapter(SearchActivity.this, R.layout.history_item, c, from, to);
132
                SimpleCursorAdapter adapter = new SimpleCursorAdapter(SearchActivity.this, R.layout.history_item, c, from, to);
174
                historyList.setAdapter(adapter);
133
                historyList.setAdapter(adapter);
175

176
            }
134
            }
177
        });
135
        });
178
        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
136
        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
179
            @Override
137
            @Override
180
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
138
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
181
//                searchInPoi();
182
//                ll_menu.setVisibility(View.GONE);
183
                imm.hideSoftInputFromWindow(etSearch.getWindowToken(), 0);
139
                imm.hideSoftInputFromWindow(etSearch.getWindowToken(), 0);
184

185
                return true;
140
                return true;
186
            }
141
            }
187
        });
142
        });
188
        etSearch.addTextChangedListener(new TextWatcher() {
143
        etSearch.addTextChangedListener(new TextWatcher() {
189
            @Override
144
            @Override
190
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
145
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
191

192
            }
146
            }
193

147

194
            @Override
148
            @Override
195
            public void onTextChanged(CharSequence s, int start, int before, int count) {
149
            public void onTextChanged(CharSequence s, int start, int before, int count) {
196
                String newText = s.toString().trim();
150
                String newText = s.toString().trim();
197
                Log.e("newText", newText);
198
                if (!TextUtils.isEmpty(etSearch.getText().toString().trim())) {
151
                if (!TextUtils.isEmpty(newText)) {
199
                    keyWord = newText;
152
                    keyWord = newText;
200
                    doSearchQuery();
153
                    doSearchQuery();
201
                    lvContent.setVisibility(View.VISIBLE);
154
                    lvContent.setVisibility(View.VISIBLE);
202
                    historyList.setVisibility(View.GONE);
155
                    historyList.setVisibility(View.GONE);
156
                    ivClear.setVisibility(View.VISIBLE);
203
                } else {
157
                } else {
204
                    list.clear();
158
                    list.clear();
205
                    mList.clear();
159
                    mList.clear();
206
                    historyList.setVisibility(View.VISIBLE);
160
                    historyList.setVisibility(View.VISIBLE);
207
                    lvContent.setVisibility(View.GONE);
161
                    lvContent.setVisibility(View.GONE);
208
                    Log.e("444", "444");
162
                    ivClear.setVisibility(View.GONE);
209
                }
163
                }
210

211
            }
164
            }
212

165

213
            @Override
166
            @Override
214
            public void afterTextChanged(Editable s) {
167
            public void afterTextChanged(Editable s) {
215

216
            }
168
            }
217
        });
169
        });
218
        etSearch.setOnKeyListener(new OnKeyListener() {
170
        etSearch.setOnKeyListener(new OnKeyListener() {
219

220
            @Override
171
            @Override
221
            public boolean onKey(View v, int keyCode, KeyEvent event) {
172
            public boolean onKey(View v, int keyCode, KeyEvent event) {
222
                if (keyCode == KeyEvent.KEYCODE_ENTER
173
                if (keyCode == KeyEvent.KEYCODE_ENTER
225
                    if (imm.isActive()) {
176
                    if (imm.isActive()) {
226
                        View view = getWindow().peekDecorView();
177
                        View view = getWindow().peekDecorView();
227
                        if (view != null) {
178
                        if (view != null) {
228
                            imm.hideSoftInputFromWindow(view.getWindowToken(),
229
                                    0);
230
//                            ll_menu.setVisibility(View.GONE);
179
                            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
231
                        }
180
                        }
232
                    }
181
                    }
233
                    if (TextUtils.isEmpty(etSearch.getText().toString().trim())) {
182
                    if (TextUtils.isEmpty(etSearch.getText().toString().trim())) {
263

212

264
            @Override
213
            @Override
265
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
214
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
266
//                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
267
            }
215
            }
268
        });
216
        });
269
        historyList.setOnScrollListener(new AbsListView.OnScrollListener() {
217
        historyList.setOnScrollListener(new AbsListView.OnScrollListener() {
290
                intent.putExtra("zhan_name", hList.get(position).getIds());
238
                intent.putExtra("zhan_name", hList.get(position).getIds());
291
                intent.putExtra("jing", hList.get(position).getJing());
239
                intent.putExtra("jing", hList.get(position).getJing());
292
                intent.putExtra("wei", hList.get(position).getWei());
240
                intent.putExtra("wei", hList.get(position).getWei());
293
//                Log.e("2222",hList.get(position).getKeyword());
294
                ProfileManager.getInstance().setSearchAddress(getApplicationContext(), hList.get(position).getIds());
241
                ProfileManager.getInstance().setSearchAddress(getApplicationContext(), hList.get(position).getIds());
295
                setResult(101, intent);
242
                setResult(101, intent);
296
                finish();
243
                finish();
299
                if (v != null) {
246
                if (v != null) {
300
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
247
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
301
                }
248
                }
302
//                MainMapActivity.search_back = true;
303
            }
249
            }
304
        });
250
        });
305

251

317
                    values.put("type", list.get(position).get("address"));
263
                    values.put("type", list.get(position).get("address"));
318
                    values.put("jing", list.get(position).get("jing"));
264
                    values.put("jing", list.get(position).get("jing"));
319
                    values.put("wei", list.get(position).get("wei"));
265
                    values.put("wei", list.get(position).get("wei"));
320

321
                    db.insert(values);
266
                    db.insert(values);
322

267

323

324
                    Cursor c = db.query();
268
                    Cursor c = db.query();
325
                    System.err.println(c.getCount());
269
                    System.err.println(c.getCount());
326
                    if (c.getCount() <= 0) {
270
                    if (c.getCount() <= 0) {
328
                    }
272
                    }
329
                    String[] from = {"keyword", "type"};
273
                    String[] from = {"keyword", "type"};
330
                    int[] to = {R.id.search_name, R.id.search_address};
274
                    int[] to = {R.id.search_name, R.id.search_address};
331
                    SimpleCursorAdapter historyAdapter = new SimpleCursorAdapter(SearchActivity.this, R.layout.history_item, c, from, to);
275
                    SimpleCursorAdapter historyAdapter = new SimpleCursorAdapter(SearchActivity.this, R.layout.history_item, c, from, to, 0);
332
                    historyList.setAdapter(historyAdapter);
276
                    historyList.setAdapter(historyAdapter);
333
                    Intent intent = new Intent();
277
                    Intent intent = new Intent();
334
                    intent.putExtra("zhan_name", list.get(position).get("name"));
278
                    intent.putExtra("zhan_name", list.get(position).get("name"));
335
                    intent.putExtra("jing", list.get(position).get("jing"));
279
                    intent.putExtra("jing", list.get(position).get("jing"));
336
                    intent.putExtra("wei", list.get(position).get("wei"));
280
                    intent.putExtra("wei", list.get(position).get("wei"));
337
                    Log.e("jing", list.get(position).get("jing"));
338
                    Log.e("zhan_name", list.get(position).get("name"));
339
                    Log.e("wei", list.get(position).get("wei"));
340
                    ProfileManager.getInstance().setSearchAddress(getApplicationContext(), list.get(position).get("name"));
281
                    ProfileManager.getInstance().setSearchAddress(getApplicationContext(), list.get(position).get("name"));
341
                    setResult(101, intent);
282
                    setResult(101, intent);
342
                    View v = getWindow().peekDecorView();
283
                    View v = getWindow().peekDecorView();
376
        hList.clear();
317
        hList.clear();
377
        while (c.moveToNext()) {
318
        while (c.moveToNext()) {
378
            history = new HistoryInfo();
319
            history = new HistoryInfo();
379
            history.setIds(c.getString(c
380
                    .getColumnIndex("keyword")));
381
            history.setJing(c.getString(c
382
                    .getColumnIndex("jing")));
383
            history.setWei(c.getString(c
384
                    .getColumnIndex("wei")));
320
            history.setIds(c.getString(c.getColumnIndex("keyword")));
321
            history.setJing(c.getString(c.getColumnIndex("jing")));
322
            history.setWei(c.getString(c.getColumnIndex("wei")));
385
            hList.add(history);
323
            hList.add(history);
386
        }
324
        }
387

325

388
        for (int i = 0; i < hList.size(); i++) {
389
            Log.e(i + "==", hList.get(i).getIds());
390
        }
391

392
        String[] from = {"keyword", "type"};
326
        String[] from = {"keyword", "type"};
393
        int[] to = {R.id.search_name, R.id.search_address};
327
        int[] to = {R.id.search_name, R.id.search_address};
394
        final SimpleCursorAdapter adapter3 = new SimpleCursorAdapter(SearchActivity.this, R.layout.history_item, c, from, to);
395
//        TextView footview = new TextView(this);
396
//        footview.setText("清楚历史记录");
397
//        footview.setHeight();
398
//        LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
399
//                LinearLayout.LayoutParams.WRAP_CONTENT);
400
//        LinearLayout layout = new LinearLayout(this);
401
//        //设置布局水平方向  
402
//        layout.setOrientation(LinearLayout.HORIZONTAL);
403
//        //文本内容  
404
//        TextView footview = new TextView(this);
405
//        footview.setText("清楚历史记录");
406
//        footview.setGravity(Gravity.CENTER_VERTICAL);
407
//        //把文本加入到layout中  
408
//        layout.addView(footview, mLayoutParams);
409
//        //设置layout的重力方向,即对齐方式是  
410
//        layout.setGravity(Gravity.CENTER);
328
        final SimpleCursorAdapter adapter3 = new SimpleCursorAdapter(SearchActivity.this, R.layout.history_item, c, from, to, 0);
411

329

412
        convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_clear, null);
330
        convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_clear, null);
413
        historyList.addFooterView(convertView);
331
        historyList.addFooterView(convertView);
430
                });
348
                });
431
                builder.setNegativeButton("取消", null);
349
                builder.setNegativeButton("取消", null);
432
                builder.create().show();
350
                builder.create().show();
433

434
            }
351
            }
435
        });
352
        });
436
        historyList.setAdapter(adapter3);
353
        historyList.setAdapter(adapter3);
437

438

439
    }
354
    }
440

355

441
//    private void searchInPoi() {
442
//        lvContent.setVisibility(View.VISIBLE);
443
//        historyList.setVisibility(View.GONE);
444
//        String search = etSearch.getText().toString().trim();
445
//        if (MainFragment.center != null) {
446
////            mPoiSearch.searchNearby((new PoiNearbySearchOption())z
447
////                    .location(MainFragment.center).keyword(search)
448
////                    .pageCapacity(20).pageNum(0).radius(10000));
449
//            mPoiSearch.searchInCity(new PoiCitySearchOption().city(MainApplication.atCity).keyword(search).pageCapacity(20).pageNum(0));
450
//
451
//        }
452
//    }
453

454

455
//    @Override
456
//    public void onRequestFinished(int resultCode, Bundle payload) {
457
//        int flag = payload.getInt(PoCService.INTENT_EXTRA_WORKER_TYPE);
458
//        switch (flag) {
459
//            case PoCRequestManager.CHARGING_WORE_TYPE_GET_ZHAN_SEARCH:
460
//                Gson g = new Gson();
461
//                Type type = new TypeToken<List<Zhan>>() {
462
//                }.getType();
463
//                List<Zhan> l = g.fromJson(payload.getString("zhansearch"), type);
464
//                list.clear();
465
//                mList.clear();
466
//                if (l != null && l.size() > 0) {
467
//                    for (int i = 0; i < l.size(); i++) {
468
//                        map = new HashMap<String, String>();
469
//                        map.put("name", l.get(i).getZhan_name());
470
//                        map.put("address", l.get(i).getZhan_address());
471
//                        map.put("jing", l.get(i).getPoi_jing() + "");
472
//                        map.put("wei", l.get(i).getPoi_wei() + "");
473
//                        map.put("url", l.get(i).getPic() + "");
474
//                        list.add(map);
475
//                        if (MainFragment.center != null) {
476
//                            double dwei = Double.parseDouble(l.get(i)
477
//                                    .getPoi_wei());
478
//                            double djing = Double.parseDouble(l.get(i)
479
//                                    .getPoi_jing());
480
//                            LatLng ll = new LatLng(dwei, djing);
481
//                            double d = DistanceUtil.getDistance(MainFragment.center, ll);
482
//                            l.get(i).setDistance(Util.getDistance(d));
483
//                        }
484
//                        mList.add(l.get(i));
485
//
486
//                    }
487
//                    Log.e("!!!",mList.size()+"");
488
//                } else {
489
//
490
//                    Toast.makeText(SearchActivity.this, "抱歉,没有找到相关的信息",
491
//                            Toast.LENGTH_SHORT).show();
492
//                }
493
//                ll_menu.setVisibility(View.GONE);
494
//                list.clear();
495
//                mList.clear();
496
//                adapter.notifyDataSetChanged();
497
//                break;
498
//
499
//            default:
500
//                break;
501
//        }
502
//    }
503

504
    @Override
356
    @Override
505
    public void onClick(View v) {
357
    public void onClick(View v) {
506
        switch (v.getId()) {
358
        switch (v.getId()) {
527

379

528
            //界面搜索框内清空图标
380
            //界面搜索框内清空图标
529
            case R.id.iv_search_clear:
381
            case R.id.iv_search_clear:
530

382
                ivClear.setVisibility(View.GONE);
531
                etSearch.setText("");
383
                etSearch.setText("");
532
                list.clear();
384
                list.clear();
533
                mList.clear();
385
                mList.clear();
534
                adapter.notifyDataSetChanged();
386
                adapter.notifyDataSetChanged();
535
                lvContent.setVisibility(View.GONE);
387
                lvContent.setVisibility(View.GONE);
536
                historyList.setVisibility(View.VISIBLE);
388
                historyList.setVisibility(View.VISIBLE);
537
//                    DBOpenHandler db = new DBOpenHandler(SearchActivity.this, "");
538
//                    Cursor c = db.query();
539
//                    System.err.println(c.getCount());
540
//                    if (c.getCount() <= 0) {
541
//                        return;
542
//                    }
543
//
544
//
545
//                    hList.clear();
546
//                    while (c.moveToNext()) {
547
//                        history = new HistoryInfo();
548
//                        history.setIds(c.getString(c
549
//                                .getColumnIndex("keyword")));
550
//                        history.setJing(c.getString(c
551
//                                .getColumnIndex("jing")));
552
//                        history.setWei(c.getString(c
553
//                                .getColumnIndex("wei")));
554
//                        hList.add(history);
555
//                    }
556
//
557
//                    String[] from = {"keyword", "type"};
558
//                    int[] to = {R.id.search_name, R.id.search_address};
559
//                    final SimpleCursorAdapter adapter3 = new SimpleCursorAdapter(SearchActivity.this, R.layout.history_item, c, from, to);
560
//                    convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_clear, null);
561
//                    historyList.addFooterView(convertView);
562
//                    TextView tv_clear = (TextView) convertView.findViewById(R.id.tv_historyClear);
563
//                    tv_clear.setOnClickListener(new OnClickListener() {
564
//                        @Override
565
//                        public void onClick(View v) {
566
//                            new com.electric.chargingpile.view.AlertDialog(SearchActivity.this).builder()
567
//                                    .setMsg("您确定要清空搜索历史吗?")
568
//                                    .setPositiveButton("确定", new View.OnClickListener() {
569
//                                        @Override
570
//                                        public void onClick(View v) {
571
//                                            DBOpenHandler db = new DBOpenHandler(SearchActivity.this, "");
572
//                                            db.delTable();
573
//                                            historyList.setVisibility(View.GONE);
574
//                                            adapter3.notifyDataSetChanged();
575
//                                        }
576
//                                    }).setNegativeButton("取消", new View.OnClickListener() {
577
//                                @Override
578
//                                public void onClick(View v) {
579
//
580
//
581
//                                }
582
//                            }).show();
583
//
584
//                        }
585
//                    });
586
//                    historyList.setAdapter(adapter3);
587
//                    historyList.setVisibility(View.VISIBLE);
588
//                    lvContent.setVisibility(View.GONE);
589
//                ll_menu.setVisibility(View.GONE);
590

591
                break;
389
                break;
592
            //按地点搜索
593
//            case R.id.ll_address:
594
//                searchInPoi();
595
//                ll_menu.setVisibility(View.GONE);
596
//                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
597
//                break;
598
//            //按站点搜索
599
//            case R.id.ll_zhan:
600
//                getSearchZhan();
601
//                ll_menu.setVisibility(View.GONE);
602
//                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
603
//                break;
604
        }
390
        }
605
    }
391
    }
606

392

626
                    poiResult = result;
412
                    poiResult = result;
627
                    // 取得搜索到的poiitems有多少页
413
                    // 取得搜索到的poiitems有多少页
628
                    List<PoiItem> mList = poiResult.getPois();// 取得第一页的poiitem数据,页数从数字0开始
414
                    List<PoiItem> mList = poiResult.getPois();// 取得第一页的poiitem数据,页数从数字0开始
629
//                    List<PoiItem> mList = result.getAllPoi();
630
                    List<SuggestionCity> suggestionCities = poiResult
631
                            .getSearchSuggestionCitys();// 当搜索不到poiitem数据时,会返回含有搜索关键字的城市信息
415
                    List<SuggestionCity> suggestionCities = poiResult.getSearchSuggestionCitys();// 当搜索不到poiitem数据时,会返回含有搜索关键字的城市信息
632

416

633
                    if (mList != null && mList.size() > 0) {
417
                    if (mList != null && mList.size() > 0) {
634
                        list.clear();
418
                        list.clear();
647
                                list.add(map);
431
                                list.add(map);
648
                            }
432
                            }
649
                            if (list != null && list.size() > 0) {
433
                            if (list != null && list.size() > 0) {
650

651

652
                                runOnUiThread(new Runnable() {
434
                                runOnUiThread(new Runnable() {
653
                                    @Override
435
                                    @Override
654
                                    public void run() {
436
                                    public void run() {
655
                                        adapter.notifyDataSetChanged();
437
                                        adapter.notifyDataSetChanged();
656
                                    }
438
                                    }
657
                                });
439
                                });
658

659

660
                            }
440
                            }
661

441

662
                        } catch (Exception e) {
442
                        } catch (Exception e) {
663
                            e.printStackTrace();
443
                            e.printStackTrace();
664
                        }
444
                        }
665
                    } else {
445
                    } else {
666
                        Toast.makeText(SearchActivity.this, "抱歉,未找到结果", Toast.LENGTH_SHORT)
667
                                .show();
446
                        Toast.makeText(SearchActivity.this, "抱歉,未找到结果", Toast.LENGTH_SHORT).show();
668
                        list.clear();
447
                        list.clear();
669
                        adapter.notifyDataSetChanged();
448
                        adapter.notifyDataSetChanged();
670
                    }
449
                    }
671

672
//                    if (poiItems != null && poiItems.size() > 0) {
673
////                        aMap.clear();// 清理之前的图标
674
//                        PoiOverlay poiOverlay = new PoiOverlay(aMap, poiItems);
675
//                        poiOverlay.removeFromMap();
676
//                        poiOverlay.addToMap();
677
//                        poiOverlay.zoomToSpan();
678
//                    } else if (suggestionCities != null
679
//                            && suggestionCities.size() > 0) {
680
//                        showSuggestCity(suggestionCities);
681
//                    } else {
682
////                        ToastUtil.show(PoiKeywordSearchActivity.this,
683
////                                R.string.no_result);
684
//                    }
685
                }
450
                }
686
            } else {
451
            } else {
687
//                ToastUtil.show(PoiKeywordSearchActivity.this,
688
//                        R.string.no_result);
689
            }
452
            }
690
        } else {
453
        } else {
691
//            ToastUtil.showerror(this, rCode);
692
        }
454
        }
693
    }
455
    }
694

456

725
        public View getView(int position, View convertView, ViewGroup parent) {
487
        public View getView(int position, View convertView, ViewGroup parent) {
726
            ViewHolder viewHolder = null;
488
            ViewHolder viewHolder = null;
727
            if (convertView == null) {
489
            if (convertView == null) {
728
                convertView = LayoutInflater.from(context).inflate(
729
                        R.layout.item_search, null);
490
                convertView = LayoutInflater.from(context).inflate(R.layout.item_search, null);
730
                viewHolder = new ViewHolder();
491
                viewHolder = new ViewHolder();
731
                viewHolder.name = (TextView) convertView
732
                        .findViewById(R.id.search_name);
733
                viewHolder.address = (TextView) convertView
734
                        .findViewById(R.id.search_address);
735
                viewHolder.zhan = (ImageView) convertView
736
                        .findViewById(R.id.search_zhan);
492
                viewHolder.name = (TextView) convertView.findViewById(R.id.search_name);
493
                viewHolder.address = (TextView) convertView.findViewById(R.id.search_address);
494
                viewHolder.zhan = (ImageView) convertView.findViewById(R.id.search_zhan);
737
                convertView.setTag(viewHolder);
495
                convertView.setTag(viewHolder);
738
            } else {
496
            } else {
739
                viewHolder = (ViewHolder) convertView.getTag();
497
                viewHolder = (ViewHolder) convertView.getTag();
746
            } else {
504
            } else {
747
                viewHolder.zhan.setVisibility(View.VISIBLE);
505
                viewHolder.zhan.setVisibility(View.VISIBLE);
748
                if (!TextUtils.isEmpty(list.get(position).get("url"))) {
506
                if (!TextUtils.isEmpty(list.get(position).get("url"))) {
749
                    ImageLoader.getInstance().init(
750
                            ImageLoaderConfiguration
751
                                    .createDefault(SearchActivity.this));
507
                    ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(SearchActivity.this));
752
                    DisplayImageOptions.Builder builder = new DisplayImageOptions.Builder();
508
                    DisplayImageOptions.Builder builder = new DisplayImageOptions.Builder();
753
                    builder.cacheInMemory(true);
509
                    builder.cacheInMemory(true);
754
                    builder.cacheOnDisc(true);
510
                    builder.cacheOnDisc(true);
756
                    builder.showImageForEmptyUri(R.drawable.image_default);
512
                    builder.showImageForEmptyUri(R.drawable.image_default);
757
                    builder.showImageOnFail(R.drawable.image_default);
513
                    builder.showImageOnFail(R.drawable.image_default);
758
                    builder.displayer(new RoundedBitmapDisplayer(0));
514
                    builder.displayer(new RoundedBitmapDisplayer(0));
759
                    ImageLoader.getInstance().displayImage(
760
                            list.get(position).get("url"), viewHolder.zhan,
761
                            builder.build());
515
                    ImageLoader.getInstance().displayImage(list.get(position).get("url"), viewHolder.zhan, builder.build());
762
                } else {
516
                } else {
763
                    viewHolder.zhan.setImageResource(R.drawable.image_default);
517
                    viewHolder.zhan.setImageResource(R.drawable.image_default);
764
                }
518
                }
772
        }
526
        }
773
    }
527
    }
774

528

775
//    @Override
776
//    public void onGetPoiDetailResult(PoiDetailResult result) {
777
//        if (result.error != SearchResult.ERRORNO.NO_ERROR) {
778
//            Toast.makeText(SearchActivity.this, "抱歉,未找到结果", Toast.LENGTH_SHORT)
779
//                    .show();
780
//        }
781
//    }
782

783
//    @Override
784
//    public void onGetPoiResult(PoiResult result) {
785
//        if (result == null
786
//                || result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
787
////            Toast.makeText(SearchActivity.this, "抱歉,没有搜到相关信息\n" +
788
////                    "您可尝试切换城市搜索", Toast.LENGTH_LONG)
789
////                    .show();
790
//            showTextToast("抱歉,没有搜到相关信息\n" +
791
//                    "您可尝试切换城市搜索");
792
//            list.clear();
793
//            adapter.notifyDataSetChanged();
794
//            return;
795
//        }
796
//        if (result.error == SearchResult.ERRORNO.NO_ERROR) {
797
//            List<PoiInfo> mList = result.getAllPoi();
798
//            if (mList != null && mList.size() > 0) {
799
//                list.clear();
800
//                try {
801
//                    for (int i = 0; i < mList.size(); i++) {
802
//                        map = new HashMap<String, String>();
803
//                        map.put("name", mList.get(i).name);
804
//                        map.put("address", mList.get(i).address);
805
//                        map.put("jing", mList.get(i).location.longitude + "");
806
//                        map.put("wei", mList.get(i).location.latitude + "");
807
//                        map.put("url", "");
808
//                        list.add(map);
809
//                    }
810
//                    adapter.notifyDataSetChanged();
811
//                } catch (Exception e) {
812
//                    e.printStackTrace();
813
//                }
814
//            }else {
815
//                Toast.makeText(SearchActivity.this, "抱歉,未找到结果", Toast.LENGTH_LONG)
816
//                        .show();
817
//            }
818
//
819
//            return;
820
//        }
821
//    }
822

823
    @Override
529
    @Override
824
    protected void onPause() {
530
    protected void onPause() {
825
        super.onPause();
531
        super.onPause();
848
     * 开始进行poi搜索
554
     * 开始进行poi搜索
849
     */
555
     */
850
    protected void doSearchQuery() {
556
    protected void doSearchQuery() {
851
//        showProgressDialog();// 显示进度框
852
        currentPage = 0;
557
        currentPage = 0;
853
        query = new PoiSearch.Query(keyWord, "", MainApplication.atCity);// 第一个参数表示搜索字符串,第二个参数表示poi搜索类型,第三个参数表示poi搜索区域(空字符串代表全国)
558
        query = new PoiSearch.Query(keyWord, "", MainApplication.atCity);// 第一个参数表示搜索字符串,第二个参数表示poi搜索类型,第三个参数表示poi搜索区域(空字符串代表全国)
854
        query.setPageSize(10);// 设置每页最多返回多少条poiitem
559
        query.setPageSize(10);// 设置每页最多返回多少条poiitem
858
        poiSearch.setOnPoiSearchListener(this);
563
        poiSearch.setOnPoiSearchListener(this);
859
        poiSearch.searchPOIAsyn();
564
        poiSearch.searchPOIAsyn();
860
    }
565
    }
861

862

863
//    @Override
864
//    public void onGetInputtips(List<Tip> tipList, int rCode) {
865
//        if (rCode == 1000) {// 正确返回
866
//            List<String> listString = new ArrayList<String>();
867
//            for (int i = 0; i < tipList.size(); i++) {
868
//                listString.add(tipList.get(i).getName());
869
//            }
870
//            ArrayAdapter<String> aAdapter = new ArrayAdapter<String>(
871
//                    getApplicationContext(),
872
//                    R.layout.route_inputs, listString);
873
//            searchText.setAdapter(aAdapter);
874
//            aAdapter.notifyDataSetChanged();
875
//        } else {
876
////            ToastUtil.showerror(this, rCode);
877
//        }
878
//
879
//    }
880
}
566
}

+ 32 - 54
app/src/main/res/layout/activity_search.xml

1
<?xml version="1.0" encoding="utf-8"?>
1
<?xml version="1.0" encoding="utf-8"?>
2
<com.zhy.autolayout.AutoLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2
<com.zhy.autolayout.AutoLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
    xmlns:tools="http://schemas.android.com/tools"
3
    android:layout_width="match_parent"
4
    android:layout_width="match_parent"
4
    android:layout_height="match_parent"
5
    android:layout_height="match_parent"
5
    android:background="@color/activity_bgcolor"
6
    android:background="@color/activity_bgcolor"
14
        <RelativeLayout
15
        <RelativeLayout
15
            android:layout_width="0dp"
16
            android:layout_width="0dp"
16
            android:layout_height="match_parent"
17
            android:layout_height="match_parent"
17
            android:layout_weight="5"
18
            android:layout_marginLeft="16dp"
18
            android:layout_marginTop="7dp"
19
            android:layout_marginTop="7dp"
19
            android:layout_marginBottom="7dp"
20
            android:layout_marginBottom="7dp"
20
            android:background="@drawable/bg"
21
            android:layout_marginLeft="16dp">
21
            android:layout_weight="5"
22
            android:background="@drawable/bg">
22

23

23

24

24
            <EditText
25
            <EditText
26
                android:layout_width="fill_parent"
27
                android:layout_width="fill_parent"
27
                android:layout_height="match_parent"
28
                android:layout_height="match_parent"
28
                android:layout_alignParentTop="true"
29
                android:layout_alignParentTop="true"
29
                android:layout_toRightOf="@+id/iv_sou"
30
                android:background="#f1f1f1"
31
                android:layout_marginTop="1dp"
30
                android:layout_marginTop="1dp"
31
                android:layout_marginRight="13dp"
32
                android:layout_marginBottom="1dp"
32
                android:layout_marginBottom="1dp"
33
                android:hint="请输入要查找的地名"
33
                android:layout_toRightOf="@+id/iv_sou"
34
                android:background="#f1f1f1"
34
                android:focusable="true"
35
                android:focusable="true"
36
                android:hint="请输入要查找的地名"
35
                android:imeOptions="actionSearch"
37
                android:imeOptions="actionSearch"
36
                android:singleLine="true"
38
                android:singleLine="true"
37
                android:textColorHint="@color/ui_68"
39
                android:textColorHint="@color/ui_68"
38
                android:textCursorDrawable="@drawable/search_biao"
40
                android:textCursorDrawable="@drawable/search_biao"
39
                android:textSize="14sp"
40
                android:layout_marginRight="13dp" />
41
                android:textSize="14sp" />
41

42

42
            <ImageView
43
            <ImageView
43
                android:id="@+id/iv_search_clear"
44
                android:id="@+id/iv_search_clear"
44
                android:layout_width="wrap_content"
45
                android:layout_width="wrap_content"
45
                android:layout_height="wrap_content"
46
                android:layout_height="wrap_content"
47
                android:layout_alignEnd="@+id/et_search"
48
                android:layout_alignRight="@+id/et_search"
49
                android:layout_centerVertical="true"
46
                android:contentDescription="@null"
50
                android:contentDescription="@null"
47
                android:padding="5dp"
51
                android:padding="5dp"
48
                android:src="@drawable/icon_edit_delete2_0"
49
                android:layout_centerVertical="true"
50
                android:layout_alignRight="@+id/et_search"
51
                android:layout_alignEnd="@+id/et_search" />
52
                android:src="@drawable/icon_edit_delete2_0" />
52

53

53
            <ImageView
54
            <ImageView
54
                android:id="@+id/iv_sou"
55
                android:id="@+id/iv_sou"
68
            android:layout_centerVertical="true"
69
            android:layout_centerVertical="true"
69
            android:contentDescription="@null"
70
            android:contentDescription="@null"
70
            android:gravity="center"
71
            android:gravity="center"
71
            android:paddingBottom="5dp"
72
            android:paddingLeft="16dp"
72
            android:paddingLeft="16dp"
73
            android:paddingRight="16dp"
74
            android:paddingTop="5dp"
73
            android:paddingTop="5dp"
74
            android:paddingRight="16dp"
75
            android:paddingBottom="5dp"
75
            android:text="取消"
76
            android:text="取消"
76
            android:textColor="@color/lvse"
77
            android:textColor="@color/lvse"
77
            android:textSize="15sp" />
78
            android:textSize="15sp" />
78

79

79
        <LinearLayout
80
        <LinearLayout
80
            android:id="@+id/ll_menu"
81
            android:id="@+id/ll_menu"
81
            android:background="@color/white"
82
            android:layout_width="match_parent"
82
            android:layout_width="match_parent"
83
            android:layout_height="50dp"
83
            android:layout_height="50dp"
84
            android:background="@color/white"
84

85

85
            android:orientation="vertical"
86
            android:orientation="vertical"
86
            android:visibility="gone">
87
            android:visibility="gone">
90
                android:layout_width="match_parent"
91
                android:layout_width="match_parent"
91
                android:layout_height="0dp"
92
                android:layout_height="0dp"
92
                android:layout_weight="1">
93
                android:layout_weight="1">
94

93
                <ImageView
95
                <ImageView
94
                    android:layout_width="0dp"
96
                    android:layout_width="0dp"
95
                    android:layout_height="match_parent"
97
                    android:layout_height="match_parent"
96
                    android:padding="5dp"
97
                    android:layout_weight="1"
98
                    android:layout_weight="1"
98
                    android:src="@drawable/icon_screening"/>
99
                    android:padding="5dp"
100
                    android:src="@drawable/icon_screening" />
99

101

100
                <TextView
102
                <TextView
101
                    android:id="@+id/address_search"
103
                    android:id="@+id/address_search"
102
                    android:layout_width="0dp"
104
                    android:layout_width="0dp"
103
                    android:layout_height="match_parent"
105
                    android:layout_height="match_parent"
104
                    android:layout_weight="6"
106
                    android:layout_weight="6"
105
                    android:text="找地名:"
106
                    android:gravity="center_vertical"
107
                    android:gravity="center_vertical"
107
                    android:paddingLeft="5dp"
108
                    android:paddingLeft="5dp"
108
                    android:textSize="18sp"
109
                    android:textColor="@color/text_light_grey"/>
109
                    android:text="找地名:"
110
                    android:textColor="@color/text_light_grey"
111
                    android:textSize="18sp" />
110
            </LinearLayout>
112
            </LinearLayout>
111

113

112
            <View
114
            <View
113
                android:layout_width="match_parent"
115
                android:layout_width="match_parent"
114
                android:layout_height="0.5dp"
116
                android:layout_height="0.5dp"
115
                android:background="@color/title_line"
117
                android:background="@color/title_line"
116
                android:visibility="gone"/>
118
                android:visibility="gone" />
117

119

118
            <LinearLayout
120
            <LinearLayout
119
                android:id="@+id/ll_zhan"
121
                android:id="@+id/ll_zhan"
126
                    android:layout_height="match_parent"
128
                    android:layout_height="match_parent"
127
                    android:layout_weight="1"
129
                    android:layout_weight="1"
128
                    android:padding="5dp"
130
                    android:padding="5dp"
129
                    android:src="@drawable/icon_screening"/>
131
                    android:src="@drawable/icon_screening" />
132

130
                <TextView
133
                <TextView
131
                    android:id="@+id/zhan_search"
134
                    android:id="@+id/zhan_search"
132
                    android:layout_width="0dp"
135
                    android:layout_width="0dp"
133
                    android:layout_height="match_parent"
136
                    android:layout_height="match_parent"
134
                    android:layout_weight="6"
137
                    android:layout_weight="6"
135
                    android:text="找电桩:"
136
                    android:gravity="center_vertical"
138
                    android:gravity="center_vertical"
137
                    android:paddingLeft="5dp"
139
                    android:paddingLeft="5dp"
138
                    android:textSize="18sp"
139
                    android:textColor="@color/text_light_grey"/>
140

140
                    android:text="找电桩:"
141
                    android:textColor="@color/text_light_grey"
142
                    android:textSize="18sp" />
141
            </LinearLayout>
143
            </LinearLayout>
142
        </LinearLayout>
144
        </LinearLayout>
143

145

144
    </com.zhy.autolayout.AutoLinearLayout>
146
    </com.zhy.autolayout.AutoLinearLayout>
147

145
    <View
148
    <View
146
        android:layout_width="match_parent"
149
        android:layout_width="match_parent"
147
        android:layout_height="0.5dp"
150
        android:layout_height="0.5dp"
148
        android:background="@color/ui_titleline"
151
        android:background="@color/ui_titleline"
149
        android:visibility="visible"/>
152
        android:visibility="visible" />
150

153

151
    <ListView
154
    <ListView
152
        android:id="@+id/lv_search_list"
155
        android:id="@+id/lv_search_list"
158
        android:divider="#dddddd"
161
        android:divider="#dddddd"
159
        android:dividerHeight="0.5dp"
162
        android:dividerHeight="0.5dp"
160
        android:fadingEdge="none"
163
        android:fadingEdge="none"
161
        android:listSelector="@color/transparent"/>
164
        android:listSelector="@color/transparent" />
162

165

163
    <ListView
166
    <ListView
164
        android:id="@+id/lv_history_list"
167
        android:id="@+id/lv_history_list"
170
        android:divider="#dddddd"
173
        android:divider="#dddddd"
171
        android:dividerHeight="0.5dp"
174
        android:dividerHeight="0.5dp"
172
        android:fadingEdge="none"
175
        android:fadingEdge="none"
173
        android:listSelector="@color/transparent"/>
174

175
    <!--<LinearLayout-->
176
    <!--android:id="@+id/ll_clear"-->
177
    <!--android:layout_width="match_parent"-->
178
    <!--android:layout_height="wrap_content"-->
179
    <!--android:orientation="vertical">-->
180

181
    <!--<View-->
182
    <!--android:layout_width="match_parent"-->
183
    <!--android:layout_height="0.5dp"-->
184
    <!--android:background="#dddddd"/>-->
185

186

187
    <!--<TextView-->
188
    <!--android:id="@+id/tv_historyClear"-->
189
    <!--android:layout_width="match_parent"-->
190
    <!--android:layout_height="45dp"-->
191
    <!--android:background="@color/white"-->
192
    <!--android:text="清除历史记录"-->
193
    <!--android:textColor="@color/hintColor"-->
194
    <!--android:gravity="center"-->
195
    <!--android:textSize="15sp"/>-->
196

197
    <!--</LinearLayout>-->
198

176
        android:listSelector="@color/transparent" />
199
</com.zhy.autolayout.AutoLinearLayout>
177
</com.zhy.autolayout.AutoLinearLayout>

+ 6 - 6
app/src/main/res/layout/item_search.xml

3
    android:layout_width="match_parent"
3
    android:layout_width="match_parent"
4
    android:layout_height="wrap_content"
4
    android:layout_height="wrap_content"
5
    android:background="@color/white"
5
    android:background="@color/white"
6
    android:orientation="vertical" >
6
    android:orientation="vertical">
7

7

8
    <RelativeLayout
8
    <RelativeLayout
9
        android:id="@+id/search_real"
9
        android:id="@+id/search_real"
10
        android:layout_width="wrap_content"
10
        android:layout_width="wrap_content"
11
        android:layout_height="wrap_content" >
11
        android:layout_height="wrap_content">
12

12

13
        <ImageView
13
        <ImageView
14
            android:id="@+id/search_image"
14
            android:id="@+id/search_image"
15
            android:layout_width="wrap_content"
15
            android:layout_width="wrap_content"
16
            android:layout_height="wrap_content"
16
            android:layout_height="wrap_content"
17
            android:layout_centerVertical="true"
17
            android:layout_centerVertical="true"
18
            android:layout_marginLeft="16dp"
18
            android:contentDescription="@null"
19
            android:contentDescription="@null"
19
            android:paddingRight="14dp"
20
            android:paddingTop="20dp"
20
            android:paddingTop="20dp"
21
            android:paddingRight="14dp"
21
            android:paddingBottom="20dp"
22
            android:paddingBottom="20dp"
22
            android:layout_marginLeft="16dp"
23
            android:src="@drawable/icon_search1117" />
23
            android:src="@drawable/icon_search1117" />
24

24

25
        <ImageView
25
        <ImageView
40
        android:layout_toRightOf="@id/search_real"
40
        android:layout_toRightOf="@id/search_real"
41
        android:gravity="center_vertical"
41
        android:gravity="center_vertical"
42
        android:orientation="vertical"
42
        android:orientation="vertical"
43
        android:paddingBottom="10dp"
43
        android:paddingTop="10dp"
44
        android:paddingRight="10dp"
44
        android:paddingRight="10dp"
45
        android:paddingTop="10dp" >
45
        android:paddingBottom="10dp">
46

46

47
        <TextView
47
        <TextView
48
            android:id="@+id/search_name"
48
            android:id="@+id/search_name"