如何将我的数据库和其他对象从MainActivity发送到我的片段?

时间:2015-02-23 23:12:18

标签: android database android-fragments

我刚刚开始使用Android,我发现这很难理解。我所拥有的是三个不同的片段,我使用NavigationDrawerFragment在片段之间进行切换。我想在ListViews中的这些不同的碎片中显示我的数据库中的信息。

这是我的 MainActivity 中的代码,用于创建数据库以及应用在片段之间的传递方式。

...
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();
}
...

这是我的班级 FragmentEmployee ,我希望在ListView中的 MainActivity 中的数据库中显示所有员工。

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

员工的信息属于Collection-class,所以我发现它只与发送变量&#34; allEmployees&#34;到我的 EmployeeFragment 。但我唯一能找到的帮助是如何使用Bundle和getArguments()发送简单的字符串。我可以在MainActivity中创建我的类变量静态还是一个坏主意?我试图松耦合。

提前多多谢谢。

1 个答案:

答案 0 :(得分:0)

您可以使用Intent或Bundle将信息从一个活动传递到其他碎片 例: Simple example for Intent and Bundle