Android 网格视图(GirdView)简易适配器的使用

activity_main.xml文件




    
    

a2.xml



    
        
        
    

MainActivity.java

package com.example.first.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    GridView gridView;
    SimpleAdapter simpleAdapter;
    /**图片地址**/
    private int[] icon = {R.mipmap.a,R.mipmap.b,R.mipmap.c,R.mipmap.d,R.mipmap.e,R.mipmap.f,R.mipmap.g};
    private String[] name = {"1","2","1","1","1","1"};

    private List> mapList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mapList = new ArrayList>();
        gridView = this.findViewById(R.id.grid);

        /**第一个参数为上下文 第二个参数为数据源 第三个参数为布局文件**/
        /**第四个参数为键值 第五个参数为布局文件中控件的id**/
        simpleAdapter = new SimpleAdapter(MainActivity.this,getlist(),R.layout.a2,new String[]{"imgview","textview"},
                new int[]{R.id.img,R.id.txt});
        /**加载适配器**/
        gridView.setAdapter(simpleAdapter);
    }

    /**添加数据源**/
    private List> getlist(){
        for(int i = 0;imap = new HashMap();
            map.put("imgview",icon[i]);
            map.put("textview",name[i]);
            mapList.add(map);
        }
        return mapList;
    }
}

 

你可能感兴趣的:(Android,Android)