Add search functionality

* AndroidManifest.xml: Register `DVDroidActivity' as searchable.

* res/drawable-hdpi/menu_search.png:
* res/drawable-mdpi/menu_search.png: New icons.

* res/menu/main_activity.xml: Add a search icon to the main activity's
  action menu.

* res/values/strings.xml: Add strings "search_hint" and "menu_search".

* res/xml/searchable.xml: New configuration file.

* src/ryuslash/org/dvdroid/DVDDataSource.java (getQuery): New
  function.

* src/ryuslash/org/dvdroid/DVDroidActivity.java (getData): New
  overload, calls `getQuery' to search for something instead of
  everything.
  (onCreate): If a search has been started, search, don't just show
  everything.
  (onCreateOptionsMenu): Instantiate the `SearchView' in order to use
  it.
This commit is contained in:
Tom Willemsen 2012-08-16 21:45:49 +02:00
parent f30b4fcd3a
commit b2a1b4aec0
8 changed files with 45 additions and 1 deletions

View file

@ -10,7 +10,10 @@
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.SEARCH" />
</intent-filter> </intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity> </activity>
<activity android:name="AddDVDActivity" /> <activity android:name="AddDVDActivity" />
</application> </application>

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

View file

@ -4,4 +4,9 @@
android:title="@string/main_menu_add" android:title="@string/main_menu_add"
android:showAsAction="ifRoom|withText" android:showAsAction="ifRoom|withText"
android:icon="@drawable/add" /> android:icon="@drawable/add" />
<item android:id="@+id/menu_search"
android:title="@string/menu_search"
android:icon="@drawable/menu_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
</menu> </menu>

View file

@ -1,6 +1,8 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<string name="app_name">DVDroid</string> <string name="app_name">DVDroid</string>
<string name="search_hint">Search DVDs</string>
<string name="menu_search">Search</string>
<string name="name_entry">Title...</string> <string name="name_entry">Title...</string>
<string name="wishlist_entry">On wishlist</string> <string name="wishlist_entry">On wishlist</string>
<string name="watched_entry">Watched</string> <string name="watched_entry">Watched</string>

5
res/xml/searchable.xml Normal file
View file

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

View file

@ -142,6 +142,15 @@ public class DVDDataSource
return getListFromCursor(cursor); return getListFromCursor(cursor);
} }
public List<DVD> getQuery(String query)
{
String params[] = { "%" + query + "%" };
Cursor cursor = database.query(SQLiteHelper.TABLE_DVD,
allColumns, "title LIKE ?",
params, null, null, "title");
return getListFromCursor(cursor);
}
private DVD cursorToDVD(Cursor cursor) private DVD cursorToDVD(Cursor cursor)
{ {
DVD dvd = new DVD(); DVD dvd = new DVD();

View file

@ -3,6 +3,7 @@ package ryuslash.org.dvdroid;
import java.util.List; import java.util.List;
import android.app.ListActivity; import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
@ -14,6 +15,7 @@ import android.view.View;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ListView; import android.widget.ListView;
import android.widget.SearchView;
public class DVDroidActivity extends ListActivity public class DVDroidActivity extends ListActivity
implements ActionMode.Callback implements ActionMode.Callback
@ -28,6 +30,11 @@ public class DVDroidActivity extends ListActivity
return datasource.getCollection(); return datasource.getCollection();
} }
private List<DVD> getData(String query)
{
return datasource.getQuery(query);
}
@Override @Override
protected void onActivityResult(int requestCode, protected void onActivityResult(int requestCode,
int resultCode, int resultCode,
@ -80,7 +87,14 @@ public class DVDroidActivity extends ListActivity
datasource = new DVDDataSource(this); datasource = new DVDDataSource(this);
datasource.open(); datasource.open();
List<DVD> values = getData(); Intent intent = getIntent();
List<DVD> values;
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
values = getData(intent.getStringExtra(SearchManager.QUERY));
} else {
values = getData();
}
ArrayAdapter<DVD> adapter = ArrayAdapter<DVD> adapter =
new ArrayAdapter<DVD>(this, new ArrayAdapter<DVD>(this,
@ -104,6 +118,12 @@ public class DVDroidActivity extends ListActivity
{ {
MenuInflater inflater = getMenuInflater(); MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity, menu); inflater.inflate(R.menu.main_activity, menu);
// Get the SearchView and set the searchable configuration.
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true; return true;
} }