diff --git a/source/src/cn/eoe/app/ui/DetailsActivity.java b/source/src/cn/eoe/app/ui/DetailsActivity.java index d90181d..bf1decc 100644 --- a/source/src/cn/eoe/app/ui/DetailsActivity.java +++ b/source/src/cn/eoe/app/ui/DetailsActivity.java @@ -70,6 +70,8 @@ public class DetailsActivity extends BaseActivity implements OnClickListener { private DetailDB mDetailDB; private String mKey; private int mDBID = -1; + + private AsyncTask initAppraiseTask; @Override protected void onCreate(Bundle savedInstanceState) { @@ -85,7 +87,13 @@ protected void onCreate(Bundle savedInstanceState) { mKey = sharePre.getString(UserLoginUidActivity.KEY, ""); initData(); initControl(); - initAppraise(); + initAppraiseTask = new AsyncTask() { + @Override + protected Void doInBackground(Void... params) { + initAppraise(); + return null; + } + }.execute(); } private void initData() { @@ -193,16 +201,32 @@ protected void onPause() { if (mKey.equals(null) && mKey.equals("")) { return; } - if (mDBID == -1 && (IsGood || IsBed || IsCollect)) { - // 添加 - mDetailDB.insertSQL(mUrl, mKey, IsGood ? 1 : 0, IsBed ? 1 : 0, - IsCollect ? 1 : 0); - } else if (mDBID != -1) { - // 修改 - mDetailDB.updateSQL(mDBID, IsGood ? 1 : 0, IsBed ? 1 : 0, - IsCollect ? 1 : 0); - } - mDetailDB.dbClose(); + new AsyncTask() { + @Override + protected void onPreExecute() { + // synchronize with initAppraiseTask + try { + initAppraiseTask.get(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Override + protected Void doInBackground(Void... params) { + if (mDBID == -1 && (IsGood || IsBed || IsCollect)) { + // 添加 + mDetailDB.insertSQL(mUrl, mKey, IsGood ? 1 : 0, IsBed ? 1 : 0, + IsCollect ? 1 : 0); + } else if (mDBID != -1) { + // 修改 + mDetailDB.updateSQL(mDBID, IsGood ? 1 : 0, IsBed ? 1 : 0, + IsCollect ? 1 : 0); + } + mDetailDB.dbClose(); + return null; + } + }.execute(); } class MyTask extends AsyncTask { @@ -277,6 +301,14 @@ public void onClick(View v) { showLongToast(getResources().getString(R.string.user_login_prompt)); return; } + + // synchronize with initAppraiseTask + try { + initAppraiseTask.get(); + } catch (Exception e) { + e.printStackTrace(); + } + String url = null; switch (v.getId()) { case R.id.rlGood: diff --git a/source/src/cn/eoe/app/utils/ImageUtil.java b/source/src/cn/eoe/app/utils/ImageUtil.java index df9bd30..f2fa428 100644 --- a/source/src/cn/eoe/app/utils/ImageUtil.java +++ b/source/src/cn/eoe/app/utils/ImageUtil.java @@ -20,9 +20,11 @@ import android.graphics.BitmapFactory; import android.graphics.BitmapFactory.Options; import android.graphics.Rect; +import android.os.AsyncTask; import android.os.Build; import android.os.Environment; import android.os.Handler; +import android.os.Looper; import android.os.Message; import android.util.Log; import android.widget.ImageView; @@ -116,16 +118,24 @@ public static boolean hasExternalCacheDir() { private static void setThumbnailImage(ImageView view, String imageUrl, String cachePath, DBHelper dbHelper, ImageCallback callback, boolean b) { - Bitmap bitmap = null; - bitmap = ImageUtil.loadThumbnailImage(cachePath, imageUrl, dbHelper, - callback, b); - if (bitmap == null) {// 先查找数据库,再查找本地sd卡,若没有.再从网站加载,若网站上没有图片或错误时返回null - // 设置默认图片 - view.setImageResource(Default_Img); - } else { - // 设置本地SD卡缓存图片 - view.setImageBitmap(bitmap); - } + new AsyncTask() { + @Override + protected Bitmap doInBackground(Void... params) { + return ImageUtil.loadThumbnailImage(cachePath, imageUrl, dbHelper, + callback, b); + } + + @Override + protected void onPostExecute(Bitmap bitmap) { + if (bitmap == null) {// 先查找数据库,再查找本地sd卡,若没有.再从网站加载,若网站上没有图片或错误时返回null + // 设置默认图片 + view.setImageResource(Default_Img); + } else { + // 设置本地SD卡缓存图片 + view.setImageBitmap(bitmap); + } + } + }.execute(); } private static Bitmap getImageFromDB(String imagePath, String imageUrl, @@ -297,7 +307,7 @@ public static Bitmap loadThumbnailImage(final String imagePath, return bitmap; } else { // 从网上加载 - final Handler handler = new Handler() { + final Handler handler = new Handler(Looper.getMainLooper()) { @Override public void handleMessage(Message msg) { if (msg.obj != null) {