0

how can I get elements of the fragment in my activity and change their values?

Hi have this fragment...

public class EventDetail extends Fragment {


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

    View v = inflater.inflate(R.layout.event_detail, container, false);

    return v;

}

}

fragment_layout...

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/bg"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imgEventCover"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:adjustViewBounds="true"
        android:src="@drawable/imagemapoio2" />

    <TextView
        android:id="@+id/txtEventDetail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/txtEventInfo"
        android:layout_centerVertical="true"
        android:text="LXFACTORY   |   2km"
        android:textColor="@android:color/black" />

    <TextView
        android:id="@+id/txtEventInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtEventDetail"
        android:layout_centerHorizontal="true"
        android:text="Playground present Drum&amp;Bass in the Factory"
        android:textColor="@color/dk" />

    <ImageView
        android:id="@+id/imgTiming"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_below="@+id/txtEventTime"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="25dp"
        android:src="@drawable/yellowtiming" />

    <TextView
        android:id="@+id/txtEventTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/txtEventDetail"
        android:layout_below="@+id/txtEventInfo"
        android:layout_marginTop="5dp"
        android:text="22.00H - 04.30H"
        android:textColor="@color/dk"
        android:textSize="10dp" />

</RelativeLayout>

Main activity...(method to change values)

public void initializeEvent() {
    Fragment fr = new EventDetail();
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
    View v = fr.getView();

    //get fragment elements.

    fragmentTransaction.replace(R.id.fragment_menu, fr);
    fragmentTransaction.commit();
}

How can i get the textviews and imageviews from this fragment to set their images and text?

Pedro Simão
  • 283
  • 1
  • 11
  • It's Fragment's layout and you should set it from within Fragment. Any special reason why you want to do it from your Activity? – Sharjeel Feb 21 '14 at 02:36
  • I've a list in my activity and when I click in one item it display a fragment with some data about that item. So that data should be dynamic. – Pedro Simão Feb 21 '14 at 02:47

2 Answers2

1

something like this?

((EventDetail)getActivity()).function(data);
GregM
  • 3,624
  • 3
  • 35
  • 51
  • try this then http://stackoverflow.com/questions/18486952/get-id-of-textview-in-fragment-from-fragmentactivity-in-viewpager – GregM Feb 21 '14 at 02:36
0

You shouldn't set any values directly via method either from Activity or Fragment.

From Activity to Fragment,

  • You should add appropriate values in the bundle which you can send via setArguments(bundle), and in fragment you can later retrive those values via getArguments()

From Fragment to Activity,

  • You should create an interface and use its method to pass the values from Fragment to Activity
shaktiman_droid
  • 2,368
  • 1
  • 17
  • 32