代碼如下:
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);
如上.