0

I have a problem with data passing between two fragments. There are two fragments in one activity. In Fragment1 i have a listView with layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp" >

<ImageView
    android:id="@+id/imageView01"
    android:layout_width="100dip"
    android:layout_height="100dip"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:scaleType="fitXY"
    android:src="@drawable/icon" />

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/titleView1"
    android:layout_alignTop="@+id/imageView01"
    android:text="add to favorites" />

<TextView
    android:id="@+id/titleView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/checkBox1"
    android:layout_toRightOf="@+id/imageView01"
    android:text="text"
    android:textColor="#FF2F22"
    android:textSize="16sp"
    android:textStyle="bold" />

</RelativeLayout>

activity code

public class MainActivity extends FragmentActivity {
private PagerAdapter mPagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewpager_layout);
    initialisePaging();     
}

private void initialisePaging() {       
    List<Fragment> fragments = new Vector<Fragment>();
    fragments.add(Fragment.instantiate(this, Fragment1.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment2.class.getName()));       
    mPagerAdapter = new PagerAdapter(this.getSupportFragmentManager(), fragments);
    ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
    pager.setAdapter(mPagerAdapter);
}
}

fragment1 code

public class Fragment1 extends Fragment {
private ListView listViewImages;
private EditText txtSearchText;
private ListViewImageAdapter adapter;
private ArrayList<Object> listImages;
private Fragment activity;
String strSearch = null;    

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = (LinearLayout) inflater.inflate(R.layout.fragment1_layout, container, false);
    activity = this;
    listViewImages = (ListView) view.findViewById(R.id.lviewImages);
    ...
    return view;
}

in my ListViewAdapter i fill a listView with ImageView, TextView and CheckBox. For checkBox i want to set a onClickListener (when i check checkBox the data must be passed to Fragment2 )

CheckBox call = (CheckBox) vi.findViewById(R.id.checkBox1);         
   call.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
        // HERE DATA MUST BE PASSED TO Fragment2
        RelativeLayout rl = (RelativeLayout) v.getParent();
    TextView txtViewTitle = (TextView) rl.findViewById(R.id.titleView1);
    String text = txtViewTitle.getText().toString();                    
    Log.i("!!!", "Click on CheckBox"+text);
}
  });

So. My question is how can I pass the data to another Fragment, when I press checkBox (not ListView item!!!) ? I have no ListFragment, it's a simple Fragment with ListView , each item of ListView contains ImageView, CheckBox, TextBox. Thanks.

zond
  • 1,473
  • 1
  • 21
  • 34

1 Answers1

2

I do not have my IDE with me, but i made you a simple example to show you the principle of passing data through MainActivity to other Fragments.

MainActivity

class MainActivity{
    //Globalize your Fragments
    Fragment1 mFrag1;
    Fragment2 mFrag2;
    //Init those two Fragments and add them to your layout;

    public void sendDataToFrag1(String data){
        mFrag1.setData(data);
    }


    public void sendDataToFrag2(String data){
        mFrag2.setData(data);
    }
}

Frag1 or Frag2

class Fragment1{
//.... oncreate and so on

   //perhaps on Buttonclick
   new OnClickListeneter{
        @Override
        onClick(View v){
            passData("The Button was clicked");
        }
   }

   //here comes the value from the other fragment
    public void setData(String data){
        myTextView.setText(data);
    }

    //Here you send some data over MainActivity to the other Fragment
    private void passData(String data){
        ((MainActivity)getActitivity()).sendDataToFrag2(data)
    }

}
A.S.
  • 4,574
  • 3
  • 26
  • 43