问题描述
- 安卓,gridview和simpleadapter,为什么显示的都是同一张图?
-
代码如下:activity_main.xml:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical"><GridView android:id="@+id/gridview" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
然后下面是imageview.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".activity" android:orientation="vertical"> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> 下面是java代码:
package com.example.test5;
import android.os.Bundle;
import android.widget.*;
import java.util.*;
import android.app.Activity;
import android.view.Menu;public class MainActivity extends Activity {
SimpleAdapter s=null; GridView grid=null; List<Map<String,Object>> list=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); grid=(GridView)super.findViewById(R.id.gridview); initAdapter(); grid.setAdapter(s); } @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; } public void initAdapter() { Map<String,Object> map=new HashMap<String,Object>(); list=new ArrayList<Map<String,Object>>(); map.put("图片", R.drawable.a1); list.add(map); map.put("图片", R.drawable.a2); list.add(map); map.put("图片", R.drawable.a3); list.add(map); s=new SimpleAdapter(MainActivity.this,list,R.layout.imageview,new String[]{"图片"},new int[]{R.id.image}); }
}
要显示三张图片,可是显示的都是同一张,是最后一张图。
请问这里哪里出了错,怎么解决?
解决方案
Map<String,Object> map=new HashMap<String,Object>();
list=new ArrayList<Map<String,Object>>();
map.put("图片", R.drawable.a1);
list.add(map);
Map<String,Object> map1=new HashMap<String,Object>();
map1.put("图片", R.drawable.a2);
list.add(map1);
Map<String,Object> map2=new HashMap<String,Object>();
map2.put("图片", R.drawable.a3);
list.add(map2);
如上.
解决方案二:
http://www.bubuko.com/infodetail-262313.html
解决方案三:
只new了一个map对象呃,后面的会put()会的覆盖掉前面的值……You see?
解决方案四:
要重新Map map=new HashMap();
然后再put,再添加到list
解决方案五:
new int[]{R.id.image},这里出错了