在SQLite Android应用程序中插入数据是挂起的

时间:2018-01-17 05:58:42

标签: android sqlite android-sqlite sqliteopenhelper

当我在主屏幕应用程序中通过后台进程在SQLite中插入数据时,挂起。我不能点击任何按钮。在SQLite应用程序中成功插入数据后,工作正常。那么在SQLite中插入数据有什么办法吗?我可以轻松点击按钮? [我不想显示进度对话框并在数据库中静默插入数据]

public void Auth(final SignIn mSignIn, final String fbId, final String msgId) {
    CustomRequest jsonObjectRequest = new CustomRequest(Request.Method.POST, General.LOGIN, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject jsonObject) {

            Log.e(LOGTAG, "SignIn Response - " + jsonObject.toString());
            try {
                JSONObject jData = jsonObject.getJSONObject(General.DATA);
                String status = jData.getString(General.STATUSCODE);

                if (status.equalsIgnoreCase("1")) {

                    // success
                    dbHelper = new DbHelper(activity);
                    if (dbHelper.dbIsOpen()) {

                    } else {
                        dbHelper.open();
                    }
                    authHandleFetchAuthFriendResponse(jData.getJSONObject(General.FRIENDLIST));

                    dbHelper.close();
                }
            } catch (IllegalStateException e) {
                e.printStackTrace();
                dbHelper.close();
            } catch (Exception e) {
                Log.e(LOGTAG, "SignIn Response Exception - " + jsonObject.toString());
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            Log.e(LOGTAG, "SignIn Error : " + volleyError.toString());
        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("deviceMake", General.DEVICEMAKE);
            params.put("deviceId", GiftAMeal.getInstance().getDeviceId());
            params.put("deviceToken", GiftAMeal.getInstance().getDeviceToken());
            params.put("userName", GiftAMeal.getInstance().getUserName());
            params.put("password", GiftAMeal.getInstance().getPassword());

            return params;
        }
    };

//这是从服务器

中提取数据的时间
public void authHandleFetchAuthFriendListResponse(JSONArray jsonArray) {

    for (int i = 0; i < jsonArray.length(); i++) {
        try {
            Friendlist r = new Friendlist(jsonArray.getJSONObject(i));
            dbHelper.createGiftAMealFriendlist(r);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

//插入数据的方法

public long createGiftAMealFriendlistNew(Data.Friend fri) {

    ContentValues initialValues = new ContentValues();
    // Attributes of Friendlist Table
    initialValues.put(friendListId, fri.getFriendListId());
    initialValues.put(fromUserId, fri.getFromUserId());
    initialValues.put(toUserId, fri.getToUserId());
    initialValues.put(facebookSyncFlag, fri.getFacebookSyncFlag());
    initialValues.put(requestStatus, fri.getRequestStatus());
    initialValues.put(requestSentDate, fri.getRequestSendDate());
    initialValues.put(requestAcceptedDate, fri.getRequestAcceptedDate());
    initialValues.put(requestRejectedDate, fri.getRequestRejectedDate());
    initialValues.put(USER_ID, fri.getUSERID());
    initialValues.put(email, fri.getEmail());
    initialValues.put(userName, fri.getUserName());
    initialValues.put(fbID, fri.getFbID());

    long rowid = mDb.insert(GiftAMeal_FRIENDLIST_TABLE, null, initialValues);
    return rowid;
}

1 个答案:

答案 0 :(得分:1)

像这样使用AsyncTask

class InsertDataAsync extends AsyncTask<Void, Void, Long> {

        ProgressDialog progressDialog;
        Data.Friend fri;

        InsertDataAsync(Data.Friend fri) {
            this.fri = fri;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog.setMessage("Please wait...");
            progressDialog.show();
        }

        @Override
        protected Long doInBackground(Void... voids) {
            try {
                return createGiftAMealFriendlistNew(fri);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return 0L;
        }

        @Override
        protected void onPostExecute(Long id) {
            super.onPostExecute(id);
            progressDialog.dismiss();
            if (id > 0) {
                //data insert success
            } else {
                //some error occurred
            }

        }
    }

并像这样打电话给这个班级

new InsertDataAsync(fri).execute();

其中friData.Friend引用。

希望这对你有所帮助。