我在我的应用程序中使用searchview。在将搜索结果转到不同的活动后,我遇到了问题。这是显示填充了项目的列表视图的类。如果用户决定不搜索项目,他们只需点击一个项目,就可以将其引导到其他活动。
public class ShowCourses extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_courses);
Toolbar courseToolbar = (Toolbar) findViewById(R.id.courses_toolbar);
setSupportActionBar(courseToolbar);
getSupportActionBar().setTitle(R.string.courses_toolbar);
String[] subjects = {"Art", "Math", Science"};
ListAdapter subjectsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subjects);
ListView DisplaySubjects = (ListView) findViewById(R.id.DisplaySubjects);
DisplaySubjects.setAdapter(subjectsAdapter);
DisplaySubjects.setOnItemClickListener(
new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
switch(position){
case 0:
Intent art = new Intent(ShowCourses.this, art.class);
startActivity(art);
break;
case 1:
Intent math = new Intent(ShowCourses.this, math.class);
startActivity(math);
break;
case 2:
Intent science = new Intent(ShowCourses.this, science.class);
startActivity(science);
break;
default:
break;
}
}
});
}
@Override
protected void onResume(){
super.onResume();
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.menu_main, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
}
这是显示搜索结果的活动。如果用户决定搜索,请说'&34;数学&#34;然后它会显示一个仅显示数学的列表视图。
public class CoursesSearchResults extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_courses_search_results);
Toolbar courseToolbar = (Toolbar) findViewById(R.id.searchCourses_toolbar); //Creates Toolbar
setSupportActionBar(courseToolbar);
String query = new String();
final Intent searchIntent = getIntent();
if(Intent.ACTION_SEARCH.equals(searchIntent.getAction())){
query = searchIntent.getStringExtra(SearchManager.QUERY);
getSupportActionBar().setTitle(query);
Toast.makeText(CoursesSearchResults.this, query, Toast.LENGTH_SHORT).show();
}
String[] subjects = {"Art", "Math", Science"};
final ArrayList<String> searchResults = new ArrayList<String>();
for(int i = 0; i < subjects.length; i++) {
if (subjects[i].toLowerCase().contains(query.toLowerCase())) {
searchResults.add(subjects[i]);
}
}
ListView courses = (ListView) findViewById(R.id.courses_search);
ListAdapter coursesAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, searchResults);
courses.setAdapter(coursesAdapter);
courses.setOnItemClickListener(
new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
switch(position){
case 0:
Intent art = new Intent(ShowCourses.this, art.class);
startActivity(art);
break;
case 1:
Intent math = new Intent(ShowCourses.this, math.class);
startActivity(math);
break;
case 2:
Intent science = new Intent(ShowCourses.this, science.class);
startActivity(science);
break;
default:
break;
}
});
}
}
我遇到的问题是因为我在点击项目时使用switch语句取决于位置。所以,如果我搜索&#34;数学&#34;它将位于零位置,如果我点击它,它会引导我进入艺术活动。它需要导致数学活动而不是艺术活动。如果我搜索科学,它将处于零位。然后,如果我点击科学,它将引导我进行艺术活动,而不是科学活动。如何从搜索结果中进行不同的活动?
答案 0 :(得分:1)
每当你搜索像数学这样的关键词时,它就会出现在0位置,对吧。 并且您有一个切换条件,检查postion是否为零,然后将Intent传递给Art.class
switch(position){
case 0:
Intent art = new Intent(ShowCourses.this,art.class);
startActivity(art);
break;
现在,无论您搜索什么,如果它位于0位置,请将其作为列表视图中的任何项目,点击后将重定向到艺术活动。
因此,对于这个问题,我们可以做的是首先检查元素的类型,然后在此基础上重定向到其他页面。例如: -
courses.setOnItemClickListener(
new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
String sub = parent.getItemAtPosition(int position).toString();
if(sub.equals(subjects[0])){
Intent art = new Intent(ShowCourses.this, art.class);
startActivity(art);
}else if(sub.equals(subjects[1])){
Intent math = new Intent(ShowCourses.this, math.class);
startActivity(math);
}else if(sub.equals(subjects[2])){
Intent science = new Intent(ShowCourses.this, science.class);
startActivity(science);
}
});