在girdview中添加simpleAdapter,为什么新建SimpleAdapter的时候会报错?
我的代码如下:
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"/>
</LinearLayout>
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>
package com.example.test4;
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,Integer>> 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,Integer> map=new HashMap<String,Integer>();
map.put("图片", R.drawable.a1);
list.add(map);
map.put("图片", R.drawable.a2);
list.add(map);
map.put("图片", R.drawable.a1);
list.add(map);
s=new SimpleAdapter(MainActivity.this,list,R.layout.imageview,new String[]{"图片"},new Integer[]{R.id.image});
}
}
其中,s=new SimpleAdapter.....那行被报错,我截张图:
应该就是说构造方法没这样的重载,明明就应该没错的吧,请问这里是怎么回事?应该怎么解决?
你这样添加有问题,而且你最后那个不能用new Integer[]{},应该用new int[]{}.这样的。
你那个添加有问题。
像下面这样添加,然后把你SimpleAdapter初始化的“图片”改成"image"
Map<String,Integer> maps1 = new HashMap<String,Integer>();
maps1.put("image",R.drawable.a1);
Map<String,Integer> maps2 = new HashMap<String,Integer>();
maps2.put("image",R.drawable.a2);
Map<String,Integer> maps3 = new HashMap<String,Integer>();
maps3.put("image",R.drawable.a3);
list.add(maps1);
list.add(maps2);
list.add(maps3);
List<Map<String,Integer>> list=null;
这行代码声明了List>对象,但你下面没有实例化此List>对象,所以应该在initAdapter()方法中实例化此对象,应在此方法内加入此行代码:
list= new ArrayList<Map<String,Integer>>();
然后才能把Map添加到list对象中,说错请见谅!
将代码;
s = new SimpleAdapter(MainActivity.this, list, R.layout.imageview, new String[]{"图片"}, new Integer[]{R.id.image});
改为:
s = new SimpleAdapter(MainActivity.this, list, R.layout.imageview, new String[]{"图片"}, new int[]{R.id.image});
因为SimpleAdapter的构造方法是:
public SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
你这样写好复杂,直接把new Integer[]{}改为newint[]{};
然后那个Map maps1 = new HashMap ();
maps1.put("image",R.drawable.a1);
maps1.put("image",R.drawable.a2);
maps1.put("image",R.drawable.a3);
list.add(maps1);
Map只要初始化一个就好了