如何在Android中膨胀可扩展列表?
如果我使用如何在Android中膨胀可扩展列表?,android,list,android-activity,expandable,Android,List,Android Activity,Expandable,如果我使用albumCovers.get(i)而不是getResources().getDrawable(R.drawable.icon)的话,我想在每一行上用一些不同的文本和不同的图像来膨胀一个可扩展列表,然后它抛出一个错误,有人能帮我们吗?谢谢p public class ExpandActivity extends ExpandableListActivity { /** Called when the activity is first created. */ @Override pub
albumCovers.get(i)
而不是getResources().getDrawable(R.drawable.icon)
的话,我想在每一行上用一些不同的文本和不同的图像来膨胀一个可扩展列表,然后它抛出一个错误,有人能帮我们吗?谢谢p
public class ExpandActivity extends ExpandableListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Construct Expandable List
final String NAME = "name";
final String IMAGE = "image";
final LayoutInflater layoutInflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ArrayList<HashMap<String, String>> headerData = new ArrayList<HashMap<String, String>>();
final HashMap<String, String> group1 = new HashMap<String, String>();
group1.put(NAME, "Group 1");
headerData.add(group1);
final ArrayList<ArrayList<HashMap<String, Object>>> childData = new ArrayList<ArrayList<HashMap<String, Object>>>();
final ArrayList<HashMap<String, Object>> group1data = new ArrayList<HashMap<String, Object>>();
childData.add(group1data);
Resources res = this.getResources();
Drawable photo = (Drawable) res.getDrawable(R.drawable.icon);
ArrayList<Drawable> albumCovers = new ArrayList<Drawable>();
albumCovers.add(photo);
// Set up some sample data in both groups
for (int i = 0; i < 10; ++i) {
final HashMap<String, Object> map = new HashMap<String, Object>();
map.put(NAME, "Child " + i);
map.put(IMAGE, getResources().getDrawable(R.drawable.icon));
(group1data).add(map);
}
setListAdapter(new SimpleExpandableListAdapter(this, headerData,
android.R.layout.simple_expandable_list_item_1,
new String[] { NAME }, // the name of the field data
new int[] { android.R.id.text1 }, // the text field to populate
// with the field data
childData, 0, null, new int[] {}) {
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final View v = super.getChildView(groupPosition, childPosition,
isLastChild, convertView, parent);
// Populate your custom view here
((TextView) v.findViewById(R.id.name))
.setText((String) ((Map<String, Object>) getChild(
groupPosition, childPosition)).get(NAME));
((ImageView) v.findViewById(R.id.image))
.setImageDrawable((Drawable) ((Map<String, Object>) getChild(
groupPosition, childPosition)).get(IMAGE));
return v;
}
@Override
public View newChildView(boolean isLastChild, ViewGroup parent) {
return layoutInflater.inflate(
R.layout.expandable_list_item_with_image, null, false);
}
});
}
}
公共类ExpandActivity扩展了ExpandableListActivity{
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//构造可扩展列表
最终字符串NAME=“NAME”;
最终字符串IMAGE=“IMAGE”;
最终LayoutFlater LayoutFlater=(LayoutFlater)此
.getSystemService(上下文布局\充气机\服务);
最终ArrayList headerData=新ArrayList();
final HashMap group1=新HashMap();
集团1.出售(名称为“集团1”);
headerData.add(第1组);
final ArrayList childData=新ArrayList();
最终ArrayList group1data=新ArrayList();
添加(group1data);
Resources res=this.getResources();
可绘制照片=(可绘制)res.getDrawable(R.Drawable.icon);
ArrayList albumCovers=新的ArrayList();
相册封面。添加(照片);
//在两个组中设置一些示例数据
对于(int i=0;i<10;++i){
final HashMap map=新HashMap();
地图放置(名称,“儿童”+i);
put(IMAGE,getResources().getDrawable(R.drawable.icon));
(组1数据)。添加(地图);
}
setListAdapter(新的SimpleExpandableListAdapter(此,headerData,
android.R.layout.simple\u可扩展\u列表\u项目\u 1,
新字符串[]{NAME},//字段数据的名称
new int[]{android.R.id.text1},//要填充的文本字段
//使用现场数据
childData,0,null,新int[]{}){
@凌驾
公共视图getChildView(int-groupPosition、int-childPosition、,
布尔值isLastChild、视图转换视图、视图组父级){
最终视图v=super.getChildView(groupPosition,childPosition,
isLastChild、convertView、父级);
//在此处填充自定义视图
((文本视图)v.findviewbyd(R.id.name))
.setText((字符串)((映射)getChild(
groupPosition,childPosition)).get(NAME));
((图像视图)v.findViewById(R.id.image))
.setImageDrawable((Drawable)((Map)getChild(
groupPosition,childPosition)).get(IMAGE));
返回v;
}
@凌驾
公共视图newChildView(布尔isLastChild、视图组父视图){
充气机(
R.layout.expandable_list_item_与_image,null,false);
}
});
}
}
您在相册封面中只有一个可绘制,但在for循环中循环十次,并尝试通过索引访问它。将albumCovers.get(i)
更改为albumCovers.get(0)
,使其工作
以下是关键位置:
Drawable photo=(Drawable)res.getDrawable(R.Drawable.icon);
ArrayList albumCovers=新的ArrayList();
相册封面。添加(照片);//这里:照片只添加一次!
对于(int i=0;i<10;++i){
final HashMap map=新HashMap();
地图放置(名称,“儿童”+i);
map.put(IMAGE,getResources().getDrawable(R.drawable.icon));//此处:您想使用albumCovers.get(i)此处。albumCovers中只有1张照片,但您尝试使用索引i从中获取图像0…9。因此,将此更改为albumCovers.get(0)或将10张照片添加到albumCovers中。
(组1数据)。添加(地图);
}
这是一个非常好的观点,哈哈,你帮我省了很多关于谢谢的麻烦;P