2011-06-07 57 views
2

我找不到在任何地方向我的可展开列表添加子项的说明。我能找到的50个关于如何改变背景颜色的问题。将“子”或子项添加到ExpandableListView

现在,我使用这个代码创建一个正常的列表视图:

public String listArray[] = { "Example1", "Example2", "Example3", "Example4", "Example5"}; 
public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main2); 
     listV = (ListView) findViewById(R.id.exListView); 
     listV.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listArray));  
    }  

但我将如何去添加子或子项下面的例1,例2,等..?

回答

2

对于初学者,您需要使用ExpandableListView而不是ListView。然后抛出一个SimpleExpandableListAdapter并保持对组(父)和子数据对象的引用。

然后,它的那么容易,因为:

mChildData.get(groupIndex).get(childIndex).put(...); 
mListAdapter.notifyDataSetChanged(); 
+0

你能给我一个SimpleExpandableListAdapter的例子吗?我很新的Android,并不完全理解所有的参数。安卓网站说这是我需要正确的? public SimpleExpandableListAdapter(Context context,List <?extends Map > groupData,int groupLayout,String [] groupFrom,int [] groupTo,List <?extends List <?extends Map >> childData,int childLayout,String [] childFrom, int [] childTo)编辑:抱歉无法获取代码标签的工作:/ – 2011-06-07 16:35:27

+0

http://mylifewithandroid.blogspot.com/2008/05/expandable-lists.html – 2011-06-07 19:10:15

+0

非常感谢。 :) – 2011-06-07 19:45:44

0

为expandablelistview基本的例子是.. here ....

在活动实施OnChildClickListener和implemet此方法..

@Override 
public boolean onChildClick(ExpandableListView parent, View v, 
int groupPosition, int childPosition, long id) { 

    //Add new chid data in your list (ex ArrayLis or Array whatever you use) 
    adapter.notifyDataSetChanged() // BaseExpandableListAdapter's adapter... 


return true; 
} 

它是为你工作吗?