public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ExpandableListAdapter adapter = new BaseExpandableListAdapter()
{
int[] logos = new int[]
{
R.drawable.ayomi1,
R.drawable.ayomi2,
R.drawable.ayomi3,
R.drawable.ic_launcher
};
private String[] ayomiTypes = new String[]
{"A","B","C","D"};
private String[][] ayomis = new String[][]
{
{"1","2","3","4","4+"},
{"5","6","7","8","8+"},
{"9","10","11","1112"},
{"12"}
};
@Override
public Object getChild(int groupPosition, int childPosition)
{
return ayomis[groupPosition][childPosition];
}
@Override
public long getChildId( int groupPosition, int childPosition)
{
return childPosition;
}
@Override
public int getChildrenCount(int groupPosition)
{
return ayomis.length;
}
private TextView getTextView()
{
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 64);
TextView textView = new TextView(MainActivity.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView.setPadding(36, 0, 0, 0);
textView.setTextSize(20);
return textView;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent)
{
TextView textView = getTextView();
textView.setText(getChild(groupPosition, childPosition)
.toString());
return textView;
}
@Override
public Object getGroup(int groupPosition)
{
return ayomiTypes[groupPosition];
}
@Override
public int getGroupCount()
{
return ayomiTypes.length;
}
@Override
public long getGroupId(int groupPosition)
{
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
LinearLayout ll = new LinearLayout(MainActivity.this);
ll.setOrientation(0);
ImageView logo = new ImageView(MainActivity.this);
logo.setImageResource(logos[groupPosition]);
ll.addView(logo);
TextView textView = getTextView();
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView);
return ll;
}
@Override
public boolean isChildSelectable(int groupPosition,
int childPosition)
{
return true;
}
@Override
public boolean hasStableIds()
{
return true;
}
};
ExpandableListView expandListView = (ExpandableListView) findViewById
(R.id.List1);
expandListView.setAdapter(adapter);
}
}
以上代码会显示四张图片,点开前三张都会有四个可选下拉项目,第四张打开后看到唯一一个项目后意外终止。
问题是:
- 为什么代码中有五个项目却只能显示四个?
- 为什么最后一个会意外退出?
- 为什么重写方法后依然能自动填充listview的内容,隐式的过程是什么呢?(代码中并没有循环录入的语句)
thank you for help and advice.