0

Inside my original Activity, I initialize an Adapter to populate a ListView and that goes smoothly. ArrayList populated with all the data gets passed as an argument to the Adapter and everything goes well.

Now inside that Adapter, or that ListView that gets populated, there's another ListView and another Adapter for it.

Again, I follow the same procedure and populate the Adapter with an ArrayList, but it seems as though only the first entry gets displayed in the nested ListView. I assume this is is because the parent ListView gets populated and the data for the child ListView only populates for one parents entry.

Anyway, here's the adapter initialization and the Adapter class:

    PlayerSeasonsStatsAdapter pssAdapter = new PlayerSeasonsStatsAdapter(getContext(), alPlayerSeasonsStats);
            vh.lvPlayerSeasonsStats.setAdapter(pssAdapter);

alPlayerSeasonsStats is an ArrayList containing data to populate the child ListView.

Here's the child Adapter:

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

What are my options here?

user1938007
  • 459
  • 1
  • 5
  • 14
  • I would start by not having a `ListView` nested in another `ListView`. Use a single `ListView` with multiple row types, or use an `ExpandableListView`, or use a `RecyclerView` with some sort of expansion semantics, or use a `RecyclerView` with multiple item types, or use a `vertical` `LinearLayout` instead of the nested `ListView`, or use something that works along the other axis (e.g., `HorizontalScrollView`), or use two activities/fragments (top-level `ListView` drilling down into a separate set of details with the formerly-nested `ListView`), etc. – CommonsWare Jul 04 '17 at 18:59
  • I heard that nested ListViews cause UI problems, but with my design as simple as it is, it won't be an issue. That being said, is there anything I can do to fix the existing logic before choosing to start all over? – user1938007 Jul 04 '17 at 19:01
  • can look at this: https://stackoverflow.com/questions/31458803/listview-inside-listview-only-one-element – rafsanahmad007 Jul 04 '17 at 19:04
  • But the problem isn't that it displays a `ListView` with space for only 1 item, the problem is that the `ArrayList` used to populate the `ListView` via Adapter only contains one entry. – user1938007 Jul 04 '17 at 19:06
  • Then your problem lies with how you are populating `alPlayerSeasonsStats`, if it has `size()` of 1. – CommonsWare Jul 04 '17 at 19:08
  • then check your `arraylist.add()`...code – rafsanahmad007 Jul 04 '17 at 19:09
  • Actually, I just ran the debugger and it turns out I was wrong... The `alPlayerSeasonsStats` indeed contains all the items, except that only one is displayed. Sorry, wrong assumption. – user1938007 Jul 04 '17 at 19:09

1 Answers1

0

Your Adapter should be look like

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;

   }

  }

Just Call below like

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

Listview Look like bellow

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

here is row_account_balance layout

<?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>

Enamul Haque
  • 4,789
  • 1
  • 37
  • 50