2017-07-04 72 views
0

在我原來的Activity中,我初始化一個Adapter來填充一個ListView,並且順利進行。所有數據填充的ArrayList都會作爲參數傳遞給Adapter,並且一切順利。ListView裏面的ListView只顯示一個條目

現在在該適配器中,或者那個被填充的ListView,還有另一個ListView和另一個適配器。

同樣,我遵循相同的程序,並用ArrayList填充適配器,但似乎只有第一個條目顯示在嵌套ListView中。我認爲這是因爲家長ListView得到填充,並且兒童ListView的數據僅填充一個父母條目。

總之,這裏的適配器初始化和適配器類:

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

alPlayerSeasonsStats是包含的數據填充到孩子ListView一個ArrayList。

這裏的孩子適配器:

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

什麼是我選擇這裏?

+0

我會開始沒有一個'ListView'嵌套在另一個'ListView'中。使用一個具有多個行類型的'ListView',或者使用'ExpandableListView',或者使用具有某種擴展語義的'RecyclerView',或者使用具有多個項目類型的'RecyclerView',或者使用'vertical'' LinearLayout '而不是嵌套的'ListView',或者使用沿着另一個軸工作的東西(例如'Horizo​​ntalScrollView'),或者使用兩個活動/片段(頂層ListView'向下鑽取到一組單獨的細節中)ListBox等) – CommonsWare

+0

我聽說嵌套的ListView會導致用戶界面問題,但我的設計儘可能簡單,它不會成爲問題。話雖如此,我有什麼可以在選擇全面開始之前修復現有的邏輯? – user1938007

+0

可以看看這個:https://stackoverflow.com/questions/31458803/listview-inside-listview-only-one-element – rafsanahmad007

回答

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

+0

你做了哪些改變?也許增加一些解釋它將如何解決問題將有助於操作.. – rafsanahmad007