设置搜索结果的意图



这里Heloo家伙正在开发一个具有自动建议和搜索功能的应用程序。问题是,当我在搜索字段中键入一个字母,建议仍然发生变化时,搜索中的项目会根据位置而不是实际的同义词类启动相同的意图。我如何在搜索条目上设置特定的意图,以便即使位置发生变化相同的活动由名称与搜索结果相对应的不同搜索条目启动第一张屏幕截图第二屏幕

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the search menu action bar.
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_search, menu);
// Get the search menu.
MenuItem searchMenu = menu.findItem(R.id.app_bar_menu_search);
searchView = (SearchView) MenuItemCompat.getActionView(searchMenu);
mSearchAutoComplete = (SearchView.SearchAutoComplete) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
mSearchAutoComplete.setThreshold(0);
mSearchAutoComplete.setBackgroundColor(getResources().getColor(R.color.tabcolor));
mSearchAutoComplete.setTextColor(Color.BLACK);
mSearchAutoComplete.setDropDownBackgroundResource(android.R.color.darker_gray);
/* Create a new ArrayAdapter and add data to search auto complete object.
how can i set each word when onclicked to open the corresponding class no matter the position
*/
String dataArr[] = {"Kiambu county", "Kisumu county", "Kitui county", "Laikipia county", "Lamu county", "Meru county", "Mombasa county", "Muranga county", "Nairobi county", "Nakuru county", "Narok county", "kajiado county", "Uansingishu county", "Makueni County", "Machakos county"};
ArrayAdapter<String> newsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, dataArr);
mSearchAutoComplete.setAdapter(newsAdapter);
// Listen to search view item on click event.
mSearchAutoComplete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (i == 0) {
Intent intent = new Intent(County.this, Kiambu.class);
startActivity(intent);
}
if (i == 1) {
Intent intent = new Intent(County.this, Kisumu.class);
startActivity(intent);
}
if (i == 2) {
Intent intent = new Intent(County.this, Kitui.class);
startActivity(intent);
}
if (i == 3) {
Intent intent = new Intent(County.this, Laikipia.class);
startActivity(intent);
}
if (i == 4) {
Intent intent = new Intent(County.this, Lamu.class);
startActivity(intent);
}
if (i == 5) {
Intent intent = new Intent(County.this, Meru.class);
startActivity(intent);
}
if (i == 6) {
Intent intent = new Intent(County.this, Mombasa.class);
startActivity(intent);
}
if (i == 7) {
Intent intent = new Intent(County.this, Muranga.class);
startActivity(intent);
}
if (i == 8) {
Intent intent = new Intent(County.this, Nairobi.class);
startActivity(intent);
}
if (i == 9) {
Intent intent = new Intent(County.this, Nakuru.class);
startActivity(intent);
}
if (i == 10) {
Intent intent = new Intent(County.this, Narok.class);
startActivity(intent);
}
if (i == 11) {
Intent intent = new Intent(County.this, Kajiado.class);
startActivity(intent);
}
if (i == 12) {
Intent intent = new Intent(County.this, Singishu                                                                                                                                                                                                                                                                                                                                                                .class);
startActivity(intent);
}
if (i == 13) {
Intent intent = new Intent(County.this, Makueni.class);
startActivity(intent);
}
if (i == 14) {
Intent intent = new Intent(County.this, Machakos.class);
startActivity(intent);
}
}
});

return super.onCreateOptionsMenu(menu);
}

帮我解决问题

问题是您添加了基于i的条件,该条件给出了所单击适配器项的当前位置。

所以,当你搜索某个东西时,它会过滤数据并相应地显示它,当你点击第一个项目时,它总是会打开Kiambu类。

你需要在onItemClick方法中更新你的条件,比如:

if (newsadapter.getItem(i).equals(dataArr[0])) {
Intent intent = new Intent(County.this, Kiambu.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[1])) {
Intent intent = new Intent(County.this, Kisumu.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[2])) {
Intent intent = new Intent(County.this, Kitui.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[3])) {
Intent intent = new Intent(County.this, Laikipia.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[4])) {
Intent intent = new Intent(County.this, Lamu.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[5])) {
Intent intent = new Intent(County.this, Meru.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[6])) {
Intent intent = new Intent(County.this, Mombasa.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[7])) {
Intent intent = new Intent(County.this, Muranga.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[8])) {
Intent intent = new Intent(County.this, Nairobi.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[9])) {
Intent intent = new Intent(County.this, Nakuru.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[10])) {
Intent intent = new Intent(County.this, Narok.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[11])) {
Intent intent = new Intent(County.this, Kajiado.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[12])) {
Intent intent = new Intent(County.this, Singishu.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[13])) {
Intent intent = new Intent(County.this, Makueni.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[14])) {
Intent intent = new Intent(County.this, Machakos.class);
startActivity(intent);
}

最新更新