数据已添加到数据库,但无法从数据库中加载。安卓系统

时间:2019-05-09 16:14:26

标签: android sqlite

我面临以下问题:我有一个基于MVP模式的应用程序。应用程序应从edittext中获取数据,将其添加到数据库中,然后从数据库中加载该数据。这是代码:

演讲者课程:

package asus.example.com.mvpexample;

import android.content.ContentValues;
import android.util.Log;

import java.util.List;
import java.util.concurrent.ExecutionException;

public class Presenter {

    private MainActivity view;
    private Model model;

    public Presenter(Model model, MainActivity mainActivity){
        this.model = model;
        this.view = mainActivity;
    }

    public void add(){
        Data data = view.getDataFromEdit();
        ContentValues cv = new ContentValues(1);
        cv.put("INFO", data.getDataString());
        Log.i(getClass().getSimpleName(), "Added in presenter");
        model.addValues(cv);
    }

    public List<Data> loadValues(){
        try {
            Log.i(getClass().getSimpleName(), "List returned in Presenter");
            return model.loadValues();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

}

模型类:

package asus.example.com.mvpexample;

import android.content.ContentValues;
import android.database.Cursor;
import android.os.AsyncTask;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;


public class Model {

    private DBHelper dbHelper;

    public Model(DBHelper dbHelper){
        this.dbHelper = dbHelper;
    }

    public void addValues(ContentValues contentValues){
        AddData addData = new AddData();
        addData.execute(contentValues);
        Log.i(getClass().getSimpleName(), "Values added in model");
    }

    public List<Data> loadValues() throws ExecutionException, InterruptedException {
        LoadData loadData = new LoadData();
        List<Data> list = loadData.execute().get();
        Log.i(getClass().getSimpleName(), "List loaded in model");
        return list;
    }



    private class AddData extends AsyncTask<ContentValues, Void, Void> {

        @Override
        protected Void doInBackground(ContentValues... contentValues) {
            ContentValues cv = contentValues[0];
            dbHelper.getWritableDatabase().insert("DATA_TABLE", null, cv);
            return null;
        }
    }

    private class LoadData extends AsyncTask<Void, Void, List<Data>>{

        @Override
        protected List<Data> doInBackground(Void... voids) {
            List<Data> dataList = new ArrayList<>();
            Cursor cursor = dbHelper.getReadableDatabase().query("DATA_TABLE", null,
                    null, null, null, null, null);
            Data data = new Data();
            while (cursor.moveToNext()){
                data.setId(cursor.getInt(cursor.getColumnIndex("ID")));
                data.setDataString(cursor.getString(cursor.getColumnIndex("INFO")));
            }

            return dataList;
        }
    }
}

数据库类:

package asus.example.com.mvpexample;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {

    public DBHelper(Context context) {
        super(context, "MyDatabase", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE DATA_TABLE ("+
                "ID INTEGER PRIMARY KEY AUTOINCREMENT,"+
                "INFO TEXT"+
                ");"
        );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

当我在调试器中选中它时,数据会添加到数据库中,因为Presenter方法data.getDataString()中的add不为null,而是包含一个值。但是Model类中的方法public List<Data> loadValues() throws ExecutionException, InterruptedException返回null,我不明白为什么。那么,怎么了,我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

此代码中有两个错误,

  1. 在doInBackground方法中,您忘记了将数据添加到dataList。
  2. 您应该在while(cursor.moveToNext())循环中创建一个新的Data对象。

所以应该像这样:

while (cursor.moveToNext()) {
  Data data = new Data();
  data.setId(cursor.getInt(cursor.getColumnIndex("ID")));
  data.setDataString(cursor.getString(cursor.getColumnIndex("INFO")));
  dataList.add(data);
}

希望有帮助!