My task is to convert an activity to a Fragment. I need help manipulating my act
ID: 3674428 • Letter: M
Question
My task is to convert an activity to a Fragment. I need help manipulating my activity code to run as a Fragment.
I know I have to convert the on Create() to be onCreateView() method now.
My code for the onCreate()...
I am trying to go off my other fragment code for a difference program for the structure but I am getting stuck...
My code I am going off by...
So how do I convert the first (Activity) onCreate to a (fragment) onCreateView? While using that same structure as the second (fragment) onCreateView. (I am writing this program in Android Studio.
Explanation / Answer
Note: I had edited your code
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
/*Edit the layout*/
View v = inflater.inflate(R.layout.fragment_bug_tracker, container, false);
mTitleField = (EditText) v.findViewById(R.id.bug_title);
mTitleField.setText(mBug.getTitle());
mTitleField.addTextChangedListener(new TextWatcher()
{
/*FUNCTION*/
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
@Override
/*TEXT TO CHANGE FUNCTION*/
public void onTextChanged(CharSequence s, int start, int before, int count)
{
/*Renew*/
mBug.setTitle(s.toString());
/*Message to compile*/
Log.d(Tag, "Title change to " + mBug.getTitle());
}
@Override
public void afterTextChanged(Editable s)
{
}
});
mDescriptionField = (EditText) v.findViewById(R.id.bug_description);
mDescriptionField.setText(mBug.getmDescription());
mDescriptionField.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
mBug.setmDescription(s.toString());
Log.d(Tag, "Set description to " + s.toString());
}
@Override
public void afterTextChanged(Editable s)
{
}
});
/*Acknowldge*/
mDateButton = (Button)v.findViewById(R.id.bug_date);
updateDate();
mDateButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
FragmentManager fm = getActivity().getSupportFragmentManager();
DatePickerFragment dialog = DatePickerFragment.newInstance(mBug.getmDate());
dialog.setTargetFragment(BugDetailsFragment.this, REQUEST_DATE);
/*SHOW THE DATE*/
dialog.show(fm, DIALOG_DATE);
}
});
/*Check the box*/
mSolvedCheckBox = (CheckBox)v.findViewById(R.id.bug_solved);
mSolvedCheckBox.setChecked(mBug.ismSolved());
mSolvedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
mBug.setmSolved(isChecked);
Log.d(Tag, "Set solved status to " + isChecked);
}
});
return v;
/*END MAIN*/
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.