freedom_wings
2015-11-18 14:41
采纳率: 21.1%
浏览 1.6k

安卓,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});




}

}

要显示三张图片,可是显示的都是同一张,是最后一张图。

请问这里哪里出了错,怎么解决?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • Q544471255 2015-11-19 01:12
    已采纳
        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);
    
    

    如上.

    打赏 评论
  • devmiao 2015-11-18 15:36
    打赏 评论
  • SamBrother 2015-11-18 15:56

    只new了一个map对象呃,后面的会put()会的覆盖掉前面的值……You see?

    打赏 评论
  • sanxiaochengyu 2015-11-19 00:31

    要重新Map map=new HashMap();
    然后再put,再添加到list

    打赏 评论
  • keyborad_boy 2015-11-19 04:00

    new int[]{R.id.image},这里出错了

    打赏 评论

相关推荐 更多相似问题