我正在制作一个有 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
我不知道为什么会这样。如果您需要我的更多代码来理解,我可以提供。
最佳答案
您的 switch 中没有 break 语句,因此对于所有其他情况,它都会落入“user6”情况。
关于android - getItem 的 fragment args.getString 没有给出正确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16093930/