chucklay
chucklay

Reputation: 137

NotifyDataSetChanged from fragment container

I have an app with an Activity that hosts a ListFragment, an EditText, and a Button. When the user presses the Button, I want to add the text in the EditText to the ListFragment. I know to do this, I would have to call the Fragment's adapter's notifyDataSetChanged() method, but I don't now how I would go about doing that. I also don't know how I would call the adapter's add method, but I get the feeling that if I figured out how to do one, I'll be able to figure out how to do the other.

Any help would be awesome.

Edit: Further clarification:

So the hosting Activity (ThreadContainer.java) inflates its layout, which contains the ListFragment, like so:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_thread_container);
        ...

Here's activity_thread_container.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context="com.claymon.android.cryptosms.ThreadContainer">

<fragment
    android:id="@+id/thread_container"
    android:name="com.claymon.android.cryptosms.MessageFragment"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_above="@+id/newMessageText"/>

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/newMessageText"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:inputType="text|textMultiLine"
    android:focusable="true"
    android:maxLines="4"
    android:maxLength="2000"
    android:scrollHorizontally="false"
    android:stackFromBottom="true"
    android:transcriptMode="alwaysScroll"
    android:layout_toLeftOf="@+id/sendButton"
    android:layout_toStartOf="@+id/sendButton"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/send"
    android:id="@+id/sendButton"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"/>

Then, MessageFragment, which extends ListFragment, just displays some predetermined data using a fairly simple custom ArrayAdapter. The code here is more or less irrelevant. What I want to do is call my custom ArrayAdapter's methods from within ThreadContainer.java. Basically, I need to figure out how to access the fragment that I created when I inflated my layout.

Sorry for the poor initial question.

Upvotes: 1

Views: 371

Answers (1)

Elltz
Elltz

Reputation: 10859

What I want to do is call my custom ArrayAdapter's methods from within ThreadContainer.java. Basically, I need to figure out how to access the fragment that I created when I inflated my layout

when you are adding the Fragment supply a Tag to it and retrieve it via the Tag FragmentMananger.findFragmentByTag("") returns a Frament then you cast the Fragment to your preferred class and call your method

Upvotes: 1

Related Questions