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.