片段活动中的片段调用startactivityforresult但notifydatasetchanged不起作用

时间:2016-11-17 07:24:47

标签: android notifydatasetchanged startactivityforresult

我有一个片段活动' feedlistActivity'在这里我有一个片段' myPostFragment'这个片段包含一个listView,我从该片段调用了startActivityForResult来开始一个新的活动' MyActivity'。

MyActivity.class

Intent returnIntent = new Intent();
                    returnIntent.putExtra("POST_OBJECT", postData);
                    setResult(Activity.RESULT_OK, returnIntent);
                    finish();

MyPostFragment.class

Intent editIntent = new Intent(context, MyActivity.class);
        editIntent.putExtra("EDIT_POST", postDetailsData);
        startActivity(editIntent);
        break;    

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
        if (requestCode == ADD_EDIT_POST ) {

                if (data != null && data.hasExtra("POST_OBJECT")) {
                    PostDetailsData postData = (PostDetailsData) data
                            .getSerializableExtra("POST_OBJECT");

                    if (postData != null) {
                        myPostFeedList.add(postData);
                        postAdapter.notifyDataSetChanged();
                        setAdapter();
                    }
                }
            }
        }

private void setAdapter() {
    // if (postAdapter == null) {
    postAdapter = new PostListAdapter(context, myPostFeedList);
    postAdapter.setSwipeOptionClickedListner(this);
    myPostfeedListView.setAdapter(postAdapter);

    postAdapter.notifyDataSetChanged();
    // } else {
    // postAdapter.notifyDataSetChanged();
    // }

}

FeedlistActivity.class

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
}

我的问题是,当我打电话给#dat; notifyDatasetChanged'时,上述代码对我不起作用。并且列表不会立即刷新

1 个答案:

答案 0 :(得分:0)

您应该逐步调试,以查找是否执行了 "postAdapter.notifyDataSetChanged()" 功能。