访问listview的成员适配器时onActivityResult意外的行为

时间:2017-05-02 09:57:13

标签: android listview android-arrayadapter

我使用它来开始结果活动

    Intent pickIntent = new Intent(this, PickerActivity.class);
    pickIntent.putExtra(PickerActivity.OBJECTS, new String[] { "One", "Two" });
    pickIntent.putExtra(PickerActivity.TITLE, getString(R.string.dummyTitle));
    startActivityForResult(pickIntent, PickerActivity.REQUEST);

我在选择后调用了

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == PickerActivity.REQUEST)
    {
        if (resultCode == Activity.RESULT_OK)
        {
            adapter.add(data.getStringExtra("result"));
            adapter.notifyDataSetChanged(); // Not required but put to prove a point
        }
    }
}

保存适配器的列表视图未更新。

然而,如果我使用

,它会起作用
        adapterData.add(data.getStringExtra("result")); // The list that was passed to the list adapter
        adapter.notifyDataSetChanged();

甚至

        Handler mainHandler = new Handler(context.getMainLooper());

        Runnable myRunnable = new Runnable()
        {
            @Override
            public void run()
            {
                 adapter.add(data.getStringExtra("result"));
            }
        };
        mainHandler.post(myRunnable);

有人可以解释原因吗?

0 个答案:

没有答案