vendredi 25 décembre 2015

How to save data to SQLite using Content provider and Asynctask

I have problem on saving my data into database SQLite by using ContentProvider and Asynctask, here is my code

BtnSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String JudulSesi = String.valueOf(judulSesi.getText());
            String Description = String.valueOf(Ket.getText());
            ContentValues contentValues = new ContentValues();

            contentValues.put(LocalDBHandler.FIELD_SESSION_TYPE, sessionType);
            contentValues.put(LocalDBHandler.FIELD_SESSION_NAME, JudulSesi);
            contentValues.put(LocalDBHandler.FIELD_SESSION_DESC, Description);
            contentValues.put(LocalDBHandler.FIELD_SESSION_TIME,Date.toString());
            contentValues.put(LocalDBHandler.FIELD_ELAPSED_TIME, dataTimeElapsed );
            contentValues.put(LocalDBHandler.FIELD_ARRAYPOINT,stringPoint);
            contentValues.put(LocalDBHandler.FIELD_DISTANCE, dataJarak);
            contentValues.put(LocalDBHandler.FIELD_SPEED, speed);
            contentValues.put(LocalDBHandler.FIELD_HEART_RATE, heartRate);
            contentValues.put(LocalDBHandler.FIELD_CALLORIES, callorie);

            SaveToDatabase savetoDB = new SaveToDatabase();

            savetoDB.execute(contentValues);

            Toast.makeText(SaveSession.this, "You Click Save Button", Toast.LENGTH_SHORT).show();

        }
    });

private class SaveToDatabase extends AsyncTask<ContentValues,Void,Void>{
    @Override
    protected Void doInBackground(ContentValues... contentValues) {
        getContentResolver().insert(Uri.parse(LocalDBHandler.DATABASE_TABLE),contentValues[0]);
        return null;
    }
}

When I Run my program, its closed and Here is the logcat result

12-26 02:21:43.236 8436-9787/com.armand17.runandride E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #5 Process: com.armand17.runandride, PID: 8436 java.lang.RuntimeException: An error occurred while executing doInBackground()
  at android.os.AsyncTask$3.done(AsyncTask.java:309)
  atjava.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
  at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
  at java.util.concurrent.FutureTask.run(FutureTask.java:242)
  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
  at java.lang.Thread.run(Thread.java:818)
  Caused by: java.lang.IllegalArgumentException: Unknown URL run_and_ride
  at android.content.ContentResolver.insert(ContentResolver.java:1227)
  at com.armand17.runandride.SaveSession$SaveToDatabase.doInBackground(SaveSession.java:143)
  at com.armand17.runandride.SaveSession$SaveToDatabase.doInBackground(SaveSession.java:140)
  at android.os.AsyncTask$2.call(AsyncTask.java:295)
  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
  at java.lang.Thread.run(Thread.java:818) 

it said that my Asynctask get wrong, but I dont know where is it.

Aucun commentaire:

Enregistrer un commentaire