在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);