Updating content within fragment

I am working on an android project and I am trying to work out how I can use fragments to make a tablet friendly UI for my app. But I am unsure how to update fragment B depending on what happens in fragment A. I know I need some sort of interface but I can't work out how to implement it.

Basically, what I have is an activity called MainActivity which sets the layout for the fragments.

In landscape mode the XML file is.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment android:name="com.BoardiesITSolutions.FragmentTest.FragmentA"
        android:id="@+id/list"
        android:layout_weight="1"
        android:layout_width="0px"
        android:layout_height="match_parent">
    </fragment>
    <FrameLayout android:id="@+id/viewer"
        android:layout_weight="1"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:background="?android:attr/detailsElementBackground">
    </FrameLayout>
</LinearLayout>

In portrait mode its

Currently the MainActivity just sets the content view to the XML file above using SetContentView within in the onCreate method. Below is how it looks.

In the FragmentA class file it extends ListFragment and contains a ListView of items and what I want to be able to do is to update the textview within Fragment B based on what is selected in Fragment A.

Below is the code for fragment A.

@Override
    public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle savedInstanceState)
    {
        return inflator.inflate(R.layout.fragment_a, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);

        myListView = getListView();

        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("Item1");
        arrayList.add("Item2");
        arrayList.add("Item3");

        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), 
                android.R.layout.simple_list_item_activated_1, arrayList);
        setListAdapter(arrayAdapter);

        View fragmentB = getActivity().findViewById(R.id.viewer);
        mDualPane = fragmentB != null && fragmentB.getVisibility() == View.VISIBLE;

        if (savedInstanceState != null)
        {
            mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
        }

        if (mDualPane)
        {
            myListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            showDetails(mCurCheckPosition);
        }
    }

    @Override
    public void onListItemClick(ListView l, View view, int position, long id)
    {
        showDetails(position);
    }

    private void showDetails(int index)
    {
        mCurCheckPosition = index;
        if (mDualPane)
        {
            myListView.setItemChecked(index, true);
            FragmentB details = (FragmentB)getFragmentManager().findFragmentById(R.id.viewer);
            if (details == null || details.getShownIndex() != index)
            {
                details = FragmentB.newInstance(index);

                FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.viewer, details);
                fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                fragmentTransaction.commit();
            }
        }
        else
        {
            Intent intent = new Intent(getActivity(), FragmentBActivity.class);
            intent.putExtra("index", index);
            startActivity(intent);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);
        outState.putInt("curChoice", mCurCheckPosition);
    }

FragmentB contains the following code, this class extends Fragment

public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle savedInstanceState)
    {
        if (container == null)
        {
            return null;
        }
        View view = inflator.inflate(R.layout.fragment_b, container, false);
        return view;
    }

    public static FragmentB newInstance(int index)
    {
        FragmentB fragmentB = new FragmentB();
        Bundle args = new Bundle();
        args.putInt("index", index);
        //rgs.putString("content", content);
        fragmentB.setArguments(args);

        return fragmentB;
    }

    public int getShownIndex()
    {
        return getArguments().getInt("index", 0);
    }

And in the Activity file for FragmentB it contains the following

@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
        {
            finish();
            return;
        }

        if (savedInstanceState == null)
        {
            FragmentB fragmentB = new FragmentB();
            fragmentB.setArguments(getIntent().getExtras());
            getFragmentManager().beginTransaction().add(android.R.id.content, fragmentB).commit();
        }
    }

As you can see from the screenshot above, I have the basis of the fragments working and when I click on each item, it shows what the currently selected item is, but I have no idea how to tell it to update the textview in fragment b based on what the user clicked from fragment a and how this is handled in both portrait and landscape mode.

Thanks for any help you can provide.

Answers


You may override the onActivityCreated() method of FragmentB, find view by id of that TextView, and update it.

Here's a mock:

public class FragmentB extends Fragment{

    //......

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        TextView textView = getActivity().findViewById(R.id.my_textview);
        textView.setText("Hello World!");
    }
}

Need Your Help

Reading fixed width format text tables from HTML page

html xml r parsing table

I am trying to read data from tables similar to the following http://www.fec.gov/pubrec/fe1996/hraz.htm using R but have been unable to make progress. I realize that to do so I need to use XML and...

Canvas, animated text rotation

html5 animation canvas rotation

I want to have a animated canvas rotation of a word, while another word on the canvas does not move. But somehow both stand still. What do I have to change?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.