getArguments().getInt() 从“1”开始返回,而不是“0”
getArguments().getInt() starts returning from "1", instead "0"
请帮我解决一下?
我有一个带有 ViewPager
和 5 个 RecyclerViewFragment
的 Activity
,每个 RecyclerFragment
都包含动态 CardView
。
ViewPager
适配器的代码:
public class CustomFragmentPagerAdapter extends FragmentStatePagerAdapter {
private Board board;
private long boardID;
public CustomFragmentPagerAdapter(FragmentManager fm, Board board, long id) {
super(fm);
this.board = board;
this.boardID = id;
}
@Override
public int getCount() {
return 5;
}
@Override
public CharSequence getPageTitle(int position) {
return board.getMyLists().get(position).getName();
}
//Here I define fragment, int position - is a position of Fragment in ViewPager
@Override
public Fragment getFragment( int position) {
**return RecyclerViewFragment.newInstance(boardID, position);**
}
}
接下来是 Fragment 的新实例:
public static RecyclerViewFragment newInstance(long id, int listPosition){
Bundle args = new Bundle();
args.putLong(BUNDLE_ID, id);
args.putInt(BUNDLE_LIST_POSITION, listPosition);
RecyclerViewFragment fragment = new RecyclerViewFragment();
fragment.setArguments(args);
return fragment;
}
我通过 Bundle 存储 片段的位置 - listPosition
然后我通过getArguments()
在OnCreateView中得到那个位置
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
boardID = this.getArguments().getLong(BUNDLE_ID);
listPosition = this.getArguments().getInt(BUNDLE_LIST_POSITION, 0);
return inflater.inflate(R.layout.fragment_recyclerview, container, false);
}
所以问题是,当我从 getArguments() 获得 int-Position 时,它有下一个序列 - 1,2,3,4,0。但是它按这个顺序写 - 0,1,2,3,4
可能你认为这无关紧要,但 Realm ORM 确实是相反的。当我从数据库中获取有关动态 CardView 的信息时,Realm 从第 0 个片段开始,而 getArguments() 从 1 开始。那东西抛出
ArrayIndexOutOfBoundsException
[更新] 我添加了 Log.d 到 newInstance 和 OnCreateView
public static RecyclerViewFragment newInstance(long id, int listPosition){
Bundle args = new Bundle();
args.putLong(BUNDLE_ID, id);
Log.d(TAG, listPosition + " Before put");
args.putInt(BUNDLE_LIST_POSITION, listPosition);
Log.d(TAG, listPosition + " After put");
RecyclerViewFragment fragment = new RecyclerViewFragment();
fragment.setArguments(args);
return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
tinyDB = new TinyDB(getContext());
boardPosition = tinyDB.getInt("boardPosition");
boardID = this.getArguments().getLong(BUNDLE_ID);
listPosition = this.getArguments().getInt(BUNDLE_LIST_POSITION, 0);
Log.d(TAG, listPosition + " Get ");
return inflater.inflate(R.layout.fragment_recyclerview, container, false);
}
然后在 LogCat 我得到下一个:
06-12 00:37:46.188 25265-25265/com.treggo.flexible D/mLogs: 0 Before put
06-12 00:37:46.188 25265-25265/com.treggo.flexible D/mLogs: 0 After put
06-12 00:37:46.188 25265-25265/com.treggo.flexible D/mLogs: 1 Before put
06-12 00:37:46.188 25265-25265/com.treggo.flexible D/mLogs: 1 After put
06-12 00:37:46.188 25265-25265/com.treggo.flexible D/mLogs: 2 Before put
06-12 00:37:46.188 25265-25265/com.treggo.flexible D/mLogs: 2 After put
06-12 00:37:46.189 25265-25265/com.treggo.flexible D/mLogs: 3 Before put
06-12 00:37:46.189 25265-25265/com.treggo.flexible D/mLogs: 3 After put
06-12 00:37:46.189 25265-25265/com.treggo.flexible D/mLogs: 4 Before put
06-12 00:37:46.189 25265-25265/com.treggo.flexible D/mLogs: 4 After put
06-12 00:37:46.190 25265-25265/com.treggo.flexible D/mLogs: 1 Get
06-12 00:37:46.266 25265-25265/com.treggo.flexible D/mLogs: 2 Get
06-12 00:37:46.322 25265-25265/com.treggo.flexible D/mLogs: 3 Get
06-12 00:37:46.397 25265-25265/com.treggo.flexible D/mLogs: 4 Get
06-12 00:37:46.452 25265-25265/com.treggo.flexible D/mLogs: 0 Get
P.S。为了绕过异常,我设置了 9 个 CardView,而不是从 Realm
读取它
虽然我不知道你是如何实现列表位置的索引的,但从这个问题来看,我假设它是从 1 开始的,而不是 0。
如果是这种情况,请在您的 newInstance()
方法中替换以下行:
args.putInt(BUNDLE_LIST_POSITION, listPosition);
有了这个:
args.putInt(BUNDLE_LIST_POSITION, listPosition - 1);
请帮我解决一下?
我有一个带有 ViewPager
和 5 个 RecyclerViewFragment
的 Activity
,每个 RecyclerFragment
都包含动态 CardView
。
ViewPager
适配器的代码:
public class CustomFragmentPagerAdapter extends FragmentStatePagerAdapter {
private Board board;
private long boardID;
public CustomFragmentPagerAdapter(FragmentManager fm, Board board, long id) {
super(fm);
this.board = board;
this.boardID = id;
}
@Override
public int getCount() {
return 5;
}
@Override
public CharSequence getPageTitle(int position) {
return board.getMyLists().get(position).getName();
}
//Here I define fragment, int position - is a position of Fragment in ViewPager
@Override
public Fragment getFragment( int position) {
**return RecyclerViewFragment.newInstance(boardID, position);**
}
}
接下来是 Fragment 的新实例:
public static RecyclerViewFragment newInstance(long id, int listPosition){
Bundle args = new Bundle();
args.putLong(BUNDLE_ID, id);
args.putInt(BUNDLE_LIST_POSITION, listPosition);
RecyclerViewFragment fragment = new RecyclerViewFragment();
fragment.setArguments(args);
return fragment;
}
我通过 Bundle 存储 片段的位置 - listPosition
然后我通过getArguments()
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
boardID = this.getArguments().getLong(BUNDLE_ID);
listPosition = this.getArguments().getInt(BUNDLE_LIST_POSITION, 0);
return inflater.inflate(R.layout.fragment_recyclerview, container, false);
}
所以问题是,当我从 getArguments() 获得 int-Position 时,它有下一个序列 - 1,2,3,4,0。但是它按这个顺序写 - 0,1,2,3,4
可能你认为这无关紧要,但 Realm ORM 确实是相反的。当我从数据库中获取有关动态 CardView 的信息时,Realm 从第 0 个片段开始,而 getArguments() 从 1 开始。那东西抛出
ArrayIndexOutOfBoundsException
[更新] 我添加了 Log.d 到 newInstance 和 OnCreateView
public static RecyclerViewFragment newInstance(long id, int listPosition){
Bundle args = new Bundle();
args.putLong(BUNDLE_ID, id);
Log.d(TAG, listPosition + " Before put");
args.putInt(BUNDLE_LIST_POSITION, listPosition);
Log.d(TAG, listPosition + " After put");
RecyclerViewFragment fragment = new RecyclerViewFragment();
fragment.setArguments(args);
return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
tinyDB = new TinyDB(getContext());
boardPosition = tinyDB.getInt("boardPosition");
boardID = this.getArguments().getLong(BUNDLE_ID);
listPosition = this.getArguments().getInt(BUNDLE_LIST_POSITION, 0);
Log.d(TAG, listPosition + " Get ");
return inflater.inflate(R.layout.fragment_recyclerview, container, false);
}
然后在 LogCat 我得到下一个:
06-12 00:37:46.188 25265-25265/com.treggo.flexible D/mLogs: 0 Before put
06-12 00:37:46.188 25265-25265/com.treggo.flexible D/mLogs: 0 After put
06-12 00:37:46.188 25265-25265/com.treggo.flexible D/mLogs: 1 Before put
06-12 00:37:46.188 25265-25265/com.treggo.flexible D/mLogs: 1 After put
06-12 00:37:46.188 25265-25265/com.treggo.flexible D/mLogs: 2 Before put
06-12 00:37:46.188 25265-25265/com.treggo.flexible D/mLogs: 2 After put
06-12 00:37:46.189 25265-25265/com.treggo.flexible D/mLogs: 3 Before put
06-12 00:37:46.189 25265-25265/com.treggo.flexible D/mLogs: 3 After put
06-12 00:37:46.189 25265-25265/com.treggo.flexible D/mLogs: 4 Before put
06-12 00:37:46.189 25265-25265/com.treggo.flexible D/mLogs: 4 After put
06-12 00:37:46.190 25265-25265/com.treggo.flexible D/mLogs: 1 Get
06-12 00:37:46.266 25265-25265/com.treggo.flexible D/mLogs: 2 Get
06-12 00:37:46.322 25265-25265/com.treggo.flexible D/mLogs: 3 Get
06-12 00:37:46.397 25265-25265/com.treggo.flexible D/mLogs: 4 Get
06-12 00:37:46.452 25265-25265/com.treggo.flexible D/mLogs: 0 Get
P.S。为了绕过异常,我设置了 9 个 CardView,而不是从 Realm
读取它虽然我不知道你是如何实现列表位置的索引的,但从这个问题来看,我假设它是从 1 开始的,而不是 0。
如果是这种情况,请在您的 newInstance()
方法中替换以下行:
args.putInt(BUNDLE_LIST_POSITION, listPosition);
有了这个:
args.putInt(BUNDLE_LIST_POSITION, listPosition - 1);