@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_choose_city); m_myApplication = MyApplication.getInstance(); mBackbtn = (ImageView) findViewById(R.id.title_back); mBackbtn.setOnClickListener(this); mCityNameTv = (TextView) findViewById(R.id.title_name); SharedPreferences sharedPreferences = getSharedPreferences("config", MODE_PRIVATE); String cityName = sharedPreferences.getString("main_city_name", "北京"); // 缺省值为北京 mCityNameTv.setText("当前城市:" + cityName); mEditText = (EditText) findViewById(R.id.search_edit); mEditText.addTextChangedListener(mTextWatcher); mSearchList = new ArrayList<City>(); mSearchList = m_myApplication.getAllCityList(); String[] data = new String[mSearchList.size()]; for (int i = 0, j = mSearchList.size(); i < j; i++) { data[i] = mSearchList.get(i).getProvince() + "-" + mSearchList.get(i).getCity(); } mlistView = (ListView) findViewById(R.id.city_list_view); ArrayAdapter<String> adapter; adapter = new ArrayAdapter<String>(ChooseCity.this, R.layout.drawer_list_style, data); mlistView.setAdapter(adapter); mlistView.setOnItemClickListener( new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // Toast.makeText(ChooseCity.this, "You have selected // "+position,Toast.LENGTH_LONG).show(); City city = mSearchList.get(position); String returnCityNumber = city.getNumber(); String returnCityName = city.getCity(); // 点击后切换到指定城市 SharedPreferences sharedPreferences = getSharedPreferences("config", MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString("main_city_code", returnCityNumber); editor.putString("main_city_name", returnCityName); editor.commit(); Intent intent = new Intent(); intent.putExtra("CityNumber", returnCityNumber); intent.putExtra("CityName", returnCityName); setResult(RESULT_OK, intent); finish(); } }); }
@Override public void onTextChanged(CharSequence s, int start, int before, int count) { String searchKey = s.toString(); mSearchList = m_myApplication.showCurCityList(searchKey); String[] data = new String[mSearchList.size()]; for (int i = 0, j = mSearchList.size(); i < j; i++) { data[i] = mSearchList.get(i).getProvince() + "-" + mSearchList.get(i).getCity(); } ArrayAdapter<String> adapter; adapter = new ArrayAdapter<String>(ChooseCity.this, R.layout.drawer_list_style, data); // adapter = new // ArrayAdapter<String>(ChooseCity.this,android.R.layout.simple_expandable_list_item_1,data); mlistView.setAdapter(adapter); }