我试图让搜索框在 Action Bar Sherlock 上工作。
这是我的PreLocationActivity
:
@ContentView(R.layout.search)
public class PreLocationActivity extends RoboSherlockActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.map_layout);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//Used to put dark icons on light action bar
menu.add("Search")
.setIcon(R.drawable.ic_search_inverse)
.setActionView(R.layout.collapsible_edittext)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
return true;
}
@Override
public boolean onSearchRequested() {
return super.onSearchRequested();
}
}
这是我的SearchableActivity
:
@ContentView(R.layout.search)
public class SearchableActivity extends RoboSherlockFragmentActivity {
@InjectView(R.id.addressListView) ListView addressView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doGeoSearch(query);
}
}
public void doGeoSearch(String query){
Geocoder geocoder;
ArrayList<Address> addresses;
ArrayList<String> address = new ArrayList<String>() ;
geocoder = new Geocoder(this, Locale.getDefault());
try {
addresses = (ArrayList<Address>) geocoder.getFromLocationName(query, 6);
Log.d("Address",String.valueOf(addresses));
for(int i = 0;i<addresses.size();i++)
{
String addr = new String();
addr.concat(addresses.get(i).getAddressLine(0));
addr.concat(addresses.get(i).getAddressLine(1));
addr = addresses.get(i).getAddressLine(0) + addresses.get(i).getLocality() + addresses.get(i).getAdminArea();
//addr.concat(addresses.get(i).getAddressLine(2));
Log.d("addr",addr);
address.add(addr);
}
SearchAddressAdapater addressList = new SearchAddressAdapater(getApplicationContext(),R.layout.search_list,addresses, SearchableActivity.this);
addressView.setAdapter(addressList);
//ListView addressListView = new ListView();
} catch (IOException e) {
//Handle exception
}
}
根本没有成功。例如,当我在 Prelocation Activity 上键入内容并按 Enter 键时,没有搜索任何内容。我是否必须将其视为一个EditText
并为它编写一个文本侦听器,然后调用它geoCoder
并获取我的位置,或者是否有更聪明的方法来解决它?