[Android] [PATCH 3/8] extract database access to AsyncTask

Yu Lin yu.lin.86 at gmail.com
Tue Mar 4 23:33:41 CET 2014


---
 .../src/org/videolan/vlc/gui/SearchFragment.java   |   28 +++++++++++++++----
 1 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/SearchFragment.java b/vlc-android/src/org/videolan/vlc/gui/SearchFragment.java
index 166d1d0..16cf1af 100644
--- a/vlc-android/src/org/videolan/vlc/gui/SearchFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/SearchFragment.java
@@ -33,6 +33,7 @@ import org.videolan.vlc.gui.video.VideoPlayerActivity;
 import com.actionbarsherlock.app.SherlockListFragment;
 
 import android.content.Context;
+import android.os.AsyncTask;
 import android.os.Bundle;
 import android.text.Editable;
 import android.text.TextWatcher;
@@ -177,10 +178,18 @@ public class SearchFragment extends SherlockListFragment {
      * Clear the search history.
      */
     public void clearSearchHistory() {
-            MediaDatabase db = MediaDatabase.getInstance(getActivity());
-            db.clearSearchhistory();
-            if (mHistoryAdapter == getListAdapter())
-                showSearchHistory();
+        new AsyncTask<Void, Void, Void>() {
+            protected Void doInBackground(Void... args) {
+                MediaDatabase db = MediaDatabase.getInstance(getActivity());
+                db.clearSearchhistory();
+                return null;
+            }
+
+            protected void onPostExecute(Void v) {
+                if (mHistoryAdapter == getListAdapter())
+                    showSearchHistory();
+            }
+        }.execute();
     }
 
     private final OnEditorActionListener searchTextListener = new OnEditorActionListener() {
@@ -201,8 +210,15 @@ public class SearchFragment extends SherlockListFragment {
             mSearchText.requestFocus();
         } else if (getListAdapter() == mResultAdapter) {
             // add search text to the database (history)
-            MediaDatabase db = MediaDatabase.getInstance(getActivity());
-            db.addSearchhistoryItem(mSearchText.getText().toString());
+            String searchText = mSearchText.getText().toString();
+            new AsyncTask<String, Void, Void>() {
+                protected Void doInBackground(String... args) {
+                    String searchText = args[0];
+                    MediaDatabase db = MediaDatabase.getInstance(getActivity());
+                    db.addSearchhistoryItem(searchText);
+                    return null;
+                }
+            }.execute(searchText);
 
             // open media in the player
             Media item = (Media) getListAdapter().getItem(position - 1);
-- 
1.7.4.4



More information about the Android mailing list