How to change fragment using Android studio navigation drawer - java

Hi can someone help me changing fragments using navigation drawer I tried some guides here none of them work
Here's my code
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
Fragment fragment;
FragmentManager fragmentManager = getSupportFragmentManager(); // For AppCompat use getSupportFragmentManager
switch(position) {
default:
case 0:
fragment = PlaceholderFragment.newInstance(position + 1);
break;
case 1:
fragment = new profile_fragment();
break;
}
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
I want to add this fragment but I always get an error when click the 2nd item from the navigation drawer
package com.the.healthescort;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class profile_fragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #param param1 Parameter 1.
* #param param2 Parameter 2.
* #return A new instance of fragment profile_fragment.
*/
// TODO: Rename and change types and number of parameters
public static profile_fragment newInstance(String param1, String param2) {
profile_fragment fragment = new profile_fragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
public profile_fragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_profile_fragment, container, false);
return rootView;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
public void onFragmentInteraction(Uri uri);
}
}

You don't implement OnFragmentInteractionListener( in profile_fragment) in your MainActivity. Because your MainActivity call profile_fragment.
So you need to implement the listener OnFragmentInteractionListener in your MainActivity. Like that:
public class MainActivity extends ActionBarActivity
implements profile_fragment.OnFragmentInteractionListener {
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
public void onFragmentInteraction(Uri uri);
}
}

use this method for replacing fragmnet :
public void selectItem(int position) {
switch (position) {
case 0:
Fragment fr2 = fragmentManager.findFragmentByTag("orders");
if (fr2 == null) {
fragmentManager
.beginTransaction()
.replace(R.id.sellers_frame_container,
new Sellers_Orders_Fragment(), "orders")
.commit();
} else {
fragmentManager.beginTransaction().show(fr2).commit();
}
break;
case 1:
Fragment fr3 = fragmentManager.findFragmentByTag("add_product");
if (fr3 == null) {
fragmentManager
.beginTransaction()
.replace(R.id.sellers_frame_container,
new Sellers_Add_Product_Fragment(),
"add_product").commit();
} else {
fragmentManager.beginTransaction().show(fr3).commit();
}
break;
}
}
Call this method on listview item click that is in your navigation drawer.
litview.onItemclickListener(.....){
selectItem(postion);
}

Related

java - How to match the details of a Dialog from RecyclerViewAdapter to another Fragment upon clicking the button

I'm new to android coding, I've tried all the possible solution here and on YouTube but still struggling. I just want how to match the data from a dialog of a fragment to another fragment upon pressing the button. we're working on a simple project. :)
Here is what we want to do.Kindly watch this link:
https://imgur.com/tGdWcfq
We want to change the name that will match the dialog upon clicking the "set parameters' button
here's what we found online the difference is we have a dialog button
https://www.youtube.com/watch?v=ZXoGG2XTjzU
https://www.youtube.com/watch?v=69C1ljfDvl0
Here are the codes
RecyclerViewAdapter.java
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
Context mContext;
List<specieList> mData;
Dialog myDialog;
public RecyclerViewAdapter(Context mContext, List<specieList> mData) {
this.mContext = mContext;
this.mData = mData;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v;
Button b;
v = LayoutInflater.from(mContext).inflate(R.layout.row,parent,false);
final MyViewHolder vHolder = new MyViewHolder(v);
myDialog = new Dialog(mContext);
myDialog.setContentView(R.layout.fishpop);
myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
b = myDialog.findViewById(R.id.toasted);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView toastfish = (TextView) myDialog.findViewById(R.id.dialog_fish_id);
Toast.makeText(mContext,"Parameters are now set for " + toastfish.getText().toString(), Toast.LENGTH_SHORT).show();
// here upon clicking this button click we want to match the details in this dialog to another tab. Kindly watch the link above :)
}
});
vHolder.fish_choices.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView dialog_fish = (TextView) myDialog.findViewById(R.id.dialog_fish_id);
TextView dialog_sciname = (TextView) myDialog.findViewById(R.id.dialog_sciname_id);
ImageView dialog_image = (ImageView) myDialog.findViewById(R.id.dialog_image_id);
dialog_fish.setText(mData.get(vHolder.getAdapterPosition()).getFish());
dialog_sciname.setText(mData.get(vHolder.getAdapterPosition()).getSciname());
dialog_image.setImageResource(mData.get(vHolder.getAdapterPosition()).getImage());
myDialog.show();
}
});
return vHolder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.tv_fish.setText(mData.get(position).getFish());
holder.tv_sciname.setText(mData.get(position).getSciname());
holder.img.setImageResource(mData.get(position).getImage());
}
#Override
public int getItemCount() {
return mData.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
private LinearLayout fish_choices;
private TextView tv_fish;
private TextView tv_sciname;
private ImageView img;
public MyViewHolder(View itemView) {
super(itemView);
fish_choices = (LinearLayout) itemView.findViewById(R.id.choices);
tv_fish = (TextView) itemView.findViewById(R.id.textView1);
tv_sciname = (TextView) itemView.findViewById(R.id.textView2);
img = (ImageView) itemView.findViewById(R.id.image);
}
}
}
Code for overview tab(This tab we want to match the content)
public class overview extends Fragment {
View v2;
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public overview() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #param param1 Parameter 1.
* #param param2 Parameter 2.
* #return A new instance of fragment overview.
*/
// TODO: Rename and change types and number of parameters
public static overview newInstance(String param1, String param2) {
overview fragment = new overview();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v2 = inflater.inflate(R.layout.fragment_overview, container, false);
Calendar calendar = Calendar.getInstance();
SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss");
String currentDate = DateFormat.getDateInstance(DateFormat.FULL).format(calendar.getTime());
String currentTime = time.format(calendar.getTime());
TextView textViewDate =(TextView) v2.findViewById(R.id.date_id);
textViewDate.setText(currentDate);
TextView textViewTime =(TextView) v2.findViewById(R.id.time_id);
textViewTime.setText(currentTime);
return v2;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
Code for Specie tab (this tab we want to refer)
public class specie extends Fragment {
View v;
private RecyclerView myrecyclerview;
private List<specieList> lstspecie;
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public specie() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #param param1 Parameter 1.
* #param param2 Parameter 2.
* #return A new instance of fragment specie.
*/
// TODO: Rename and change types and number of parameters
public static specie newInstance(String param1, String param2) {
specie fragment = new specie();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lstspecie = new ArrayList<>();
lstspecie.add(new specieList("Nile Tilapia", "Oreochromis niloticus", R.drawable.tilapia));
lstspecie.add(new specieList("Ayungin (Silver Perch)", "Bidyanus bidyanus", R.drawable.ayungin));
lstspecie.add(new specieList("Sugpo (Tiger Prawn)", "Penaeus monodon", R.drawable.hipon));
lstspecie.add(new specieList("Hito (Catfish)", "Siluriformes", R.drawable.hito));
lstspecie.add(new specieList("Giant Gourami", "Osphronemus goramy", R.drawable.giant));
lstspecie.add(new specieList("Bangus (Milkfish)", "Chanos chanos", R.drawable.bangus));
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_specie, container, false);
myrecyclerview = (RecyclerView)v.findViewById(R.id.specie_recycleview);
RecyclerViewAdapter recyclerAdapter = new RecyclerViewAdapter(getContext(), lstspecie);
myrecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
myrecyclerview.setAdapter(recyclerAdapter);
return v;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
Thank you!
Using object oriented Approach
You can write a function in your activity and you can call a fragment's method from it, as you have your Fragment's reference in your Activity when you initialized it like
In Your Activity
class yourActivity ... {
// your other methods
public void callFragmentMethod(String params) {
// here call your fragment's method
fragment.method(params);
}
}
In your Fragment now
class yourFragment ... {
// your other methods
public void method(String params) {
// here call your fragment's method
here do whatever you want to do it with params
}
}
Now you can call your Activity's method either from another fragment or from Adapter, whatever you want
From Fragment you can call like
((yourActivity)getActivity()).callFragmentMethod(params);
From Adapter
((yourActivity)context).callFragmentMethod(params);
Call below method on your button click..
public void replaceFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frameContainer, fragment);
fragmentTransaction.addToBackStack(fragment.toString());
fragmentTransaction.commit();
}
pass frameContainer in constructor of your Adapter like..
new RecyclerViewAdapter(context, list, R.id.frameContainer);

PageViewer nested inside a fragment

im trying to have a ViewPager inside a Fragment, I've already seen that i have to use getChildFragmentManager() but i cant find a way to make it work.
Basically what i have is a drawer layout to have a side menu where i can choose the fragment i want to use. In one of these fragments i need a ViewPager with 2 sub-fragments.
When i try to run the fragment with the sub-fragments what i usually get is the error "must implement OnFragmentInteractionListener".
The code i actually have, if i run it as an activity, it goes well, but being a fragment makes it crash
public class Calculator extends Fragment implements Basic.OnFragmentInteractionListener, Cientific.OnFragmentInteractionListener{
private SectionsPagerAdapter mSectionsPagerAdapter;
private OnFragmentInteractionListener mListener;
private ViewPager mViewPager;
public Calculator() {
// Required empty public constructor
}
public static Calculator newInstance(String param1, String param2) {
Calculator fragment = new Calculator();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set up the ViewPager with the sections adapter.
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
mViewPager = (ViewPager) getView().findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
//here i already tried using getChildFragmentManager()
// but i dont know if i used it wrong
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_calculator, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
#Override
public void onFragmentInteraction(Uri uri) {
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
public static Fragment newInstance(int sectionNumber) {
Fragment fragment=null;
switch (sectionNumber){
case 1: fragment=new Basic();
break;
case 2: fragment=new Cientific();
break;
}
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.content_main, container, false);
return rootView;
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
#Override
public int getCount() {
// Show 2 total pages.
return 2;
}
}
The context received in onAttach(Context context) is the instance of Activity so to make it work You need to implement OnFragmentInteractionListener in your Activity.

How to initialize a Listview inside a fragment of Tab view android?

I have an activity which has three tabs. One fragment for each tabs. In the xml of the fragment layout I have a listview. I need to initialize this list view and populate it with data from the firebase. How can I do that?
My activity code is
public class AccountActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_account);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(AccountActivity.this,AddLeaveActivity.class));
}
});
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(getArguments().getInt(ARG_SECTION_NUMBER) == 1){
View rootView = inflater.inflate(R.layout.fragment_today, container, false);
return rootView;
} else if(getArguments().getInt(ARG_SECTION_NUMBER) == 2){
View rootView = inflater.inflate(R.layout.fragment_tomorrow, container, false);
return rootView;
} else {
View rootView = inflater.inflate(R.layout.fragment_dayafter, container, false);
return rootView;
}
//View rootView = inflater.inflate(R.layout.activity_account, container, false);
//TextView textView = (TextView) rootView.findViewById(R.id.section_label);
//textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
//return rootView;
}
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "TODAY";
case 1:
return "TOMORROW";
case 2:
return "DAY AFTER";
}
return null;
}
}
}
and my fragment.java is
public class TodayFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public TodayFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #param param1 Parameter 1.
* #param param2 Parameter 2.
* #return A new instance of fragment TodayFragment.
*/
// TODO: Rename and change types and number of parameters
public static TodayFragment newInstance(String param1, String param2) {
TodayFragment fragment = new TodayFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference databaseReference = firebaseDatabase.getReference();
DatabaseReference childRef;
ListView listView;
TextView textView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
textView = (TextView) getView().findViewById(R.id.todayfragmentheader);
textView.setText("Inside onCreate Fragment1");
listView = (ListView) getView().findViewById(R.id.listview);
displaylistview("20170130");
}
private void displaylistview(String str){
childRef = databaseReference.child(str);
// //databaseReference = firebaseDatabase.getReferenceFromUrl(leavedate);
FirebaseListAdapter<Leave> firebaseListAdapter = new FirebaseListAdapter<Leave>(
getActivity(),
Leave.class,
R.layout.leavelistview,
childRef
) {
#Override
protected void populateView(View v, Leave model, int position) {
((TextView)v.findViewById(R.id.lv_staffname)).setText(model.getName());
((TextView)v.findViewById(R.id.lv_type)).setText(model.getType());
((TextView)v.findViewById(R.id.lv_duration)).setText(model.getDuration());
}
};
listView.setAdapter(firebaseListAdapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_today, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
Here the problem is not only the data is populating but also its not setting the text of the textview. But the data is populating and listing if I am using an activity. So I guess the data retrieving code is correct.
So where can is initialize the listview inside a fragment?

Why does onAttach not recognize the correct instanceof context?

I am having troubles getting Android to set my listener. Somehow the context isn't of the type I am expecting it to be. I'm not sure where I am going wrong.
Below is AddEditCharacterFragment.java, where it is throwing an exception because context isn't of the type I expect.
public class AddEditCharacterFragment extends Fragment {
public static final String ARG_PARAM1 = "param1";
private InitiativeTrackerDBHelper mHelper;
private String mParam1;
private Character mCharacter;
public AddEditCharacterFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #return A new instance of fragment AddEditCharacterFragment.
*/
// TODO: Rename and change types and number of parameters
public static AddEditCharacterFragment newInstance() {
AddEditCharacterFragment fragment = new AddEditCharacterFragment();
Bundle args = new Bundle();
//args.putInt(ARG_PARAM1, id);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_add_character, container, false);
mHelper = new InitiativeTrackerDBHelper(getActivity());
mCharacter = mHelper.addCharacter();
EditText characterNameEditText = (EditText) v.findViewById(R.id.character_name_text_edit);
characterNameEditText.setText(mCharacter.getName());
characterNameEditText.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence c, int start, int before, int count) {
mCharacter.setName(c.toString());
}
public void beforeTextChanged(CharSequence c, int start, int before, int after) {
}
public void afterTextChanged(Editable c) {
}
});
EditText modifierPicker =
(EditText) v.findViewById(R.id.modEditText);
modifierPicker.setText(Integer.toString(mCharacter.getModifier()));
modifierPicker.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence c, int start, int before, int count) {
mCharacter.setModifier(Integer.parseInt(c.toString()));
}
public void beforeTextChanged(CharSequence c, int start, int before, int after) {
}
public void afterTextChanged(Editable c) {
}
});
Button saveButton = (Button) v.findViewById(R.id.saveButton);
saveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mHelper != null)
{
mHelper.updateCharacter(mCharacter);
Toast.makeText(getActivity(), "Update complete!", Toast.LENGTH_LONG).show();
mListener.onCharacterSave();
}
}
});
return v;
}
private OnCharacterSave mListener;
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnCharacterSave) {
mListener = (OnCharacterSave) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnCharacterSave {
public void onCharacterSave();
}
}
AddEditCharacterActivity is the activity for the fragment above.
public class AddEditCharacterActivity extends SingleFragmentActivity
implements AddEditCharacterFragment.OnCharacterSave {
#Override
protected Fragment createFragment() {
return AddEditCharacterFragment.newInstance();
}
#Override
public void onCharacterSave() {
FragmentManager fm = getFragmentManager();
// Get the container for the character list
InitiativeListFragment initiativeListFragment = (InitiativeListFragment)
fm.findFragmentById(R.id.fragmentContainer);
// Update the UI
initiativeListFragment.updateInitiativeList();
}
}
InitiativeTrackerActivity which is using an intent to start the AddEditCharacterActivity and subsequently AddEditCharacterFragment.
public class InitiativeTrackerActivity extends SingleFragmentActivity
implements InitiativeListFragment.OnCharacterListListener, AddEditCharacterFragment.OnCharacterSave {
#Override
protected Fragment createFragment() {
return InitiativeListFragment.newInstance();
}
#Override
public void onAddCharacter() {
Intent intent = new Intent(this, AddEditCharacterActivity.class);
startActivity(intent);
}
#Override
public void onCharacterSave() {
FragmentManager fm = getFragmentManager();
// Get the container for the character list
InitiativeListFragment initiativeListFragment = (InitiativeListFragment)
fm.findFragmentById(R.id.fragmentContainer);
// Update the UI
initiativeListFragment.updateInitiativeList();
}
}
And the base class of SingleFragmentActivity for reference:
public abstract class SingleFragmentActivity extends AppCompatActivity {
protected abstract Fragment createFragment();
protected int getLayoutId() {
return R.layout.activity_single_fragment;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutId());
FragmentManager fm = getFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);
if (fragment == null) {
fragment = createFragment();
fm.beginTransaction()
.add(R.id.fragmentContainer, fragment)
.commit();
}
}
}
And InitiativeListFragment.java
package com.example.twistedpurpose.finalproject;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* A simple {#link Fragment} subclass.
* Activities that contain this fragment must implement the
* {#link InitiativeListFragment.OnCharacterListListener} interface
* to handle interaction events.
* Use the {#link InitiativeListFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class InitiativeListFragment extends Fragment {
private InitiativeTrackerDBHelper.CharacterCursor mCursor;
private CharacterCursorAdapter adapter;
private OnCharacterListListener mListener;
public InitiativeListFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #return A new instance of fragment InitiativeListFragment.
*/
public static InitiativeListFragment newInstance() {
return new InitiativeListFragment();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (adapter != null) {
adapter.notifyDataSetChanged();
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_initiative_list, container, false);
//getActivity().deleteDatabase("characters.db");
Context context = getActivity();
// 1. Create a new InitiativeTrackerDBHelper
InitiativeTrackerDBHelper dbHelper = new InitiativeTrackerDBHelper(context);
// 2. Query the characters and obtain a cursor (store in mCursor).
mCursor = dbHelper.queryCharacters();
// Find ListView to populate
ListView characterListView = (ListView) v.findViewById(R.id.character_listView);
// Setup cursor adapter using cursor from last step
adapter = new CharacterCursorAdapter(context, mCursor);
// Attach cursor adapter to the ListView
characterListView.setAdapter(adapter);
Button rollButton = (Button) v.findViewById(R.id.rollBtn);
rollButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
InitiativeTrackerDBHelper dbHelper = new InitiativeTrackerDBHelper(getContext());
List<Character> characterList = dbHelper.getCharacters();
InitiativeRoller.rollInitiative(characterList);
for (Character c : characterList) {
dbHelper.updateCharacter(c);
}
updateInitiativeList();
Toast.makeText(getContext(), "Roll initiative!", Toast.LENGTH_SHORT).show();
}
});
Button addButton = (Button) v.findViewById(R.id.addBtn);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mListener != null) {
mListener.onAddCharacter();
}
}
});
return v;
}
public void updateInitiativeList(){
if(mCursor != null && adapter != null){
mCursor.requery();
adapter.notifyDataSetChanged();
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnCharacterListListener) {
mListener = (OnCharacterListListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnCharacterListListener {
public void onAddCharacter();
}
/**
* A character cursor adaptor for adding characters
* to a list
*/
private static class CharacterCursorAdapter extends CursorAdapter {
private InitiativeTrackerDBHelper.CharacterCursor mCharacterCursor;
public CharacterCursorAdapter(Context context, InitiativeTrackerDBHelper.CharacterCursor cursor) {
super(context, cursor, 0);
mCharacterCursor = cursor;
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// Use a layout inflater to get a row view
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return inflater.inflate(R.layout.character_listview, parent, false);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView characterName = (TextView) view.findViewById(R.id.name);
TextView characterMod = (TextView) view.findViewById(R.id.mod);
TextView characterInit = (TextView) view.findViewById(R.id.init);
characterName.setText(mCharacterCursor.getCharacter().getName());
characterMod.setText(Integer.toString(mCharacterCursor.getCharacter().getModifier()));
characterInit.setText(Integer.toString(mCharacterCursor.getCharacter().getTotalInitiative()));
}
}
}
Your problem is that you do NOT implement the Interface in your activity…
public class AddEditCharacterActivity extends SingleFragmentActivity {
This does not implement OnCharacterSave.
This is what I meant with the earlier comment.
UPDATE:
You are misunderstanding Fragments.
// Get the container for the character list -> This is not true. You're not getting the "Container", you're trying to get an actual instance of a fragment.
InitiativeListFragment initiativeListFragment = (InitiativeListFragment)
fm.findFragmentById(R.id.fragmentContainer);
This would be fine, if that fragment were there.
Let me put it in a more graphic way, this is what you're doing… (give or take)
Start Activity XXX (SingleFragmentActivity).
At some point, InitiativeListFragment in R.id.fragmentContainer is replaced by AddEditCharacterActivity / AddEditCharacterFragment combo.
At this point, R.id.fragmentContainer contains a fragment of type AddEditCharacterFragment.
Since your Activity implements OnCharacterSave, so far so good.
Still in the same activity/fragment combo, you call onCharacterSave() which is implemented (see #4), so all is good.
You then tell the Fragment manager to get you the fragment in R.id.fragmentContainer and you explicitly say (aka: cast) that the Fragment is of the type InitiativeListFragment, but… your Activity should know this is not the case… because the current fragment is AddEditCharacterFragment.
What you ought to do is:
Re-Read about FragmentManager and FragmentTransactions.
If you're going to pass info to another Fragment that is not currently visible/started/attached/etc. then you have to obtain a reference (via TAG if you have one).
Then maybe add it to a container if possible while passing the "data" via a Bundle.
It's completely unclear what you're trying to do and it what order because your code doesn't really have a lot of separation of concerns so as you can see your Activities and Fragments are becoming monolithic monsters full of code and business logic. There are solutions and alternatives (Read about Model-View-Presenter or similar patterns) that can ease the mess while providing an easier environment to test your code.
That being said, regardless of the complexity of your code, I believe you need to understand WHY you're getting the exception, and I have the feeling that you need to practice that a little bit.
In short… when you do findFragmentById, you do get the Fragment (if existing), but you can't just cast it to whatever you want.
OLD COMMENTS:
newInstance() static methods should generally live inside the Fragments and return new YourFragment();
What I mean is the Fragment creation is usually done via a static method IN the fragment.
Say you have
MyFragment extends Fragment {
public static MyFragment newInstance() {
return new MyFragment();
}
public MyFragment() {
// empty constructor is most of the time needed to restore.
}
}
Then from the activity you usually do what you're doing, but the fragment instance is created by calling MyFragment.newInstance(); (this is how Google does it).
I suggest you add your fragment by Tag as well (it's faster). So you do
final Fragment existing = getSupportFragmentManager().findFragmentByTag(tag);
if (existing == null) {
final Fragment newInstance = MyFragment.newInstance();
getSupportFragmentManager()
.beginTransaction()
.add(R.id.fragmentContainer, newInstance, tag)
.commit();
}
Tag is a String and you can keep it in constants (final static String MYFRAGMENT_TAG = "MYFRAGMENT_TAG"; for example).
Are you using Support.V4 fragments? If so you need to change getFragmentManager() to getSupportFragmentManager() (It looks like you are, because you have AppCompatActivity.
Also, the fragment transaction, should be surrounded by if (savedInstaceState == null) { // do it here }
I do not see any problems with creating a fragment instance in your question as stated in other answers.
I think the problem is that your context is AddEditCharacterActivity where you do not implement OnCharacterSave interface.
So you should add:
public class AddEditCharacterActivity extends SingleFragmentActivity implements OnCharacterSave

Issue with switching fragments while using navigation drawer

im having an issue with the navigation drawer. I am using eclipse and using the code it provides for the drawer. I have managed to add additional links to the drawer. When I click on them, the title (in the action bar) changes correctly, but the information doesnt change. I have created all the fragments inside the MainActivity.java file. Below is the code from my MainActivity.java...
MainActivity.java
public class MainActivity extends ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in {#link #restoreActionBar()}.
*/
private CharSequence mTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, SuikodenFragment.newInstance(position + 1))
.replace(R.id.container, SuikodenIIFragment.newInstance(position + 1))
.replace(R.id.container, SuikodenIIIFragment.newInstance(position + 1))
.replace(R.id.container, SuikodenIVFragment.newInstance(position + 1))
.replace(R.id.container, SuikodenVFragment.newInstance(position + 1))
.commit();
}
public void onSectionAttached(int position) {
switch (position) {
case 1:
mTitle = getString(R.string.title_suiko1);
break;
case 2:
mTitle = getString(R.string.title_suiko2);
break;
case 3:
mTitle = getString(R.string.title_suiko3);
break;
case 4:
mTitle = getString(R.string.title_suiko4);
break;
case 5:
mTitle = getString(R.string.title_suiko5);
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* Suikoden 1 Fragment Class
*/
public static class SuikodenFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static SuikodenFragment newInstance(int sectionNumber) {
SuikodenFragment fragment = new SuikodenFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public SuikodenFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.test_drawer_item1, container, false);
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
/**
* Suikoden 2 Fragment Class
*/
public static class SuikodenIIFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static SuikodenIIFragment newInstance(int sectionNumber) {
SuikodenIIFragment fragment = new SuikodenIIFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public SuikodenIIFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.test_drawer_item2, container, false);
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
/**
* Suikoden 3 Fragment Class
*/
public static class SuikodenIIIFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static SuikodenIIIFragment newInstance(int sectionNumber) {
SuikodenIIIFragment fragment = new SuikodenIIIFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public SuikodenIIIFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.test_drawer_item3, container, false);
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
/**
* Suikoden 4 Fragment Class
*/
public static class SuikodenIVFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static SuikodenIVFragment newInstance(int sectionNumber) {
SuikodenIVFragment fragment = new SuikodenIVFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public SuikodenIVFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.test_drawer_item4, container, false);
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
/**
* Suikoden 5 Fragment Class
*/
public static class SuikodenVFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static SuikodenVFragment newInstance(int sectionNumber) {
SuikodenVFragment fragment = new SuikodenVFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public SuikodenVFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.test_drawer_item5, container, false);
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
I have checked this site many many times to come up with a solution and have found some 'solutions' that havent worked. For example, placing the onSectionAttached content inside the onNavigationDrawerItemSelected class. However, this doesnt work or crashes the app. I feel that I am close to figuring it out. Any help would be much appreciated! Thanks!
What is this code:
fragmentManager.beginTransaction()
.replace(R.id.container, SuikodenFragment.newInstance(position + 1))
.replace(R.id.container, SuikodenIIFragment.newInstance(position + 1))
.replace(R.id.container, SuikodenIIIFragment.newInstance(position + 1))
.replace(R.id.container, SuikodenIVFragment.newInstance(position + 1))
.replace(R.id.container, SuikodenVFragment.newInstance(position + 1))
.commit();
It should be like this:
switch(position){
case 0:
fragmentManager.beginTransaction()
.replace(R.id.container, SuikodenFragment.newInstance(position + 1)).commit();
//similar for others
}
I ran into the problem of the action bar not updating properly on Back presses using navigation drawer and also that the fragments just weren't working as intended, I posted a code example on this question here.
Android - How to change fragments in the Navigation Drawer

Categories