Volley没有向SQLite数据库添加对象

时间:2015-10-22 01:18:38

标签: android android-volley

在我的Android项目中,一次添加一个对象:

TextView testText = (TextView) findViewbyId(R.id.testText);
Person person = new Person("John", "Smith");
Person person2 = new Person("Mary", "Sue");

    dbHelper.addPerson(person);
    dbHelper.addPerson(person2);

    testText.setText(dbHelper.getPeople());

正确导致testText列出了人们的名字:

John
Mary

然而,在使用Volley时,在通过JSON数组解析后成功创建了People个对象,并且我已将其名称输出到logcat

D/main: John Donne
D/main: Arthur Miller
D/main: Edvard Grieg

但是当使用addPerson()添加到SQLite数据库时,它们都没有被添加到数据库中:dbHelper.getPeople()仍然只会产生两个条目。

我的目标是将已创建的Person个对象成功添加到数据库,并在firstname中显示其TextView个属性。

以下是我的主要课程:

MainActivity.java

public class MainActivity extends AppCompatActivity {

private static String peopleURL= "http://myurl";
private static String TAG = "main";
private ProgressDialog progressDialog = null;
DBHelper dbHelper = null;
TextView testText = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    testText = (TextView) findViewById(R.id.testText);
    dbHelper = new DBHelper(this, null, null, 2);

    progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Please wait...");
    progressDialog.setCancelable(false);


    Person person= new Person("John", "Smith");
    Person person2 = new Person("Mary", "Sue");

    refresh();
    dbHelper.addPerson(person);
    dbHelper.addPerson(person2);

    testText.setText(dbHelper.getPeople());
}

public void refresh() {
    dbHelper.refreshDB();
    getJSONPeople()

}

private void showpDialog() {
    if (!progressDialog.isShowing())
        progressDialog.show();
}

private void hidepDialog() {
    if (progressDialog.isShowing())
        progressDialog.dismiss();
}



public void getJSONPeople(){

    showpDialog();

    JsonArrayRequest request = new JsonArrayRequest(peopleURL,
            new Response.Listener<JSONArray>(){

                @Override
                public void onResponse(JSONArray response) {
                    try{

                        for (int i = 0; i < response.length(); i++){
                            JSONObject json = response.getJSONObject(i);
                            Person person= new Office(json.getString("firstname"), json.getString("lastname"));

                            dbHelper.addPerson(person);
                            Log.d(TAG, person.get_firstname() + " " + office.get_lastname());


                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(),
                                "Error: " + e.getMessage(),
                                Toast.LENGTH_LONG).show();
                    }

                    hidepDialog();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_SHORT).show();
            hidepDialog();
        }
    });

    AppController.getInstance().addToRequestQueue(request);


}

} 

Person.java

public class Person {

    private int _id;
    private String firstname;
    private String lastname;

    public void set_id(int id) {
        this._id = id;
    }

    public void set_firstname(String firstname) {
        this.firstname= firstname;
    }

    public void set_lastname(String lastname) {
        this.lastname = lastname;
    }

    public int get_id() {
        return this._id;
    }
    public String get_firstname() {
        return this.firstname;
    }
    public String get_lastname() {
        return this.lastname;
    }

    public Person() {

    }

    public Person(String firstname, String lastname) {
        this.firstname = firstname;
        this.lastname = lastname;
    }


}

DBHelper.java

public class DBHelper extends SQLiteOpenHelper {

    public static final int DATABASE_VERSION = 2;
    public static final String DATABASE_NAME = "people.db";

    public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, factory, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE people (_id INTEGER PRIMARY KEY AUTOINCREMENT, firstname TEXT, lastname TEXT)");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS people");
        onCreate(db);
    }

    public void refreshDB() {
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("DELETE FROM people WHERE 1");
    }

    public void addPerson(Person person) {
        ContentValues values = new ContentValues();
        values.put("firstname", person.get_firstname());
        values.put("lastname", person.get_lastname());

        SQLiteDatabase db = getWritableDatabase();
        db.insert("people", null, values);
        db.close();
    }

    public String getPeople() {
        SQLiteDatabase db = getWritableDatabase();
        String result = "";
        String query = "SELECT * FROM people WHERE 1";
        Cursor c = db.rawQuery(query, null);
        c.moveToFirst();

        while (!c.isAfterLast()) {
            if (c.getString(c.getColumnIndex("firstname")) != null) {
                result += c.getString(c.getColumnIndex("firstname"));
                result += "\n";
                c.moveToNext();
            }
        }
        c.close();
        db.close();
        return result;
    }
}

0 个答案:

没有答案