hallo,
ich habe folgendes Problem. Ich habe 2 Fragmente: fragment1 behinhaltet nur ein button und fragment2 nur eine Textview. durch klicken des buttons bei fragment1 möchte ich, dass ein vorderfinierter text bei fragment 2 angezeigt werden soll. nun, durch recherche kam ich auf ein tutorial, welches 2 interfaces für die kommunikation von fragments implementiert. ich bekomme zwar keine fehlermeldung, aber der punkt ist, das keine kommunikation stattfindet, da FragmentCommunicator stets null ist. wie müsste ich das denn instanziieren? mein bisheriger code sieht folgendermaßen aus:
interfaces:
Code
public interface ActivityCommunicator {
public void passDataToActivity(String someValue);
}
public interface FragmentCommunicator {
public void passDataToFragment(String someValue);
}
Alles anzeigen
mainactivity:
Code
public class MainActivity extends FragmentActivity implements ActivityCommunicator {
public FragmentCommunicator fragmentCommunicator;
public int someIntValue = 1;
SectionsPagerAdapter mSectionsPagerAdapter;
Button btn;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button)findViewById(R.id.button1);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
public void SendText(View v) {
if(fragmentCommunicator == null) {
fragmentCommunicator.passDataToFragment("Hi from FragmentActivity");
}
}
@Override
public void passDataToActivity(String someValue) {
// TODO Auto-generated method stub
}
}
Alles anzeigen
fragment1
Code
public class Fragment1 extends Fragment {
Button btn;
public Fragment1(int position) {
// TODO Auto-generated constructor stub
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragmen1, container, false);
btn = (Button)v.findViewById(R.id.button1);
return v;
}
}
Alles anzeigen
fragment2
Code
public class Fragment2 extends Fragment implements FragmentCommunicator {
public Context context;
TextView tv;
private ActivityCommunicator activityCommunicator;
private String activityAssignedValue ="";
public Fragment2(int position) {
// TODO Auto-generated constructor stub
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment2, container, false);
tv = (TextView)v.findViewById(R.id.textView1);
return v;
}
@Override
public void onResume(){
super.onResume();
int activityValue =((MainActivity)context).someIntValue;
if(activityValue == 0){
//show one view
}else{
// show other view
}
}
@Override
public void onAttach(Activity activity){
super.onAttach(activity);
context = getActivity();
activityCommunicator =(ActivityCommunicator)context;
}
@Override
public void passDataToFragment(String someValue) {
activityAssignedValue = someValue;
tv.setText(someValue);
}
}
Alles anzeigen
fragment1.xml
Code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="SendText"
android:text="Button" />
</LinearLayout>
Alles anzeigen
fragment2.xml
Code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Alles anzeigen
main.xml
Code
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<!--
This title strip will display the currently visible page title, as well as the page
titles for adjacent pages.
-->
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff" />
</android.support.v4.view.ViewPager>
Alles anzeigen
danke im voraus für die ratschläge und hilfsbereitschaft