0

I have a Listview which displays the names of the flowers which are fetched from a URL (JSON data). I have made a ArrayList of the type class "Flower" in which I have saved all the flowers information which is there in the JSON data. I have added the SearchView in the app. Its properly configured in the application but when I enter some text inside it and click on the search button in the keyboard of mobile then there is no action done after that.

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.titlebar, menu);
        SearchManager searchManager=(SearchManager)getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView=(SearchView)menu.findItem(R.id.new_search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this,FrontPage.class)));


        return  true;
    }

This is my SearchResultActivity.java file.

package com.example.hsports.flowers;

import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;

public class SearchResultsActivity extends AppCompatActivity {

    FrontPage frontPage=new FrontPage();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_results);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        handleIntent(getIntent());


        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        handleIntent(getIntent());

    }


    private void handleIntent(Intent intent) {

        TextView name=(TextView)findViewById(R.id.displayName);
        TextView url=(TextView)findViewById(R.id.displayUrl);


        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);

            for(Flower f:frontPage.flowersList)
            {
                if(f.name.equalsIgnoreCase(query))
                {

                    name.setText(f.name);
                    url.setText(f.url);

                }
            }



        }
    }
}

I have configured this activity in the androidmanifest.xml file as:

 <activity
            android:name=".SearchResultsActivity"
            android:label="@string/title_activity_search_results"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>


        </activity>

When I am clicking on the searchview icon and entering some text and pressing enter from keyboard , its not giving me any result. I have one more doubt , where will the flow go after entering query into the searchView ?

shikher.mishra
  • 155
  • 8
  • 24

1 Answers1

1

You are missing android:launchMode="singleTop"in the activity level of your manifest

EDIT: You also need to create an searchable.xml file in an xml directory with the following code

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
            android:label="@string/app_name"
            android:hint="Search" />

then in your manifest for the same activity add the following meta-data

<activity   android:name=".SearchResultsActivity"
            android:label="@string/title_activity_search_results"
            android:theme="@style/AppTheme.NoActionBar"
            android:launchMode="singleTop">
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" /> 
            </intent-filter>
            <meta-data android:name="android.app.searchable"
                       android:resource="@xml/searchable" />
        </activity>
Ivan Leong
  • 118
  • 2
  • 10
  • 1
    can you please tell me the flow when I type in a query to the searchView. Where does it goes after this? – shikher.mishra Nov 16 '16 at 09:18
  • 1
    I have added all those things which you have suggested above, but its not working. – shikher.mishra Nov 16 '16 at 09:21
  • 1
    @shikher.mishra assuming it all works. You should be able to grab the string query from `query` in `handleIntent`. You also need to add `setIntent(intent)` in `onNewIntent` – Ivan Leong Nov 16 '16 at 09:39
  • I want to pass a ArrayList from this searchView . Can I pass so that in other activity SearchresultActivity I can compare that query with the elements present in the ArrayList? – shikher.mishra Nov 16 '16 at 09:41
  • can you not access this ArrayList statically in SearchResultActivity? – Ivan Leong Nov 16 '16 at 09:44
  • See this @NPike at https://stackoverflow.com/a/15735869/2449314 basically, do not hard code the strings in the xml file – Tanner Summers Sep 23 '17 at 05:30