Search code examples
androidlistviewsearchview

Opening another activity after getting search results


I am using searchview in my application. I am having a problem after getting the search results to go to different activities. Here is the class that shows the listview populated with items. If the user decides to not search the items they can just click an item and it leads them to the other activity.

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);
}

}

This is the activity that displays the search results. If the user decides to search lets say "math" then it will show a listview only showing math.

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;

                }
            });
}
}

The problem I am having is since I am using switch statements when clicking on items it depends on the position. So if I search "math" it will be in position zero and if I click on it, it leads me to the art activity. It needs to lead to the math activity not the art activity. And if I search science it will be in position zero. Then if I click science it will lead me to the art activity not the science activity. How can I go to different activities from the search results?


Solution

  • Every time you search a keyword like math, it will come at 0 postion,right. And you have a switch condition that checks if postion is zero then pass Intent to Art.class

     switch(position){
                        case 0:
                            Intent art = new Intent(ShowCourses.this,art.class);
                            startActivity(art);
                            break;
    

    Now,irrespective of whatever you search, if it is at position 0 let it be any item fro your listview,on click will redirect to art Activity.

    So, for this issue what we can do is first check the type of element and then redirect to other page on the basis of that.For example :-

     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);
                            }
    
            });