2

おそらく、これに対する答えが見つからない理由は、私が質問を間違った方法で行っているためですが、それでも誰かがこれに答えてくれることを願っています.

処理後にデータベースからいくつかの値を提示する 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);
}

ここで必要なのは、私が開始してデータベースを更新した Intent Service から updateObjectReceiver 変数が応答を受け取ったときに、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();
    }
4

2 に答える 2

2

onReceiveResult(int, Bundle)のオーバーライドUpdateObjectResultReceiver:

@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();

}

ResultReceiver を初期化するときに、アクティビティのコンテキストを渡す必要があります。

編集

public class MainActivity extends Activity {

....
....
....

    public void showResults() {
        cttObjects.clear();
        CTTObjectsDataSource dataSource = new CTTObjectsDataSource(MainActivity.this);
        dataSource.open();
        cttObjects.addAll(dataSource.getAllObjects());
        dataSource.close();
        arrayAdapter.notifyDataSetChanged();
    }

....
....
....



    class UpdateObjectResultReceiver extends ResultReceiver {

    ....
    ....
    ....

        @Override
        protected void onReceiveResult (int resultCode, Bundle resultData) {
            MainActivity.this.showResults();
        }

    }
}
于 2013-07-23T17:50:05.893 に答える
0

UpdateObjectResultReceiver を作成して UpdateObjectResultReceiver にする代わりに、インターフェイスにしてからアクティビティに実装します。次に、コールバック メソッドをアクティビティに追加し、その中でコードを実行します。

于 2013-07-23T17:45:28.150 に答える