0
您好,我有一個搜索視圖來過濾短列表中的結果。我目前使用整數傳遞到下一個活動來加載需要顯示的內容。但是,如果您搜索,則您搜索的項目現在位於第0個位置,即第一個項目所在的位置。那麼,如何爲此設置一個唯一的ID系統,或者我可以爲此調用一個方法嗎?帶有ListView的SearchView更改物品位置,需要其他活動的確切物品ID
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_clinics);
listView = (ListView) findViewById(R.id.listView);
final ArrayList<String> clinicInfoArrayList = new ArrayList<String>();
clinicInfoArrayList.add("Phoenix Urgent Care \n 3931 E Camelback Rd, Phoenix, AZ 85018");
clinicInfoArrayList.add("Pain Stop North Phoenix Clinic \n 3329 East Bell Road, Suite A2-A5, Phoenix, AZ 85032");
clinicInfoArrayList.add("The Little Clinic \n 2727 W Bell Rd, Phoenix, AZ 85053");
clinicInfoArrayList.add("Native Health \n Building C, 4041 N Central Ave, Phoenix, AZ 85012");
clinicInfoArrayList.add("Allied Clinic, PLLC \n 1147 E Glendale Ave, Phoenix, AZ 85020");
clinicInfoArrayList.add("Neighborhood Clinic \n 1929 W Fillmore St, Phoenix, AZ 85009");
clinicInfoArrayList.add("MinuteClinic \n 4742 E Indian School Rd, Phoenix, AZ 85018");
mSearchView = (SearchView) findViewById(R.id.menu_search);
listView.setTextFilterEnabled(true);
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
clinicInfoArrayList);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3) {
Intent intent = new Intent(SearchClinics.this,ClinicInfoView.class);
int something = (int)arrayAdapter.getItemId(position);
intent.putExtra("Another", something);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_menu, menu);
SearchManager searchManager = (SearchManager)
getSystemService(Context.SEARCH_SERVICE);
MenuItem searchMenuItem = menu.findItem(R.id.menu_search);
mSearchView = (SearchView) searchMenuItem.getActionView();
mSearchView.setSearchableInfo(searchManager.
getSearchableInfo(getComponentName()));
mSearchView.setIconifiedByDefault(false);
mSearchView.setOnQueryTextListener(this);
mSearchView.setSubmitButtonEnabled(true);
mSearchView.setQueryHint("Search Here");
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText)) {
listView.clearTextFilter();
} else {
listView.setFilterText(newText.toString());
}
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
return true;
}
傳遞你的對象,而不是itemId,我想getItemId()只是返回項目的位置。您可以從列表中獲取對象,'onItemClick'內的'clinicInfoArrayList.get(position)'' –
問題是,position是一個數字,當文本從SearchView中過濾時會發生變化。因此,例如在列表視圖項目編號5中,如果現在搜索的是基於位置值的項目編號0,因爲列表視圖僅顯示搜索到的一個。所以這個位置對這些都沒有幫助,因爲它總是會給出錯誤的。 –
如果診所的名稱是唯一的,循環,檢查名稱匹配,然後你得到的對象。 –