【发布时间】:2017-07-04 18:54:35
【问题描述】:
在我原来的 Activity 中,我初始化了一个 Adapter 来填充一个 ListView,这很顺利。 ArrayList 填充了所有数据作为参数传递给适配器,一切顺利。
现在,在那个适配器或填充的 ListView 内部,还有另一个 ListView 和另一个适配器。
再次,我遵循相同的过程并使用ArrayList 填充适配器,但似乎只有第一个条目显示在嵌套的ListView 中。我认为这是因为父 ListView 被填充,而子 ListView 的数据仅填充一个父条目。
不管怎样,这里是适配器初始化和适配器类:
PlayerSeasonsStatsAdapter pssAdapter = new PlayerSeasonsStatsAdapter(getContext(), alPlayerSeasonsStats);
vh.lvPlayerSeasonsStats.setAdapter(pssAdapter);
alPlayerSeasonsStats 是一个 ArrayList,其中包含用于填充子 ListView 的数据。
这是子适配器:
public class PlayerSeasonsStatsAdapter extends ArrayAdapter{
private ArrayList<PlayerStatsTeamModelSeasons> playerSeasons = new ArrayList<PlayerStatsTeamModelSeasons>();
private LayoutInflater mInflater;
public PlayerSeasonsStatsAdapter(Context context, ArrayList<PlayerStatsTeamModelSeasons> playerSeasons) {
super(context, 0, playerSeasons);
this.playerSeasons = playerSeasons;
mInflater = LayoutInflater.from(context);
}
@Override
public PlayerStatsTeamModelSeasons getItem(int position) {
return playerSeasons.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vh;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.player_seasons_stats_row, parent, false);
vh = new ViewHolder();
vh.tvPSSRCompetition = (TextView) convertView.findViewById(R.id.tvPSSRCompetition);
vh.tvPSSRMatchesPlayed = (TextView) convertView.findViewById(R.id.tvPSSRMatchesPlayed);
vh.tvPSSRGoalsScored = (TextView) convertView.findViewById(R.id.tvPSSRGoalsScored);
vh.tvPSSRAssists = (TextView) convertView.findViewById(R.id.tvPSSRAssists);
vh.tvPSSRYellowCards = (TextView) convertView.findViewById(R.id.tvPSSRYellowCards);
vh.tvPSSRRedCards = (TextView) convertView.findViewById(R.id.tvPSSRRedCards);
vh.ivPSSRCompetitionLogo = (ImageView) convertView.findViewById(R.id.ivPSSRCompetitionLogo);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
PlayerStatsTeamModelSeasons pstmsModel = getItem(position);
vh.tvPSSRCompetition.setText(pstmsModel.pstmSeasonModel.name);
vh.tvPSSRMatchesPlayed.setText(pstmsModel.pstmsModel.matchesPlayed);
vh.tvPSSRGoalsScored.setText(pstmsModel.pstmsModel.goalsScored);
vh.tvPSSRAssists.setText(pstmsModel.pstmsModel.assists);
vh.tvPSSRYellowCards.setText(pstmsModel.pstmsModel.yellowCards);
int totalRedCards = Integer.parseInt(pstmsModel.pstmsModel.yellowRedCards) + Integer.parseInt(pstmsModel.pstmsModel.redCards);
vh.tvPSSRRedCards.setText(totalRedCards + "");
//loadLogoToView(vh.ivPSSRCompetitionLogo, pstmsModel.pstmSeasonModel.id, true);
return convertView;
}
public static void loadLogoToView(ImageView iv, String teamId, boolean transform) {
String image = Constant.IMAGES_ROOT +
Constant.LOGO_PATH +
teamId +
Constant.LOGO_EXTENSION;
RequestCreator img = Picasso.with(iv.getContext())
.load(image);
if (transform) img.transform(new RoundedCornersTransform(2));
img.into(iv);
}
static class ViewHolder {
ImageView ivPSSRCompetitionLogo;
TextView tvPSSRCompetition;
TextView tvPSSRMatchesPlayed;
TextView tvPSSRGoalsScored;
TextView tvPSSRAssists;
TextView tvPSSRYellowCards;
TextView tvPSSRRedCards;
}
我有什么选择?
【问题讨论】:
-
我会先不让
ListView嵌套在另一个ListView中。将单个ListView与多个行类型一起使用,或使用ExpandableListView,或将RecyclerView与某种扩展语义一起使用,或将RecyclerView与多个项目类型一起使用,或使用vertical@987654341 @ 而不是嵌套的ListView,或者使用沿另一个轴工作的东西(例如,HorizontalScrollView),或者使用两个活动/片段(顶级ListView向下钻取到一组单独的细节与以前-嵌套ListView)等 -
我听说嵌套的 ListViews 会导致 UI 问题,但我的设计如此简单,这不会成为问题。话虽如此,在选择重新开始之前,我能做些什么来修复现有的逻辑吗?
-
但问题不在于它显示的
ListView仅包含一项空间,问题在于用于通过适配器填充ListView的ArrayList仅包含一个条目。 -
那么您的问题在于您如何填充
alPlayerSeasonsStats,如果它的size()为1。
标签: java android listview android-arrayadapter