也许我无法找到答案的原因是,我正在以错误的方式提出这个问题,但我仍然希望有人可以回答这个问题。
我有一个带有ListView的MainActivity,它在处理后从数据库中显示一些值:
public class MainActivity extends Activity {
ListView mainViewList;
CTTObjectsArrayAdapter arrayAdapter;
ArrayList<CTTObject> cttObjects = null;
public UpdateObjectResultReceiver updateObjectReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Update all the Objects in the DB
CTTUtilities.updateAllObjects(context, updateObjectReceiver);
// DB stuff
CTTObjectsDataSource dataSource = new CTTObjectsDataSource(context);
dataSource.open();
// Initialize ListView
mainViewList = (ListView) findViewById(R.id.listMain);
// Initialize our ArrayList
cttObjects = new ArrayList<CTTObject>();
cttObjects = dataSource.getAllObjects();
dataSource.close();
// Initialize our array adapter notice how it references the
// listMain.xml layout
arrayAdapter = new CTTObjectsArrayAdapter(context,
R.layout.main_list_row_layout, cttObjects);
// Set the above adapter as the adapter of choice for our list
mainViewList.setAdapter(arrayAdapter);
}
现在,我需要的是,当updateObjectReceiver变量从我启动的Intent Service收到答案并更新数据库时,调用以下代码片段来更新ListView:
cttObjects.clear();
CTTObjectsDataSource dataSource = new CTTObjectsDataSource(context);
dataSource.open();
cttObjects.addAll(dataSource.getAllObjects());
dataSource.close();
arrayAdapter.notifyDataSetChanged();
我怎么能这样做?
修改
在实现了receiver.send和下面提出的方法之后,应用程序失败了:
07-23 20:30:40.435: W/dalvikvm(3467): threadid=15: thread exiting with uncaught exception (group=0x40c88930)
07-23 20:30:40.435: E/AndroidRuntime(3467): FATAL EXCEPTION: IntentService[UpdateObjectIntentService]
07-23 20:30:40.435: E/AndroidRuntime(3467): java.lang.NullPointerException
07-23 20:30:40.435: E/AndroidRuntime(3467): at info.hecatosoft.ctttrack2.UpdateObjectIntentService.onHandleIntent(UpdateObjectIntentService.java:89)
07-23 20:30:40.435: E/AndroidRuntime(3467): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
07-23 20:30:40.435: E/AndroidRuntime(3467): at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 20:30:40.435: E/AndroidRuntime(3467): at android.os.Looper.loop(Looper.java:137)
07-23 20:30:40.435: E/AndroidRuntime(3467): at android.os.HandlerThread.run(HandlerThread.java:60)
编辑2:
该行失败:receiver.send(STATUS_UPDATED_CHANGED, Bundle.EMPTY);
有问题的代码是:
@Override
protected void onHandleIntent(Intent arg0) {
// ---process the received extras to the service call and get the URL
// from it---
this.receiver = arg0.getParcelableExtra(RECEIVER_KEY);
String recTrackNo = arg0.getStringExtra("trackNo");
Log.i("jbssmDeveloper", "received trackNo=" + recTrackNo);
String jsonDataString = null;
jsonDataString = getHTTPJSONData(recTrackNo);
if (jsonDataString != null) {
dataSource = new CTTObjectsDataSource(getBaseContext());
dataSource.open();
CTTObject cttObject = dataSource
.getObjectWithTrackNo(recTrackNo);
dataSource.close();
updateDB(cttObject, jsonDataString);
receiver.send(STATUS_UPDATED_CHANGED, Bundle.EMPTY);
}
receiver.send(STATUS_ERROR, Bundle.EMPTY);
this.stopSelf();
}
答案 0 :(得分:2)
覆盖onReceiveResult(int, Bundle)
的{{1}}:
UpdateObjectResultReceiver
初始化ResultReceiver时,您必须传递活动的上下文。
修改强>
@Override
protected void onReceiveResult (int resultCode, Bundle resultData) {
cttObjects.clear();
CTTObjectsDataSource dataSource = new CTTObjectsDataSource(context);
dataSource.open();
cttObjects.addAll(dataSource.getAllObjects());
dataSource.close();
arrayAdapter.notifyDataSetChanged();
}
答案 1 :(得分:0)
而不是创建和UpdateObjectResultReceiver,使其成为一个接口,然后在您的活动中实现它。然后将回调方法添加到您的活动中并在其中运行代码。