0

我一直在兜圈子,试图做一些看起来很基本的事情。我有一个DialogFragment接受用户输入的 a ,然后在提交时刷新 aListView中的 a Fragment,它是 a 的一部分ViewPager

我有一切工作除了FragmentListView刷新本身。这有点令人困惑,因为它确实刷新了数据,但我必须滑动几个视图,然后再返回查看更新的数据。

在做了一些研究之后,我应该使用getItemPositionandnotifyDataSetChanged并且ViewPager它应该可以工作。问题是调用notifyDataSetChangedRecursive entry to executePendingTransactions引发异常:

主要活动

public class Main extends SherlockFragmentActivity implements MyListFragment.OnRefreshAdapterListener, DialogConfirmation.OnRefreshKeywordsListener //Updated Code
{
    private static List<Fragment> fragments;

    @Override
    public void onCreate(final Bundle icicle)
    {    
        setContentView(R.layout.main);
    }

    @Override
    public void onResume()
    { 
        mViewPager = (ViewPager)findViewById(R.id.viewpager);

        fragments = new ArrayList<Fragment>();

        fragments.add(new MyListFragment()); //fragment with the ListView

        fragments.add(MyDetailFragment.newInstance(0));
        fragments.add(MyDetailFragment.newInstance(1));
        fragments.add(MyDetailFragment.newInstance(2));

        mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mMyFragmentPagerAdapter);
    }

    private static class MyFragmentPagerAdapter extends FragmentStatePagerAdapter  {  

        public MyFragmentPagerAdapter(FragmentManager fm) {  
             super(fm);  
        }  

        @Override  
        public Fragment getItem(int index) {
            return fragments.get(index);
        }  

        @Override
        public int getCount() {  
             return 4;  
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }
   }

    @Override
    public void onRefreshAdapterListener() {
        this.mMyFragmentPagerAdapter.notifyDataSetChanged();
    }

    //Updated Code
    @Override
    public void onRefreshTextListener() {
        MyListFragment tf = (MyListFragment)getSupportFragmentManager().findFragmentById(R.id.fragmentText);

        if (tf == null)
            tf = (MyListFragment)this.fragments.get(0);

            tf.RefreshText();       
    }
}

列表片段

public class MyListFragment extends SherlockListFragment
{
    OnRefreshAdapterListener mRefreshAdapter;

    @Override
    public void onActivityCreated(Bundle savedState) {

        adapter = new CustomAdapter();

        /*code to add items to adapter */

        this.setListAdapter(adapter);

        adapter.notifyDataSetChanged();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        if (getArguments() != null && getArguments().getString("text").length() > 0)
        {
            SaveText(getArguments().getString("text"));
            this.mRefreshAdapter.onRefreshAdapterListener(); //this line causes a "java.lang.IllegalStateException: Recursive entry to executePendingTransactions" exception
        }

        return inflater.inflate(R.layout.listing, container, false);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        mRefreshAdapter = (OnRefreshAdapterListener)activity;
    }

    public interface OnRefreshAdapterListener {
        public void onRefreshAdapterListener();
    }


    @Override
    public void onDialogTextAdd(final String text) {
    }   
}

对话片段

public class DialogTextAdd extends DialogFragment implements OnEditorActionListener {

    private EditText mText;
    OnRefreshTextListener mTextKeywords; //Updated Code

    public interface DialogTextAddListener {
        void onDialogTextAdd(final String inputText);
    }

    public DialogTextAdd() {
        // Empty constructor required for DialogFragment
    }

    //Updated Code
    @Override
    public void onAttach(Activity act) {
        super.onAttach(act);
        mTextKeywords = (OnRefreshTextListener)act;
    }

    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.dialog_edit, container);
        mText = (EditText)view.findViewById(R.id.text_add);
        getDialog().setTitle("Add Text");

        // Show soft keyboard automatically
        mText.requestFocus();
        getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        mText.setOnEditorActionListener(this);

        return view;
    }

    @Override
    public boolean onEditorAction(final TextView v, final int actionId, final KeyEvent event) {
        if (EditorInfo.IME_ACTION_DONE == actionId) {

            MyListFragment mf = new MyListFragment();
            Bundle args = new Bundle();
            args.putString("text", mText.getText().toString());
            mf.setArguments(args);

            //this seems to be intefering with the notifyDataSetChanged in the listing fragment
            getActivity().getSupportFragmentManager().beginTransaction().add(mf, "my_fragment").commit();

            mTextKeywords.onRefreshTextListener(); //Updated Code

            this.dismiss();
            return true;
        }
        return false;
    }
}
4

1 回答 1

1

除了带有 ListView 的 Fragment 不会自行刷新之外,我一切正常。

创建和FragmentActivity添加MyListFragment. 从您的代码看来,您将使用的片段存储在列表中,因此您可以引用它们(此外,出于好奇,您是否将片段设置为纵向,是否旋转手机并重试使用DialogFragment? )。引用这些片段意味着您始终可以从列表中获取它们并使用它们来调用刷新/更新方法。

于 2013-01-25T06:54:19.087 回答