ListView仅在加载所有SMS后更新

时间:2017-02-26 18:47:51

标签: android listview android-sqlite loader android-cursoradapter

我有两台装载机。首先将SMS加载到我的DB。第二个从我的DB加载SMS到游标适配器。只有在所有短信插入我的数据库后,我的listView才会更新。

这是加载器和适配器:

...
    private static final int SMS_LOADER = 0;
    private static final int MY_DB_LOADER = 1;
    MyCursorAdapter mAdapter;
...

以下是initLoader中的onCreate

    @Override
    protected void onCreate(Bundle savedInstanceState) {
...
        ListView listView = (ListView) findViewById(R.id.list);
        mAdapter = new MyCursorAdapter(this, null);
        listView.setAdapter(mAdapter);
...
        getSupportLoaderManager().initLoader(SMS_LOADER, null, this);
        getSupportLoaderManager().initLoader(MY_DB_LOADER, null, this);
    }

以下onCreateLoader包含2个加载器:

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        switch (id) {
            case SMS_LOADER: {
                String[] projection = {
                        BaseColumns._ID,
                        Telephony.TextBasedSmsColumns.DATE_SENT,
                        Telephony.TextBasedSmsColumns.BODY
                };

                return new CursorLoader(this,
                        BASE_CONTENT_URI,
                        projection,
                        null,
                        null,
                        null);
            }
            case MY_DB_LOADER: {
                String[] projection = {
                        MyEntry._ID,
                        MyEntry.COLUMN_1,
                        MyEntry.COLUMN_2
                };

                return new CursorLoader(this,
                        MyEntry.CONTENT_URI,
                        projection,
                        null,
                        null,
                        null);
            }
            default:
                Log.e(LOG_TAG, "Unsupported id " + id);
                return null;
        }
    }

onLoadFinished我通过短信游标并插入我的数据库:

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        int id = loader.getId();
        switch (id) {
            case SMS_LOADER:
                while (data.moveToNext()) {
                    int columnIndexDateSent = data.getColumnIndex(Telephony.TextBasedSmsColumns.DATE_SENT);
                    int columnIndexBody = data.getColumnIndex(Telephony.TextBasedSmsColumns.BODY);

                    String date = data.getString(columnIndexDateSent);
                    String body = data.getString(columnIndexBody);

                    ContentValues values = new ContentValues();

                    values.put(MyEntry.COLUMN_1, date);
                    values.put(MyEntry.COLUMN_2, body);

                    getContentResolver().insert(MyEntry.CONTENT_URI, values);
                }
                break;
            case MY_DB_LOADER:
                mAdapter.swapCursor(data);
                break;
            default:
                Log.e(LOG_TAG, "Unsupported id " + id);
        }
    }

我想这不是一个合适的插入位置,因为Android Studio会继续说:

  

应用程序可能在其主线程上做了太多工作。

我应该在哪里移动这些插页?如何在第一行插入DB后使listView更新?

1 个答案:

答案 0 :(得分:0)

解决了这个问题。创建了新的类FillDB extends AsyncTask,移动了将数据库填充到doInBackground并在new FillDB().execute(data);中调用onLoadFinished的代码。