I want to send data from Teachers Fragment to OneTeacher Fragment. I found the following code from the internet. But it gives an output as "Null" when I clicked an item of the Listbox.
Please be kind enough to give a solution for this. (My question is why I can't send the data from Teachers Fragment to One Teacher Fragment by using following code).
Teachers Fragment
try {
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView myTextView = (TextView) view.findViewById(R.id.tName);
String teachernamefrom = myTextView.getText().toString();
Toast.makeText(getActivity(), teachernamefrom, Toast.LENGTH_SHORT).show();
//this toast works properly
Bundle bundle = new Bundle();
bundle.putString("key1",teachernamefrom);
getFragmentManager().beginTransaction()
.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
.replace(R.id.fragment_container, new OneTeacherFragment())
.addToBackStack("tag")
.commit();
Fragment nextFrag = new OneTeacherFragment();
nextFrag.setArguments(bundle);
}
});
} catch (Exception e) {
Toast.makeText(getActivity(), e.toString(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
OneTeacherFragment:
teacherName = (TextView) v.findViewById(R.id.teacher_name);
Bundle bundle = getArguments();
if (bundle != null){
String key = bundle.getString("key1");
teacherName.setText(key);
}
else{
Toast.makeText(getActivity(), "Null", Toast.LENGTH_SHORT).show();
}