我正在尝试制作一种方法,如果卡position == 0
然后viewHolder.mPostSortNum.setText("1");
"如果position == 1
然后viewHolder.mPostSortNum.setText("2");
,但问题是我想自动为所有卡编号,因为这些方法内容位置0,1只是因此我需要一种方法来按位置自动对所有卡进行编号。
这是我的活动方法:
FirebaseRecyclerAdapter<Getting_Posts, PostViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Getting_Posts, PostViewHolder>(
Getting_Posts.class, R.layout.post_card_design, PostViewHolder.class, mDatabase) {
@Override
protected void populateViewHolder(final PostViewHolder viewHolder, final Getting_Posts model, int position) {
viewHolder.setUsername(model.getUsername());
viewHolder.setProfile(getActivity().getApplicationContext(), model.getProfile());
viewHolder.setDescribe(model.getDescribe());
viewHolder.setWallpaper(getActivity().getApplicationContext(), model.getWallpaper());
//heres my methods but its for 3 cards only
if (position==0){viewHolder.mPostSortNum.setText("1");}
if (position==1){viewHolder.mPostSortNum.setText("2");}
if (position==2){viewHolder.mPostSortNum.setText("3");}
}
};
mPostList.setAdapter(firebaseRecyclerAdapter);}
答案 0 :(得分:0)
position
总是比实际数字少一个,因为索引始终从0开始。每次调用position + 1
时都要使用viewHolder.mPostSortNum.setText("");
。
FirebaseRecyclerAdapter<Getting_Posts, PostViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Getting_Posts, PostViewHolder>(
Getting_Posts.class, R.layout.post_card_design, PostViewHolder.class, mDatabase) {
@Override
protected void populateViewHolder(final PostViewHolder viewHolder, final Getting_Posts model, int position) {
viewHolder.setUsername(model.getUsername());
viewHolder.setProfile(getActivity().getApplicationContext(), model.getProfile());
viewHolder.setDescribe(model.getDescribe());
viewHolder.setWallpaper(getActivity().getApplicationContext(), model.getWallpaper());
viewHolder.mPostSortNum.setText(String.valueOf(position + 1));
}
};
mPostList.setAdapter(firebaseRecyclerAdapter);}