【问题标题】:How to update ListView in Android after an IntentService returns something to an ResultReceiverIntentService 向 ResultReceiver 返回内容后如何在 Android 中更新 ListView
【发布时间】:2013-07-23 17:42:03
【问题描述】:

也许我找不到答案的原因是我做错了问题,但我仍然希望在这里有人可以回答这个问题。

我有一个带有 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);
}

现在,我需要的是调用以下 sn-p,当 updateObjectReceiver 变量从我启动并更新数据库的 Intent 服务接收到答案时更新 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();
    }

【问题讨论】:

  • NPE。发生此异常时需要查看代码。
  • 我刚刚添加了它。非常感谢您的帮助。
  • 在启动服务之前确保你有intent.putExtra(RECEIVER_KEY, updateObjectReceiver)
  • 另一个问题(与 NPE 无关)我明白了:您在 if 块中有 receiver.send(STATUS_UPDATED_CHANGED, Bundle.EMPTY);,仅当 jsonDataString 不是 null 时才调用它。但是,receiver.send(STATUS_ERROR, Bundle.EMPTY); 总是被调用(不管jsonDataString 是否为空)。这是自愿的吗?
  • 我有intent.putExtra(RECEIVER_KEY, updateObjectReceiver)。关于你提到的不相关的问题。不,这不是故意的,谢谢您指出。

标签: android database listview intentservice


【解决方案1】:

覆盖UpdateObjectResultReceiveronReceiveResult(int, Bundle)

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

    }
}

【讨论】:

  • 谢谢,这似乎是可行的方法,但我还需要传递我的 cttObjects 和 arrayAdapter,对吗?我如何传递它和上下文?在构造函数中?
  • 您可以将UpdateObjectResultReceiver 作为MainActivity 的内部类。将刷新代码放在MainActivity 的方法中,并从onReceiveResult(int, Bundle) 调用它。请参阅上面的编辑。
  • 对不起,我没有回答你的问题:And how do I pass that and the context? In the constructor?。是的,你可以这样做。或者,在初始化 UpdateObjectResultReceiver 对象后,在 UpdateObjectResultReceiver 中有一个 setter 方法来设置上下文变量。 --> public void setContext(Mainactivity context) { mContext = context; }。但是,通过上面的编辑,您不需要这样做。
  • 再次感谢您。我正在尝试实现这一点,但是当我以这种方式从 IntentService 调用 receiver.send 时,我的应用程序崩溃了。我想看看有什么问题。
  • @jbssm 你得到什么异常?你能发布logcat 的输出吗?
【解决方案2】:

不要创建和 UpdateObjectResultReceiver,而是将其设为接口,然后在您的活动中实现它。然后将回调方法添加到您的活动中并在其中运行代码。

【讨论】:

    猜你喜欢
    • 2016-02-26
    • 1970-01-01
    • 2017-06-28
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-01
    • 1970-01-01
    相关资源
    最近更新 更多