我使用多级ExpandableListView
,如何达到3级Listview
请给出一些参考。
我添加了2个级别,但无法在第2级ExpandableListView
子元素中添加第3级子元素。
public class MainActivity extends Activity implements OnClickListener
{
private LinkedHashMap<String, Header> myDepartments = new LinkedHashMap<String, Header>();
private ArrayList<Header> deptList = new ArrayList<Header>();
private ListAdapter listAdapter;
private ExpandableListView myList;
List<String> li;
EditText et;
Spinner spinner;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.department);
li=new ArrayList<String>();
Button b=(Button) findViewById(R.id.add1);
et=(EditText)findViewById(R.id.product1);
b.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// TODO Auto-generated method stub
li.add(et.getText().toString());
et.setText(null);
add();
}
});
add();
myList = (ExpandableListView) findViewById(R.id.myList);
listAdapter = new ListAdapter(MainActivity.this, deptList);
myList.setAdapter(listAdapter);
expandAll();
Button add = (Button) findViewById(R.id.add);
add.setOnClickListener(this);
myList.setOnChildClickListener(myListItemClicked);
myList.setOnGroupClickListener(myListGroupClicked);
}
private void add()
{
ArrayAdapter<String> adp=new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line,li);
spinner.setAdapter(adp);
}
public void onClick(View v)
{
switch (v.getId())
{
//add entry to the List
case R.id.add:
Spinner spinner = (Spinner) findViewById(R.id.department);
String department = spinner.getSelectedItem().toString();
EditText editText = (EditText) findViewById(R.id.product);
String product = editText.getText().toString();
editText.setText("");
int groupPosition = addProduct(department,product);
listAdapter.notifyDataSetChanged();
collapseAll();
myList.expandGroup(groupPosition);
myList.setSelectedGroup(groupPosition);
break;
}
}
//method to expand all groups
private void expandAll() {
int count = listAdapter.getGroupCount();
for (int i = 0; i < count; i++){
myList.expandGroup(i);
}
}
//method to collapse all groups
private void collapseAll() {
int count = listAdapter.getGroupCount();
for (int i = 0; i < count; i++){
myList.collapseGroup(i);
}
}
//our child listener
private OnChildClickListener myListItemClicked = new OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
//get the group header
Header headerInfo = deptList.get(groupPosition);
//get the child info
Detail detailInfo = headerInfo.getProductList().get(childPosition);
//display it or do something with it
Toast.makeText(getBaseContext(), "Clicked on Detail " + headerInfo.getName()
+ "/" + detailInfo.getName(), Toast.LENGTH_LONG).show();
return false;
}
};
//our group listener
private OnGroupClickListener myListGroupClicked = new OnGroupClickListener()
{
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
//get the group header
Header headerInfo = deptList.get(groupPosition);
//display it or do something with it
Toast.makeText(getBaseContext(), "Child on Header " + headerInfo.getName(),
Toast.LENGTH_LONG).show();
return false;
}
};
private int addProduct(String department, String product){
int groupPosition = 0;
//check the hash map if the group already exists
Header headerInfo = myDepartments.get(department);
//add the group if doesn't exists
if(headerInfo == null){
headerInfo = new Header();
headerInfo.setName(department);
myDepartments.put(department, headerInfo);
deptList.add(headerInfo);
}
//get the children for the group
ArrayList<Detail> productList = headerInfo.getProductList();
//size of the children list
int listSize = productList.size();
//add to the counter
listSize++;
//create a new child and add that to the group
Detail detailInfo = new Detail();
detailInfo.setSequence(String.valueOf(listSize));
detailInfo.setName(product);
productList.add(detailInfo);
headerInfo.setProductList(productList);
//find the group position inside the list
groupPosition = deptList.indexOf(headerInfo);
return groupPosition;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}