2012-12-01 173 views
0

我有一個自定義列表視圖適配器,其中包含ArrayList作爲成員。自定義列表視圖適配器中的對象數組

每場比賽都屬於一輪。 我的意思是:

public class GameSummary 
{ 
String round; 
Game game; 
} 

因此,實際上,我需要創建一個排序部一覽,該輪將是頭和下面的比賽。

問題是,listview引擎在數組中生成行PER對象。

所以如果我將有3個GameSummary和10個遊戲在它的數組 - 它只會產生3行!

我該怎麼辦?

當前自定義適配器從BaseAdapter類繼承。

+1

隨着baseAdapter,行的數量由getCount將方法來控制。但是你看起來你會從expandableListView中獲益。 – mango

回答

2

你必須像這樣使用expandableListView和customAdapter。

@SuppressLint("ResourceAsColor") 
    public class ExpandableListAdapter extends BaseExpandableListAdapter { 


     private LayoutInflater inflater; 
     private ArrayList<GameSummary> mParent; 

    public ExpandableListAdapter(Context context, ArrayList<GameSummary> parent){ 
      this.mParent = parent; 
      this.inflater = LayoutInflater.from(context); 
     } 


     //counts the number of group/parent items so the list knows how many times calls getGroupView() method 
     public int getGroupCount() { 
      return mParent.size(); 
     } 

     //counts the number of children items so the list knows how many times calls getChildView() method 
     public int getChildrenCount(int i) { 
      return mParent.getGameList(i).size(); 
     } 

     //gets the title of each parent/group 
     public Object getGroup(int i) { 
      return mParent.getGameList(i).getTitle(); //game Title 
     } 

     //gets the name of each item 
     public Object getChild(int i, int i1) { 
      return mParent.getGameList(i); 
     } 

     public long getGroupId(int i) { 
      return i; 
     } 

     public long getChildId(int i, int i1) { 
      return i1; 
     } 

     public boolean hasStableIds() { 
      return true; 
     } 


     //in this method you must set the text to see the parent/group on the list 
     public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) { 

      if (view == null) { 
       view = inflater.inflate(R.layout.expandable_listview, viewGroup,false); 
      } 

      TextView textView = (TextView) view.findViewById(R.id.list_item_text_parent); 
      //"i" is the position of the parent/group in the list 
      textView.setText(getGroup(i).toString()); 


      //return the entire view 
      return view; 
     } 


     //in this method you must set the text to see the children on the list 
     public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) { 
      if (view == null) { 
       view = inflater.inflate(R.layout.expandable_listview_child_item, viewGroup,false); 
      } 

      TextView textView = (TextView) view.findViewById(R.id.list_item_text_child); 
      //"i" is the position of the parent/group in the list and 
      //"i1" is the position of the child 
      textView.setText(mParent.get(i).getGameList(i1)); 

      //return the entire view 
      return view; 
     } 

     public boolean isChildSelectable(int i, int i1) { 
      return true; 
     } 

和類:

public class GameSummary 
{ 
String round; 
List<Game> gameList; 
} 
相關問題