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

Android 如何将数据库和其他对象从MainActivity发送到片段?,android,database,android-fragments,Android,Database,Android Fragments,我刚刚开始使用安卓系统,我发现这很难理解。我有三个不同的片段,我使用NavigationDrawerFragment在片段之间切换。我想在ListView的这些不同框架中显示数据库中的信息 这是myMainActivity中创建数据库的代码,以及应用程序如何在片段之间运行 ... private NavigationDrawerFragment mNavigationDrawerFragment; private DatabaseHandler dbHandler; private Collec

我刚刚开始使用安卓系统,我发现这很难理解。我有三个不同的片段,我使用NavigationDrawerFragment在片段之间切换。我想在ListView的这些不同框架中显示数据库中的信息

这是myMainActivity中创建数据库的代码,以及应用程序如何在片段之间运行

...
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,我想在这里以列表视图的形式显示我的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);
}
公共类FragmentEmployees扩展了Fragment{
//TODO:重命名参数参数,选择匹配的名称
//片段初始化参数,例如ARG_ITEM_NUMBER
私有静态最终字符串ARG_PARAM1=“PARAM1”;
私有静态最终字符串ARG_PARAM2=“PARAM2”;
//TODO:重命名和更改参数类型
私有字符串mParam1;
私有字符串mParam2;
私有OnFragmentInteractionListener mListener;
私有ListView listViewEmployees;
/**
*使用此工厂方法创建的新实例
*使用提供的参数创建此片段。
*
*@param param1参数1。
*@param param2参数2。
*@return fragmentemoployes的新实例。
*/
//TODO:重命名和更改参数的类型和数量
公共静态FragmentEmployees newInstance(字符串param1,字符串param2){
FragmentEmoployees fragment=新的FragmentEmoployees();
Bundle args=新Bundle();
args.putString(ARG_PARAM1,PARAM1);
args.putString(ARG_PARAM2,PARAM2);
fragment.setArguments(args);
返回片段;
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
如果(getArguments()!=null){
mParam1=getArguments().getString(ARG_PARAM1);
mParam2=getArguments().getString(ARG_PARAM2);
}
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=充气机。充气(R.layout.fragment\u员工,容器,假);
listViewEmployees=(ListView)view.findViewById(R.id.listViewEmployees);
//设置listview以显示数据库中的所有员工。但如何设置?。。。
返回视图;
}
//TODO:重命名方法、更新参数并将方法挂接到UI事件中
public void onButtonPressed(Uri){
if(mListener!=null){
onFragmentInteraction(uri);
}
}
@凌驾
公共事务主任(活动){
超级转速计(活动);
}
@凌驾
公共无效连接(){
super.onDetach();
mListener=null;
}
/**
*此接口必须由包含以下内容的活动实现
*片段,以允许通信此片段中的交互
*该活动以及其中可能包含的其他片段
*活动。
*

*有关更多信息,请参阅Android培训课程。 */ FragmentInteractionListener上的公共接口{ //TODO:更新参数类型和名称 公共void onFragmentInteraction(Uri); }

员工的信息在一个集合类中,因此我发现只将变量“allEmployees”发送到我的EmployeeFragment是相关的。但是我能找到的唯一帮助是如何使用Bundle和getArguments()发送简单字符串.我可以在MainActivity中使我的类变量为静态的吗?或者这是一个坏主意吗?我正在尝试松散耦合


非常感谢。

您可以使用Intent或Bundle将信息从一个活动传递到其他片段。
例子:

我正在尝试松散耦合…然后以正确的方式执行此操作:使用
ContentProvider
并只将员工的uri传递给片段(例如:
content://my.super.authority/emoployees/1
)fragment应该自己负责从ContentProvider加载数据…好的,非常感谢!我会尝试一下,尽管阅读文档我发现这更难理解。实现ContentProvider不是一件容易的工作…但是一旦你得到了它,你就再也不想在android平台上使用其他东西了(比如ORM/POJO)(这只是我的意见:))