#19 Local Search

This commit is contained in:
Stefan Niedermann 2016-02-03 14:58:44 +01:00
parent 1a0066d897
commit 04f766d822
8 changed files with 96 additions and 9 deletions

View file

@ -77,6 +77,7 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/pre-dexed" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/release" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />

View file

@ -6,7 +6,7 @@ android {
defaultConfig {
applicationId "it.niedermann.owncloud.notes"
minSdkVersion 10
minSdkVersion 11
targetSdkVersion 23
versionCode 8
versionName "0.6.0"

View file

@ -6,7 +6,7 @@
android:versionName="0.6.0">
<uses-sdk
android:minSdkVersion="10"
android:minSdkVersion="11"
android:targetSdkVersion="23" />
<uses-permission android:name="android.permission.INTERNET" />
@ -28,7 +28,15 @@
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.SEARCH"/>
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable"/>
<meta-data
android:name="android.app.default_searchable"
android:value="it.niedermann.owncloud.notes.android.activity.NotesListViewActivity"/>
</activity>
<activity
android:name="it.niedermann.owncloud.notes.android.activity.NoteActivity"

View file

@ -1,14 +1,17 @@
package it.niedermann.owncloud.notes.android.activity;
import android.app.SearchManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.view.ActionMode;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
@ -44,6 +47,7 @@ public class NotesListViewActivity extends AppCompatActivity implements
private ActionMode mActionMode;
private SwipeRefreshLayout swipeRefreshLayout = null;
private NoteSQLiteOpenHelper db = null;
private SearchView searchView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -188,8 +192,40 @@ public class NotesListViewActivity extends AppCompatActivity implements
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_list_view, menu);
// Associate searchable configuration with the SearchView
final MenuItem item = menu.findItem(R.id.search);
searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
search(newText);
return true;
}
});
return true;
}
private void search(String query) {
if (query.length() > 0) {
setListView(db.searchNotes(query));
} else {
setListView(db.getNotes());
}
listView.scrollToPosition(0);
}
@Override
protected void onNewIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
search(intent.getStringExtra(SearchManager.QUERY));
}
super.onNewIntent(intent);
}
/**
* Handels click events on the Buttons in the Action Bar.
@ -352,6 +388,7 @@ public class NotesListViewActivity extends AppCompatActivity implements
}
mode.finish(); // Action picked, so close the CAB
//after delete selection has to be cleared
searchView.setIconified(true);
setListView(db.getNotes());
return true;
default:

View file

@ -1,6 +1,7 @@
package it.niedermann.owncloud.notes.model;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -88,14 +89,18 @@ public class ItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
}
public boolean deselect(Integer position) {
Log.v("Note", "Position: " + position);
for (int i = 0; i < selected.size(); i++) {
Log.v("Note", "Compare: " + selected.get(i) + " == " + position);
if (selected.get(i) == position) {
//position was selected and removed
selected.remove(i);
Log.v("Note", "Return: true");
return true;
}
}
// position was not selected
Log.v("Note", "Return: false");
return false;
}

View file

@ -146,7 +146,7 @@ public class NoteSQLiteOpenHelper extends SQLiteOpenHelper {
*/
public List<Note> getNotes() {
List<Note> notes = new ArrayList<>();
SQLiteDatabase db = this.getWritableDatabase();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + table_notes + " WHERE " + key_status + " != ? ORDER BY " + key_modified + " DESC", new String[]{DBStatus.LOCAL_DELETED.getTitle()});
if (cursor.moveToFirst()) {
do {
@ -165,6 +165,32 @@ public class NoteSQLiteOpenHelper extends SQLiteOpenHelper {
return notes;
}
/**
* Returns a list of all Notes in the Database
*
* @return List&lt;Note&gt;
*/
public List<Note> searchNotes(String query) {
List<Note> notes = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + table_notes + " WHERE " + key_status + " != ? AND " + key_content + " LIKE ? ORDER BY " + key_modified + " DESC", new String[]{DBStatus.LOCAL_DELETED.getTitle(), "%" + query + "%"});
if (cursor.moveToFirst()) {
do {
Calendar modified = Calendar.getInstance();
try {
String modifiedStr = cursor.getString(3);
if (modifiedStr != null)
modified.setTime(new SimpleDateFormat(DATE_FORMAT, Locale.GERMANY).parse(modifiedStr));
} catch (ParseException e) {
e.printStackTrace();
}
notes.add(new Note(Long.valueOf(cursor.getString(0)), modified, cursor.getString(2), cursor.getString(4)));
} while (cursor.moveToNext());
}
cursor.close();
return notes;
}
/**
* Returns a list of all Notes in the Database with a sepcial status, e.g. Edited, Deleted,...
*

View file

@ -1,18 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.owncloudnotes.NotesListViewActivity"
xmlns:app="http://schemas.android.com/apk/res-auto" >
tools:context="com.example.owncloudnotes.NotesListViewActivity">
<item
android:id="@+id/search"
android:title="Search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/action_settings"
android:icon="@drawable/ic_action_settings"
android:orderInCategory="100"
app:showAsAction="never"
android:title="@string/action_settings"/>
android:title="@string/action_settings"
app:showAsAction="never"/>
<item
android:id="@+id/action_about"
android:orderInCategory="100"
app:showAsAction="never"
android:title="@string/action_about"/>
android:title="@string/action_about"
app:showAsAction="never"/>
</menu>

View file

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