0
我有兩個加載程序。第一個將短信加載到我的數據庫。第二個從我的數據庫加載SMS到遊標適配器。我有我的listView更新後,所有的短信插入到我的數據庫。ListView僅在所有SMS加載後才更新
這裏是裝載機和適配器:
...
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
I' m通過SMS光標並插入我的DB:
@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中不停地說:
應用程序可以做它的主線程的工作太多了。
我應該在哪裏移動這些插頁?如何使第一行插入到數據庫後更新listView?