ListItem中的ListView只显示一个条目

时间:2017-07-04 18:54:35

标签: java android listview android-arrayadapter

在我原来的Activity中,我初始化一个适配器以填充ListView并且顺利进行。填充了所有数据的ArrayList作为参数传递给适配器,一切顺利。

现在在该适配器或已填充的ListView内,还有另一个ListView和另一个适配器。

同样,我遵循相同的过程并使用ArrayList填充适配器,但似乎只有第一个条目显示在嵌套的ListView中。我假设这是因为父ListView被填充,而子ListView的数据只填充一个父条目。

无论如何,这里是适配器初始化和Adapter类:

    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;
    }

我有什么选择?

1 个答案:

答案 0 :(得分:0)

您的适配器应该看起来像

public class PlayerSeasonsStatsAdapter extends ArrayAdapter<PlayerStatsTeamModelSeasons> {

private  class ViewHolder {
    TextView tvPSSRCompetition;
}

public PlayerSeasonsStatsAdapter(Context context, List<PlayerStatsTeamModelSeasons> balanceModels) {

    super(context, R.layout.row_account_balance, balanceModels);

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

    PlayerStatsTeamModelSeasons model = getItem(position);

    PlayerSeasonsStatsAdapter.ViewHolder viewHolder;
    if (convertView == null) {

        viewHolder = new PlayerSeasonsStatsAdapter.ViewHolder();

        LayoutInflater inflater = LayoutInflater.from(getContext());

        convertView = inflater.inflate(R.layout.row_account_balance, parent, false);

        viewHolder.tvPSSRCompetition = (TextView) convertView.findViewById(R.id.tvPSSRCompetition);
        convertView.setTag(viewHolder);

    } else {

        viewHolder = (PlayerSeasonsStatsAdapter.ViewHolder) convertView.getTag();
    }
    viewHolder.tvPSSRCompetition.setText(model.getPSSRCompetition());


    if (position%2== 0) {
        convertView.setBackgroundColor(Color.parseColor("#F5EEE5"));
    }else{

        convertView.setBackgroundColor(Color.parseColor("#FFFFFF"));
    }

    return convertView;

   }

  }

只需拨打下面的电话

即可
   PlayerSeasonsStatsAdapter adapter = new PlayerSeasonsStatsAdapter(MainActivity.this, balanceList);
   listView.setAdapter(adapter);

Listview看起来像波纹管

        <ListView
         android:id="@+id/listView"
          android:layout_width="match_parent"
           android:layout_height="match_parent" />
        </LinearLayout>

这里是row_account_balance布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"  >

    <TextView
        android:id="@+id/tvPSSRCompetition"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center|center_vertical|center_horizontal"
        android:text="@string/agent_statement_sl"
        android:textStyle="bold"
        android:paddingLeft="2dp"
        android:textSize="16sp"
        android:paddingBottom="5dp"
        android:paddingTop="5dp"/>   
</LinearLayout>