Home How to pass data to activity opened from SearchView VoiceSearchButton?
Reply: 0

How to pass data to activity opened from SearchView VoiceSearchButton?

user6317
1#
user6317 Published in July 21, 2018, 1:49 pm

I am using 4 lists in my application on which I'm performing search operation. In the activity where I have the SearchView, I'm loading ListView with the list name which I get from intent from previous Activity. Now when I press the voice button it opens another instance of the activity with the default list. I want to pass list name on which I was operating with to the activity opened by this voice search.

Here is the code of activity containing my searchView.

public class SearchActivity extends AppCompatActivity implements 
LoaderManager.LoaderCallbacks<Cursor>,SearchView.OnQueryTextListener {

ListView searchedLv;
String[] projection;
MyCursorAdapter adapter;
String toSearchWord="a";
SearchView searchView;
MenuItem item;
Intent intent;
String selection;
String [] selectionArgs;
String whichList;
Uri tableUri;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search);
    projection = new String[]{
            DatabaseSchemas.CompleteTableSchema._ID,
            DatabaseSchemas.CompleteTableSchema.SYNONYMS_STRING,
            DatabaseSchemas.CompleteTableSchema.Meaning,
            DatabaseSchemas.CompleteTableSchema.Word
    };
    intent=getIntent();
    whichList=intent.getStringExtra("List");
    if(whichList==null)
    {
        whichList="Complete";
    }
    selection="Word like ? and Word!=? and Meaning!=? and Word!=? and Meaning!=?";
    selectionArgs=new String[]{toSearchWord+"%","---","---","----","----"};
    if(whichList.equals("Complete"))
    {
        projection = new String[]{
                DatabaseSchemas.CompleteTableSchema._ID,
                DatabaseSchemas.CompleteTableSchema.SYNONYMS_STRING,
                DatabaseSchemas.CompleteTableSchema.Meaning,
                DatabaseSchemas.CompleteTableSchema.Word
        };
        tableUri=DatabaseSchemas.CompleteTableSchema.CONTENT_URI;
        selection="Word like ? and Word!=? and Meaning!=? and Word!=? and Meaning!=?";
        selectionArgs=new String[]{toSearchWord+"%","---","---","----","----"};
    }
    else
    {
        projection = new String[]{
                DatabaseSchemas.SearchedTableSchema.SYNONYMS_STRING,
                DatabaseSchemas.SearchedTableSchema.MEANING,
                DatabaseSchemas.SearchedTableSchema.HAS_NOTES,
                DatabaseSchemas.SearchedTableSchema.LEARNED,
                DatabaseSchemas.SearchedTableSchema.Marked,
                DatabaseSchemas.SearchedTableSchema.Word,
                DatabaseSchemas.SearchedTableSchema._ID,
                DatabaseSchemas.SearchedTableSchema.NumberOfTimesSearched,
                DatabaseSchemas.SearchedTableSchema.Notes
        };
        tableUri=DatabaseSchemas.SearchedTableSchema.CONTENT_URI;
        if(!whichList.equals("Searched")) {
            selection = "Word like ? and Word!=? and Meaning!=? and Word!=? and Meaning!=? and " + whichList + "=?";
            selectionArgs = selectionArgs = new String[]{toSearchWord + "%", "---", "---", "----", "----", "1"};
            if (whichList.equals(DatabaseSchemas.SearchedTableSchema.Notes)) {
                selection = "Word like ? and Word!=? and Meaning!=? and Word!=? and Meaning!=? and " + whichList + " is not null and " + whichList + "!=?";
                selectionArgs = new String[]{toSearchWord + "%", "---", "---", "----", "----", "0"};
            }
        }
    }
    //searchView=(SearchView) item.getActionView();
    searchedLv=(ListView) findViewById(R.id.searchResultsLvId);
    adapter=new MyCursorAdapter(this,null);
    searchedLv.setAdapter(adapter);
    getSupportLoaderManager().initLoader(1,null, this);
    searchedLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            String selected=((TextView) view.findViewById(R.id.name)).getText().toString();
            Intent intent=new Intent(getApplicationContext(),mainClusterActivity.class);
            intent.putExtra("Word",selected);
            startActivity(intent);
        }
    });
}


@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        if(whichList.equals(DatabaseSchemas.SearchedTableSchema.Notes))
            selectionArgs = new String[]{toSearchWord + "%", "---", "---", "----", "----", "0"};
        else{
            if(whichList.equals("Searched")||whichList.equals("Complete"))
                selectionArgs = new String[]{toSearchWord + "%", "---", "---", "----", "----"};
            else
            selectionArgs = new String[]{toSearchWord + "%", "---", "---", "----", "----", "1"};
        }
    return new CursorLoader(this,tableUri,projection,selection,selectionArgs, DatabaseSchemas.SearchedTableSchema._ID);
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    adapter.swapCursor(data);
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
    adapter.swapCursor(null);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater=getMenuInflater();
    inflater.inflate(R.menu.search_menu,menu);
     item=menu.findItem(R.id.searchWordsItemId);
    searchView=(SearchView) item.getActionView();
    searchView.setQueryHint("Search from Complete List");
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(false);
    //Intent intent = getIntent();
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        toSearchWord = intent.getStringExtra(SearchManager.QUERY);
        searchView.setQuery(toSearchWord,false);
        getSupportLoaderManager().restartLoader(1,null,this);
    }
    searchView.setOnQueryTextListener(this);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onQueryTextSubmit(String s) {
    return false;
}

@Override
protected void onPause() {
    super.onPause();
    finish();
}

@Override
public boolean onQueryTextChange(String s) {
   toSearchWord=s;
   getSupportLoaderManager().restartLoader(1,null,this);
    return false;
}
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.310918 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO