如何在android中设置默认片段?

时间:2015-04-23 03:44:54

标签: android listview fragment

我已经尝试了几种方法将默认片段设置为第一次打开应用程序时的第一个列表项,以及第一次打开应用程序时最后打开的片段。但这两种情况都不起作用。请帮忙。

  mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int stupid, long id) {
            // getting values from selected ListItem
            name = ((TextView) view.findViewById(R.id.name))
                    .getText().toString();
            Strpath = ((TextView) view.findViewById(R.id.email))
                    .getText().toString();
            String description = ((TextView) view.findViewById(R.id.mobile))
                    .getText().toString();
            source_id = Integer.parseInt(description);
            // Starting single contact activity
            Bundle data = new Bundle();
            data.putString(TAG_NAME,name);
            data.putString(TAG_SOURCES, Strpath);
            Fragment fragment = new SingleContactActivity();
            fragment.setArguments(data);
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.frame_container, fragment).commit();
            mDrawerLayout.closeDrawer(mDrawerList);
        }
    });
new GetContacts().execute();

FMI,我没有在我的程序中使用switch case。我试过了

mDrawerList.setSelection(0);

这是我的数组列表适配器

 ListAdapter adapter = new SimpleAdapter(
                MainActivity.this, navDrawerItems,
                R.layout.list_item, new String[] { TAG_NAME, TAG_SOURCES,
                TAG_ID }, new int[] { R.id.name,
                R.id.email, R.id.mobile });

navDrawerItem,navdrawer中的项目是从MySQL检索

 try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                // Getting JSON Array node
                contacts = jsonObj.getJSONArray(TAG_IPTV);

                // looping through All Contacts
                for (int i = 0; i < contacts.length(); i++) {
                    JSONObject c = contacts.getJSONObject(i);

                    String id = c.getString(TAG_ID);
                    String name = c.getString(TAG_NAME);
                    String email = c.getString(TAG_SOURCES);
                    String address = c.getString(TAG_PACKAGE);



                    // tmp hashmap for single contact
                    HashMap<String, String> contact = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    contact.put(TAG_ID, id);
                    contact.put(TAG_NAME, name);
                    contact.put(TAG_SOURCES, email);
                    contact.put(TAG_PACKAGE, address);


                    // adding contact to contact list
                    navDrawerItems.add(contact);

                }
            } catch (JSONException e) {
                e.printStackTrace();

0 个答案:

没有答案