How to update ListView in Android after an IntentService returns something to an ResultReceiver

…衆ロ難τιáo~ 提交于 2019-12-24 03:17:13

问题


Perhaps the reason I can't find an answer to this, is that I'm doing the question the wrong way, but still I hope that here on SO someone can answer this.

I have a MainActivity with a ListView that present some values - after processing - from a database:

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);
}

Now, what I need, is to call the following snippet that updates the ListView when the updateObjectReceiver variable receives an answer from the Intent Service that I started and that updated the database:

cttObjects.clear();
CTTObjectsDataSource dataSource = new CTTObjectsDataSource(context);
dataSource.open();
cttObjects.addAll(dataSource.getAllObjects());
dataSource.close();
arrayAdapter.notifyDataSetChanged();

How can I do this then?

EDIT:

After implementing the receiver.send and the method proposed below, the app fails with:

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)

EDIT 2:

It fails in the line: receiver.send(STATUS_UPDATED_CHANGED, Bundle.EMPTY);

The code in question is:

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

回答1:


Override the onReceiveResult(int, Bundle) of 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();

}

You will have to pass your activity's context when you initialize the ResultReceiver.

Edit

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

    }
}



回答2:


Instead of creating and UpdateObjectResultReceiver, make it an interface and then implement it in your activity. Then add the callback method to your activity and run the code within it.



来源:https://stackoverflow.com/questions/17817286/how-to-update-listview-in-android-after-an-intentservice-returns-something-to-an

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!