I am trying to pass values through a Bundle
using setArguments()
from an activity to another class extends Fragment
when I press one of the listed items in a ListView
.
As shown below, I used a Log.i
statement to know if the Bundle
received is null
or not. Unfortunately, it is always null
and consequently, no data shows on the designated TextView
of the class that extends Fragment
. What I am missing or what is wrong with the code.
FragmentClass
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragmentclasslayout, null);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
if (getArguments() != null) {
Log.i(TAG, "getArgument is not null");
int value = getArguments().getInt("pos");
TextView tv = (TextView) getView().findViewById(R.id.tvID);
tv.setTextSize(value);
tv.setText("size = "+value*10);
}else {
Log.i(TAG, "getArgument is null");
}
}
MainClass_implements_onItemSelectedListener
:
setContentView(R.layout.mainactivity);
ListView mListView = (ListView) findViewById(R.id.list);
ArrayAdapter<String> mArrayAdapter = new ArrayAdapter<String>(getApplicationContext(),
R.layout.listviewlayout, R.id.tv, string);
mListView.setAdapter(mArrayAdapter);
mListView.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Fragment f = new FragmentClass();
Bundle bundle = new Bundle();
bundle.putInt("pos", (position+1));
FragmentClass fc = new FragmentClass().newInstance(bundle);
//f.setArguments(bundle);
FragmentTransaction t =
getFragmentManager().beginTransaction();
t.replace(R.id.fragment00ID, f);
t.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
t.addToBackStack(null);
t.commit();
}
mainActivity_layout
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
tools:ignore="MergeRootFrame">
<ListView
android:id="@+id/list"
android:layout_width="100px"
android:layout_weight="1"
android:layout_height="match_parent" />
<fragment
android:name="com.example.fragmentwithlistview.FragmentClass"
android:id="@+id/fragment00ID"
android:layout_toRightOf="@+id/list"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="4" />
FragmentClass_layout
:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:ignore="MergeRootFrame"
android:background="#FFFFFF">
<TextView
android:id="@+id/tvID"
android:textColor="#000000"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>