无法从活动

时间:2015-05-16 21:01:16

标签: java android android-fragments

所以,我回过头来尝试从Activity中更新Fragment中的EditText内容。 我应该使用它的位置从viewpager中获取正确的片段对象,但是名为drawSurname(surname)的方法仍在给我“无法解析”#39;错误。

FormActivity的相关代码;

//initialise pager for swipe screens
        ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
        adapter = new MyPagerAdapter(getSupportFragmentManager());
        pager.setAdapter(adapter);

public void DrawText() {
        // Fetching user details from sqlite
        HashMap<String, String> user = db.getUserDetails();

        if (user.size() != 0) {
            Log.e(TAG, "string surname: " + surname);

            // Displaying the user details on the screen
            Fragment n = adapter.getRegisteredFragment(0);
            n.drawSurname(surname);

        }else{
            Log.e(TAG, "table empty");
        }
    }

public class MyPagerAdapter extends FragmentStatePagerAdapter {
        SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();

        public MyPagerAdapter(FragmentManager fm) {

            super(fm);
        }

        @Override
        public Fragment getItem(int pos) {
            switch(pos) {

                case 0: return FirstFragment.newInstance("@string/form_instruct");
                case 1: return SecondFragment.newInstance("SecondFragment, Instance 1");
                case 2: return ThirdFragment.newInstance("ThirdFragment, Instance 1");
                case 3: return FourthFragment.newInstance("FourthFragment, Instance 1");
                //case 4: return FifthFragment.newInstance("ThirdFragment, Instance 3");
                default: return FirstFragment.newInstance("FirstFragment, Default");
            }
        }

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

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Fragment fragment = (Fragment) super.instantiateItem(container, position);
            registeredFragments.put(position, fragment);
            return fragment;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            registeredFragments.remove(position);
            super.destroyItem(container, position, object);
        }

        public Fragment getRegisteredFragment(int position) {
            return registeredFragments.get(position);
        }
    }

FirstFragment.java

public class FirstFragment extends android.support.v4.app.Fragment {

    //create variables & Logcat tag
    private static final String TAG = FirstFragment.class.getSimpleName();
    private EditText inputTitle;
    private EditText inputName;
    private EditText inputSurname;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_first, container, false);

        //set inputs for fields
        inputTitle = (EditText) v.findViewById(R.id.titleText);
        inputName = (EditText) v.findViewById(R.id.foreText);
        inputSurname = (EditText) v.findViewById(R.id.surnameText);

        TextView tv = (TextView) v.findViewById(R.id.tvFragFirst);
        tv.setText(getArguments().getString("msg"));

        inputSurname = (EditText) getActivity().findViewById(R.id.surnameText);

        return v;
    }
    public static FirstFragment newInstance(String text) {

        FirstFragment f = new FirstFragment(text);
        Bundle b = new Bundle();
        b.putString("msg", text);

        f.setArguments(b);

        return f;
    }

    public void drawSurname(String x) {

        inputSurname.setText(x);
    }
}

我确信我已经使用getRegisteredFragment()正确地获得了片段,但是无法理解为什么该方法不会以其他方式调用。

1 个答案:

答案 0 :(得分:3)

问题来自于Fragment类不应该有一个名为drawSurname但是FirstFragment的函数。要解决此问题,请创建一个名为SuperFrag的扩展Fragment的抽象类,所有自定义片段都将从该类继承。在SuperFrag中添加一个名为drawSurname的函数。您可以在继承类中覆盖它。最后,SparseArray应该是SuperFrag的列表