android - IntentService 将某些内容返回到 ResultReceiver 后如何更新 Android 中的 ListView

标签 android database listview intentservice

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

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

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

最佳答案

重写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 时,您必须传递 Activity 的上下文。

编辑

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 - IntentService 将某些内容返回到 ResultReceiver 后如何更新 Android 中的 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17817286/

相关文章:

mysql - 将 4 个表连接在一起

c# - .NET - 类似于 Rails 的数据库部署

java - 为什么我会收到 ClassCastException

android - getItemAtPosition() 不在 ListView 中返回值

android - colorAccent 或 colorSecondary 与 Material Component 主题

Android 自定义 Listview 未显示

java - 如何在 Blackberry 项目中存储 32 MB 文件

android - 设置 LinearLayout 相对于同一容器中其他 LinearLayout 的高度

android - 我如何将可绘制图像放入此 Canvas 并让它在此图像上绘制线条?

android - RecyclerView 中延迟加载子级的 RecyclerView 可访问性遍历