getItem的片段args.getString没有给出正确的结果

时间:2013-04-18 22:08:45

标签: android android-fragments android-viewpager

我正在制作一个有6个用户的Android应用程序,我需要为其提取数据。我设置了Bundle args以容纳两件事;一个带有位置编号的int和一个带有用户名的字符串。这是这样做的。

@Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a DummySectionFragment (defined as a static inner class
        // below) with the page number as its lone argument.
        Fragment fragment = new DummySectionFragment();
        Bundle args = new Bundle();
        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
        switch (position) {
        case 0:
            args.putString(DummySectionFragment.ARG_USER_NAME, "user1");
        case 1:
            args.putString(DummySectionFragment.ARG_USER_NAME, "user2");
        case 2:
            args.putString(DummySectionFragment.ARG_USER_NAME, "user3");
        case 3:
            args.putString(DummySectionFragment.ARG_USER_NAME, "user4");
        case 4:
            args.putString(DummySectionFragment.ARG_USER_NAME, "user5");
        case 5:
            args.putString(DummySectionFragment.ARG_USER_NAME, "user6");
        }
        fragment.setArguments(args);
        return fragment;
    }

之后在我的DummySectionFragment中,我这样做了。

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.ourContainer = container;
        this.ourInflater = inflater;
        View rootView = inflater.inflate(R.layout.fragment_main_dummy,
                container, false);

        //TextView dummyTextView = (TextView) rootView.findViewById(R.id.following);

        this.userPic = (ImageView) rootView.findViewById(R.id.user_Icon);
        this.following = (TextView) rootView.findViewById(R.id.following);
        this.followers = (TextView) rootView.findViewById(R.id.followers);
        this.tweets = (TextView) rootView.findViewById(R.id.tweets);


        //THIS IS THE IMPORTANT PART
        Bundle args = getArguments();
        System.out.println("Current Number: " + args.getInt(ARG_SECTION_NUMBER));
        System.out.println("Current Name: " + args.getString(ARG_USER_NAME));
        this.userName = args.getString(ARG_USER_NAME);
        //THIS IS THE IMPORTANT PART


        m_orders = new ArrayList<Order>();
        m_Users = new ArrayList<twitUser>();
        this.m_adapter = new OrderAdapter(getActivity(), inflater, R.layout.row, m_orders);         
        //setListAdapter(this.m_adapter);

        ListView mListView;
        mListView = (ListView) rootView.findViewById(R.id.list);
        mListView.setAdapter(this.m_adapter);

        new DummySectionFragment.AsyncTest().execute();

        m_ProgressDialog = ProgressDialog.show(getActivity(),    
              "Please wait...", "Retrieving data ...", true);
        return rootView;
    }

然而,我在那里测试的打印语句总是会返回这样的内容。

当前号码:1

当前名称:user6

当前号码:2

当前名称:user6

我不知道为什么会这样。如果您需要更多我的代码来理解我可以提供。

1 个答案:

答案 0 :(得分:1)

您的交换机中没有break语句,因此对于其他每种情况都会出现“user6”情况。