DarkMalice
DarkMalice

Reputation: 205

setText to Fragment from within Activity

I am trying in vain to simply take a value from FormActivity and use it to fill a EditText in a Fragment. I have to wait for activity to retrieve the value from a remote server and populate the local SQLite table with it, this means no solution I have tried from within the fragment has worked (null error because it hasn't been populated that early). I need help.

Relevant section of FormActivity.java

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 fragment

            FirstFragment f1 = new FirstFragment();

            //put info into a bundle to pass to fragments
            Bundle drawbundle = new Bundle();
            drawbundle.putString("surname", surname);

            f1.setArguments(drawbundle);

            FragmentManager FM = getSupportFragmentManager();
            FragmentTransaction FT = FM.beginTransaction();
            FT.add(R.id.something, f1);
            FT.commit();

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

    private class MyPagerAdapter extends FragmentPagerAdapter {

        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");

            }
        }

Obviously, id.something is not correct, I do not know how to get the id. I read I can use a tag, but my attempt at that did not work. I tried the following:

FirstFragment fragA = (FirstFragment) getSupportFragmentManager().findFragmentByTag("fragA");

Here is my 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;
    //private SQLiteHandler db;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_first, container, false);
        TextView tv = (TextView) v.findViewById(R.id.tvFragFirst);
        tv.setText(getArguments().getString("msg"));

        inputSurname = (EditText) getActivity().findViewById(R.id.surnameText);
        inputSurname.setText(getArguments().getString("surname"));

        return v;
    }

    public static FirstFragment newInstance(String text) {

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

        f.setArguments(b);

        return f;
    }

I'm going around in circles with this, can anyone please help?

Upvotes: 0

Views: 83

Answers (2)

Haris Qurashi
Haris Qurashi

Reputation: 2124

Create interface implement your fragment with it after fragment creation access your activity and save your interface reference in activity, after your operation will complete access your method from activity using saved interface reference and update your fragment. Second option add your fragment with tag and using this fragment manager find your fragment by tag than you have the reference of your fragment than simply call your method inside your activity

Upvotes: 1

Blackbelt
Blackbelt

Reputation: 157437

if you are using a ContentProvider you could use a ContentObserver to get notified when your table is filled up, and when onChange is invoked, you can update the TextView. If you are not using a ContentProvider, yo could use the LocalBroadcastManger and a BroadcastReceiver to achieve the same thing

Upvotes: 1

Related Questions