0

First of all sorry if im new to android and java and still want to implement something for my thesis regarding the app im creating, I just want it to have a search view in the action bar for a certain activity to search for the corresponding letters or words in the search for my custom listview... and I dont know what to put in the "doMySearch(String query)" to get the results.

SearchResultsActivity.java

  import android.app.ActionBar;
  import android.app.ListActivity;
  import android.app.SearchManager;
  import android.content.Intent;
  import android.os.Bundle;
  import android.widget.TextView;

  public class SearchResultsActivity extends ListActivity {

private TextView txtQuery;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search_results);

    // get the action bar
    ActionBar actionBar = getActionBar();

    // Enabling Back navigation on Action Bar icon
    actionBar.setDisplayHomeAsUpEnabled(true);

    txtQuery = (TextView) findViewById(R.id.txtQuery);

    handleIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {
    setIntent(intent);
    handleIntent(intent);
}

/**
 * Handling intent data
 */
private void handleIntent(Intent intent) {
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);

        doMySearch(query);
        txtQuery.setText("Search Query: " + query);
    }
}

private void doMySearch(String query) {
    // TODO Auto-generated method stub
       ArrayAdapter<String> adapter =new ArrayAdapter<String>(this, R.layout.subject_list_layout, R.id.subject_course_title_row);
    return adapter;

}
}

Searchable.xml

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

Manifest.xml

        <!-- Sample Search -->
    <activity
        android:name="com.randomlyroaming.com.loaopencourseware.courses.BSIT"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="stateHidden" >
        <meta-data
            android:name="android.app.default_searchable"
            android:value=".SearchResultsActivity" />
    </activity>

    <!-- Search results activity -->
    <activity
        android:name=".SearchResultsActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>

        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>

Please Help... It is already redirecting to "txtQuery.setText("Search Query: " + query);" but no results... Do I need to put something the method "doMySearch(query);" what code can I put it in there for it to work?..

Thank you and Your help is much Appreciated...

user3438885
  • 79
  • 1
  • 5
  • `doMySearch()` does not have any code in it. Nothing will happen. Does the `TextView` show what you typed in the Search box? Also, what does `setIntent()` do? can you show that method – Luis Lavieri Apr 18 '14 at 20:01
  • yes it show what I type in the search box, my problem is I dont know what code I should put in the doMysearch because I really can't find any tutorial regarding to it, I have my customListview and adapter and I dont know how to code it there.. Sorry for being a newbie sir luis – user3438885 Apr 18 '14 at 20:03
  • Could you be a little more specific of what you want? What is in the adapter? Do you have an array populated in that `ListView`? do you want to change the adapter while you search the word? – Luis Lavieri Apr 18 '14 at 20:07
  • I think [this](http://rakhi577.wordpress.com/2012/06/26/buttons-on-list-view-with-easy-searching-in-android/) is what you are looking for. [Here is](http://abdennour-insat.blogspot.com/2012/05/listview-textwatcher-autocomplete.html) another tutorial. – Luis Lavieri Apr 18 '14 at 20:09
  • Hi Sir Luis, I have an array that already populated from a list view. and what I want is when I search for a certain word it should populate what is similar to the word you type in the search action bar... for example. I am looking to a listview right now with 50 names of school subjects, then I go click the magnifier to go for a search of names of school subjects, when i press enter I should get similar names that i typed in listview order... Sorry i hope i make sense... – user3438885 Apr 18 '14 at 20:19
  • I think you are trying to filter those words. Take a look at my answer. I provided a good tutorial for doing that. Good luck in your project! – Luis Lavieri Apr 18 '14 at 20:20

1 Answers1

0

but no results... Do I need to put something the method "doMySearch(query);

Yes! it isn't supposed to show anything because you are just printing the query into a TextView.

If you want to access an SQLite Database, change a ListView's adapter, or do something else, you would have to implement it in that method.

Going back to what you want, I think you are looking to filter the words in the ListView using the query, am I right? Anyway, Here you have a nice tutorial for that. Also, this post may help as you well.

Community
  • 1
  • 1
Luis Lavieri
  • 4,064
  • 6
  • 39
  • 69