我正在制作一个我需要为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
我不知道为什么会这样。如果您需要更多我的代码来理解,我可以提供。
最佳答案
您的开关中没有break语句,因此对于其他所有情况,它都属于“ user6”情况。