2013-05-19 70 views
1

我想以編程方式創建一個ExpandableListView,但到目前爲止,它保留空間,但空間是全黑!沒有顯示。 我在這裏有一些特殊要求:我如何以編程方式創建一個ExpandableListView?

  1. 我想在代碼上做所有事情,沒有XML。到目前爲止,我的整個UI具有零XML,我想保持這種方式
  2. 我想如果可能的話,從活動中獲得我的活動。我覺得傻更改類只是因爲ExpandableListView會喜歡的。如果其他控件還想要奇怪的活動特殊類呢?希望它可以有一個活動的ExpandableListView,對不對?

因此,這裏是我的代碼迄今適配器:

// ------------------------------------------- 
// DoubleStringArrayExpandableListAdapter 
// ------------------------------------------- 
public class DoubleStringArrayExpandableListAdapter extends BaseExpandableListAdapter 
{ 
public ArrayList<String> Parents = new ArrayList<String>(); 
public ArrayList<String> Children = new ArrayList<String>(); 
public Activity mContext; 

public Object getChild(int groupPosition, int childPosition) 
{ 
    return Children.get(groupPosition); 
} 

public long getChildId(int groupPosition, int childPosition) 
{ 
    return childPosition; 
} 

public int getChildrenCount(int groupPosition) 
{ 
    return 1; 
} 

public TextView getGenericView() 
{ 
    AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
    LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 

    TextView textView = new TextView(mContext); 
    textView.setLayoutParams(lp); 
    // Center the text vertically 
    textView.setGravity(Gravity.CENTER); 
    // Set the text starting position 
    textView.setPadding(55, 0, 0, 0); 
    return textView; 
} 

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, 
     View convertView, ViewGroup parent) 
{ 
    TextView textView = getGenericView(); 
    textView.setText(getChild(groupPosition, childPosition).toString()); 
    return textView; 
} 

public Object getGroup(int groupPosition) 
{ 
    return Parents.get(groupPosition); 
} 

public int getGroupCount() 
{ 
    return Parents.size(); 
} 

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

public View getGroupView(int groupPosition, boolean isExpanded, View convertView, 
     ViewGroup parent) 
{ 
    TextView textView = getGenericView(); 
    textView.setText(getGroup(groupPosition).toString()); 
    return textView; 
} 

public boolean isChildSelectable(int groupPosition, int childPosition) 
{ 
    return true; 
} 

public boolean hasStableIds() 
{ 
    return true; 
} 
} 

這裏是我創建ExpandableListView代碼:

listNews = new ExpandableListView(this); 
adapterNews = new DoubleStringArrayExpandableListAdapter(); 
adapterNews.mContext = this; 
listNews.setAdapter(adapterNews); 
listNews.setVerticalScrollBarEnabled(true); 
listNews.setScrollbarFadingEnabled(false); 
//listNews.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
//listNews.setFocusableInTouchMode(true); 
listNews.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 200/*doesnt workLayoutParams.WRAP_CONTENT*/)); 
lLayout.addView(listNews); 
adapterNews.Parents.add("Parent"); 
adapterNews.Children.add("Child"); 

任何想法,爲什麼它不工作,這裏有什麼錯?非常感謝=)

回答

1

你需要調用notifyDataSetChanged()的適配器,因爲你添加的數據中的代碼最後兩行或者您可以在代碼的最後一行後調用setAdapter。

相關問題