0

I've just started working with Android and I find this so hard to understand. What I have is three different fragments and I'm using a NavigationDrawerFragment to go between the fragments. I want to display information from my database in these different fragmets in ListViews.

This is the code in my MainActivity that creates the Database and how the app goes between fragments.

...
private NavigationDrawerFragment mNavigationDrawerFragment;
private DatabaseHandler dbHandler;
private CollectionEmployee allEmployees;

/**
 * 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 database.
    dbHandler = new DatabaseHandler(this);
    allEmployees =  new CollectionEmployee(dbHandler.getAllContacts(), dbHandler);
    allEmployees.addEmployee(new Employee(10,"Kim D","07","da@gmail.com","ki3"));

    // 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();
    Fragment myFragment;

    switch(position){
        case 0: myFragment = new FragmentEmoployees();
            break;
        case 1: myFragment = new FragmentProjects();
            break;
        case 2: myFragment = new FragmentDomains();
            break;
        default: myFragment = new FragmentEmoployees();
            break;
    }

    fragmentManager.beginTransaction()
            .replace(R.id.container, myFragment)
            .commit();
}
...

This is my class FragmentEmployee where I want to display all the employees, from the Database in my MainActivity, in a ListView.

public class FragmentEmoployees 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;
private ListView listViewEmployees;


/**
 * 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 FragmentEmoployees.
 */
// TODO: Rename and change types and number of parameters
public static FragmentEmoployees newInstance(String param1, String param2) {
    FragmentEmoployees fragment = new FragmentEmoployees();
    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) {
    View view = inflater.inflate(R.layout.fragment_employee, container, false);
    listViewEmployees = (ListView) view.findViewById(R.id.listViewEmployees);

    //Set up listview so it displays all employees from database.. but how?...

    return view;
}

// 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);

}

@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);
}

The information of the employees are in a Collection-class so I find it relevant to just send variable "allEmployees" to my EmployeeFragment. But the only help I can find is how to send simple Strings by using Bundle and getArguments(). Could I make my class variables in MainActivity static or is that a bad idea? I'm trying to have loose coupling.

Thanks alot in advance.

Kim
  • 127
  • 12
  • *I'm trying to have loose coupling* ... then do this in right way: use `ContentProvider` and pass just employee's uri to fragment (like: `content://my.super.authority/emoployees/1`) fragment should take care of loading the data from ContentProvider by itself ... – Selvin Feb 23 '15 at 23:15
  • Okey thanks alot! I'll try this, though reading the documentation I find this even harder to understand. – Kim Feb 23 '15 at 23:29
  • Implementing ContentProvide is not an easy job ... but once you will get it you will never want to use other stuff(like ORM/POJO) anymore on android platform(it is just my opinion :) ) – Selvin Feb 23 '15 at 23:35

1 Answers1

0

You can use Intent or Bundle to pass information from one activity to other Fragments.
Example: Simple example for Intent and Bundle

Community
  • 1
  • 1
KulArtist
  • 965
  • 8
  • 20