I want to set the text of some EditTexts after clicking on an item of a list.
The activity:
- CadPersonActivity
And three fragments, each one having fields:
- FragmentPersonId
- FragmentPersonAddress
- FragmentPersonPhones
However those EditTexts insist on not showing what I expect.
There's no error, but blank fields.
I hope someone can help me.
Thanks in advance.
THE ACTIVITY
CadPersonActivity recover the Id in Bundle extras:
public class CadPersonActivity extends FragmentActivity implements TabListener {
private ActionBar actionBar;
private ViewPager viewPager;
private PersonPageAdapter pageAdapter;
private Person person;
private PersonDAO bd;
private Bundle extras;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cad_person);
extras = getIntent().getExtras();
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
pageAdapter = new PersonPageAdapter(getSupportFragmentManager());
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(pageAdapter);
viewPager.setPageTransformer(true, new ZoomOutPageTransformer());
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
});
actionBar.addTab(actionBar.newTab().setText(getString(R.string.tab_id)).setTabListener(this));
actionBar.addTab(actionBar.newTab().setText(getString(R.string.tab_address)).setTabListener(this));
actionBar.addTab(actionBar.newTab().setText(getString(R.string.tab_phones)).setTabListener(this));
if (extras.getInt("id") > 0) {
bd = new PersonDAO(getApplicationContext());
person = bd.getPerson(extras.getInt("id"));
callSetFields();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (bd != null)
bd.close();
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
private void callSetFields() {
try {
FragmentPersonId fragId = (FragmentPersonId) getSupportFragmentManager().findFragmentById(R.id.fragment_id);
fragId.setFields(person);
FragmentPersonAddress fragEndereco = (FragmentPersonAddress) getSupportFragmentManager().findFragmentById(R.id.fragment_endereco);
fragEndereco.setFields(person);
FragmentPersonPhones fragPhones = (FragmentPersonPhones) getSupportFragmentManager().findFragmentById(R.id.fragment_phones);
fragPhones.setFields(person);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "An error occurred while trying to open the record.", Toast.LENGTH_SHORT).show();
}
}
}
CallSetFields is called when a record is clicked and its Id is passed to CadPersonActivity.
The activity_cad_person.xml:
<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">
<fragment
android:id="@+id/fragment_id"
android:name="com.fragment.FragmentPersonId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
tools:layout="@layout/fragment_id" />
<fragment
android:id="@+id/fragment_address"
android:name="com.fragment.FragmentPersonAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
tools:layout="@layout/fragment_address" />
<fragment
android:id="@+id/fragment_phones"
android:name="com.fragment.FragmentPersonPhones"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
tools:layout="@layout/fragment_phones" />
</android.support.v4.view.ViewPager>
THE FRAGMENTS:
And each fragment have a code like this:
public class FragmentPersonId extends Fragment {
private EditText edtName;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_id, container,
false);
return view;
}
public void setFields(Pessoa pessoa) {
try {
edtName = (EditText) getView().findViewById(R.id.edtName);
edtName.setText(person.getName());
} catch (Exception e) {
Toast.makeText(getActivity().getApplicationContext(), "Error while setting fields.", Toast.LENGTH_SHORT).show();
}
}
}