如何在Android的ListView的更新的IntentService返回某件事的ResultReceiver后 [英] How to update ListView in Android after an IntentService returns something to an ResultReceiver

查看:197
本文介绍了如何在Android的ListView的更新的IntentService返回某件事的ResultReceiver后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我无法找到答案,原因是我做的问题,错误的方式,但我仍然希望,这里SO有人能回答这个问题。

我对此present一些值一个ListView一个MainActivity - 加工后 - 从数据库:

 公共类MainActivity延伸活动{
ListView的mainViewList;
    CTTObjectsArrayAdapter arrayAdapter;
    ArrayList的< CTTObject> cttObjects = NULL;
    公共UpdateObjectResultReceiver updateObjectReceiver;@覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        //更新在数据库中的所有对象
        CTTUtilities.updateAllObjects(背景下,updateObjectReceiver);
        // DB的东西
        CTTObjectsDataSource数据源=新CTTObjectsDataSource(背景);
        dataSource.open();        //初始化的ListView
        mainViewList =(ListView控件)findViewById(R.id.listMain);        //初始化我们的ArrayList
        cttObjects =新的ArrayList< CTTObject>();
        cttObjects = dataSource.getAllObjects();
        dataSource.close();        //初始化我们的阵列适配器注意它引用
        // listMain.xml布局
        arrayAdapter =新CTTObjectsArrayAdapter(背景下,
                R.layout.main_list_row_layout,cttObjects);        //设置上述适配器作为首选适配器我们的名单
        mainViewList.setAdapter(arrayAdapter);
}

现在,我需要的,是当updateObjectReceiver变量接收从我开始的意图服务和更新数据库中的答案调用下面的代码片段更新的ListView:

  cttObjects.clear();
CTTObjectsDataSource数据源=新CTTObjectsDataSource(背景);
dataSource.open();
cttObjects.addAll(dataSource.getAllObjects());
dataSource.close();
arrayAdapter.notifyDataSetChanged();

我怎样才能做到这一点呢?

编辑:

实施receiver.send及以下提出的方法后,应用程序失败:

  20 07-23:30:​​40.435:W / dalvikvm(3467):主题ID = 15:螺纹未捕获的异常退出(组= 0x40c88930)
07-23 20:30:40.435:E / AndroidRuntime(3467):致命异常:IntentService [UpdateObjectIntentService]
07-23 20:30:40.435:E / AndroidRuntime(3467):显示java.lang.NullPointerException
07-23 20:30:40.435:E / AndroidRuntime(3467):在info.hecatosoft.ctttrack2.UpdateObjectIntentService.onHandleIntent(UpdateObjectIntentService.java:89)
07-23 20:30:40.435:E / AndroidRuntime(3467):在android.app.IntentService $ ServiceHandler.handleMessage(IntentService.java:65)
07-23 20:30:40.435:E / AndroidRuntime(3467):在android.os.Handler.dispatchMessage(Handler.java:99)
07-23 20:30:40.435:E / AndroidRuntime(3467):在android.os.Looper.loop(Looper.java:137)
07-23 20:30:40.435:E / AndroidRuntime(3467):在android.os.HandlerThread.run(HandlerThread.java:60)

编辑2:

它没有在该行: receiver.send(STATUS_UPDATED_CHANGED,Bundle.EMPTY);

在考虑中的code是:

  @覆盖
    保护无效onHandleIntent(意向为arg0){
        // ---处理接收到的额外服务调用并获取URL
        // 从中 - -
        this.receiver = arg0.getParcelableExtra(RECEIVER_KEY);
        串recTrackNo = arg0.getStringExtra(trackNo);
        Log.i(jbssmDeveloper+ recTrackNotrackNo =收到);        字符串jsonDataString = NULL;            jsonDataString = getHTTPJSONData(recTrackNo);            如果(jsonDataString!= NULL){
                数据源=新CTTObjectsDataSource(getBaseContext());
                dataSource.open();
                CTTObject cttObject =数据源
                        .getObjectWithTrackNo(recTrackNo);
                dataSource.close();
                数据库更新(cttObject,jsonDataString);
                receiver.send(STATUS_UPDATED_CHANGED,Bundle.EMPTY);
            }
            receiver.send(STATUS_ERROR,Bundle.EMPTY);
        this.stopSelf();
    }


解决方案

重写 onReceiveResult(INT,捆绑) UpdateObjectResultReceiver

  @覆盖
保护无效onReceiveResult(INT结果code,捆绑resultData){    cttObjects.clear();
    CTTObjectsDataSource数据源=新CTTObjectsDataSource(背景);
    dataSource.open();
    cttObjects.addAll(dataSource.getAllObjects());
    dataSource.close();
    arrayAdapter.notifyDataSetChanged();}

您将有当你初始化ResultReceiver来传递您的活动的背景。

修改

 公共类MainActivity延伸活动{....
....
....    公共无效showResults(){
        cttObjects.clear();
        CTTObjectsDataSource数据源=新CTTObjectsDataSource(MainActivity.this);
        dataSource.open();
        cttObjects.addAll(dataSource.getAllObjects());
        dataSource.close();
        arrayAdapter.notifyDataSetChanged();
    }....
....
....    类UpdateObjectResultReceiver扩展ResultReceiver {    ....
    ....
    ....        @覆盖
        保护无效onReceiveResult(INT结果code,捆绑resultData){
            MainActivity.this.showResults();
        }    }
}

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

解决方案

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

    }
}

这篇关于如何在Android的ListView的更新的IntentService返回某件事的ResultReceiver后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆