【Android】GridView可以操作但不能显示图片
publicclassMainActivityextendsActivity{int[]imageIds=newint[]{R.drawable.one,R.drawab...
public class MainActivity extends Activity {
int [] imageIds = new int[]
{
R.drawable.one,R.drawable.two,R.drawable.three,
R.drawable.four,R.drawable.five,R.drawable.six
};
ImageSwitcher switcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Map<String,Object>> listItems = new ArrayList<Map<String,Object>>();
for (int i = 0;i<imageIds.length;i++)
{
Map<String,Object> items = new HashMap<String,Object>();
items.put("iamge", imageIds[i]);
listItems.add(items);
}
switcher = (ImageSwitcher)findViewById(R.id.switcher);
switcher.setFactory(new ViewFactory()
{
@Override
public View makeView() {
// TODO Auto-generated method stub
ImageView imageview = new ImageView(MainActivity.this);
imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageview.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
return imageview;
}
});
SimpleAdapter simpleadapter = new SimpleAdapter(this, listItems, R.layout.cell, new String[]{"image"},new int[]{R.id.image1} );
GridView grid01 = (GridView)findViewById(R.id.grid01);
grid01.setAdapter(simpleadapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
} 展开
int [] imageIds = new int[]
{
R.drawable.one,R.drawable.two,R.drawable.three,
R.drawable.four,R.drawable.five,R.drawable.six
};
ImageSwitcher switcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Map<String,Object>> listItems = new ArrayList<Map<String,Object>>();
for (int i = 0;i<imageIds.length;i++)
{
Map<String,Object> items = new HashMap<String,Object>();
items.put("iamge", imageIds[i]);
listItems.add(items);
}
switcher = (ImageSwitcher)findViewById(R.id.switcher);
switcher.setFactory(new ViewFactory()
{
@Override
public View makeView() {
// TODO Auto-generated method stub
ImageView imageview = new ImageView(MainActivity.this);
imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageview.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
return imageview;
}
});
SimpleAdapter simpleadapter = new SimpleAdapter(this, listItems, R.layout.cell, new String[]{"image"},new int[]{R.id.image1} );
GridView grid01 = (GridView)findViewById(R.id.grid01);
grid01.setAdapter(simpleadapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
} 展开
1个回答
展开全部
拼错了~
items.put("iamge", imageIds[i]);
改为
items.put("image", imageIds[i]);
items.put("iamge", imageIds[i]);
改为
items.put("image", imageIds[i]);
追问
谢谢 但是为什么SimpleAdapter simpleadapter = new SimpleAdapter(this, listItems, R.layout.cell, new String[]{"image"},new int[]{R.id.image1} );这句代码中的new String[]{"image"},不会报错呢?
追答
这边new出的字段会在listitems中查询,没有查询到就是空的数据,不会报错。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询