Android-添加到SQLite数据库后,除非重新启动应用程序,否则不会更新ListView [英] Android - ListView not being updated after adding to SQLite database unless app is restarted

查看:85
本文介绍了Android-添加到SQLite数据库后,除非重新启动应用程序,否则不会更新ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试通过查询提供适配器的SQLite数据库来填充ListView,可以通过添加条目来更新它.但是,除非重新启动应用程序,否则ListView不会更新.

I am currently trying to populate a ListView by querying a SQLite database which feeds the adapter, which I can update by adding an entry. However, the ListView doesn't get updated unless I restart the app.

这是我的数据源代码:

public class DataSource {

    // Database fields
    private SQLiteDatabase database;
    private SQLHelper dbHelper;
    private String[] allColumns = null;

    public DataSource(Context context) {
        dbHelper = new SQLHelper(context);
    }

    public void open() throws SQLException {
        database = dbHelper.getWritableDatabase();
    }

    public void close() {
        dbHelper.close();
    }

    public Person addPerson(String firstname, String lastname) {    
        ContentValues values = new ContentValues();
        values.put(SQLHelper.COLUMN_FIRSTNAME, firstname);
        values.put(SQLHelper.COLUMN_LASTNAME, lastname);

        long insertId = database.insert(SQLHelper.TABLE_PEOPLE, null, values);
        Cursor cursor = database.query(SQLHelper.TABLE_PEOPLE, allColumns, SQLHelper.COLUMN_ID + " = " + insertId, null, null, null, null);
        cursor.moveToFirst();
        Person newPerson = cursorToPerson(cursor);
        cursor.close();
        return newPerson;
    }

    public List<Person> getAllPeople() {
        List<Person> people = new ArrayList<Person>();

        Cursor cursor = database.query(SQLHelper.TABLE_PEOPLE, allColumns, null, null, null, null, null);

        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            Person person = cursorToPerson(cursor);
            people.add(person);
            cursor.moveToNext();
        }
        cursor.close();
        return people;
    }

    private Person cursorToPerson(Cursor cursor) {
        if(cursor.getCount() > 0) {
            Person person = new Person();
            person.setId(cursor.getLong(0));
            person.setFirstname(cursor.getString(1));
            person.setLastname(cursor.getString(1));
            return person;
       }
       return null;
    }
}

这是SQLHelper:

And here is SQLHelper:

public class SQLHelper extends SQLiteOpenHelper {

    // Database
    private static final String DATABASE_NAME = "people.db";
    private static final int DATABASE_VERSION = 1;

    // Table
    public static final String TABLE_PEOPLE = "people";

    // Columns
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_FIRSTNAME = "first_name";
    public static final String COLUMN_LASTNAME = "last_name";

    // Database creation sql statement
    private static final String DATABASE_CREATE = "create table " + TABLE_PEOPLE + "(" + COLUMN_ID + " integer primary key autoincrement, " + COLUMN_FIRSTNAME + " text not null, " + COLUMN_LASTNAME + " text not null);";

    public SQLHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(SQLHelper.class.getName(), "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data"); 
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PEOPLE);
        onCreate(db);
    }
}

要填充ListView适配器,请调用getAllPeople()方法.要添加到数据库中,我调用addPerson()方法. ListView在对话框中.

To populate my ListView adapter, I call the getAllPeople() method. To add to my database, I call the addPerson() method. The ListView is inside a dialog.

有什么特殊的原因为什么我必须重新启动我的应用程序才能用新用户更新ListView?

Is there any particular reason why I must restart my app for the ListView to be updated with the new person?

推荐答案

您必须将有关更改的数据通知适配器.根据适配器的类型,您需要调用swapCursor()notifyDatasetChanged()

You must notify your adapter about data changed. Depending on type of adapter you need to either call swapCursor() or notifyDatasetChanged()

这篇关于Android-添加到SQLite数据库后,除非重新启动应用程序,否则不会更新ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆