代码如下:
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});
}
}
要显示三张图片,可是显示的都是同一张,是最后一张图。
请问这里哪里出了错,怎么解决?