zoukankan      html  css  js  c++  java
  • GridView使用SimpleAdapter

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
        <GridView
            android:id="@+id/gridview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:columnWidth="80dp"
            android:stretchMode="spacingWidthUniform"
            android:numColumns="3"
            />

    主布局

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content"
        android:orientation="vertical"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/img"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_marginTop="10dp"
            android:src="@drawable/ic_launcher_background" />
        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dp"
            android:layout_gravity="center"
            android:text="文字"
            />
    
    </LinearLayout>

    项目布局gridview_item

    public class MainActivity extends AppCompatActivity {
        private GridView gridView;
        private List<Map<String, Object>> dataList;
        private SimpleAdapter adapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            gridView = (GridView) findViewById(R.id.gridview);
            //初始化数据
            initData();
            String[] from={"img","text"};
    
            int[] to={R.id.img,R.id.text};
    
            adapter=new SimpleAdapter(this, dataList, R.layout.gridview_item, from, to);
    
            gridView.setAdapter(adapter);
    
            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                        long arg3) {
                    AlertDialog.Builder builder= new AlertDialog.Builder(MainActivity.this);
                    builder.setTitle("提示").setMessage(dataList.get(arg2).get("text").toString()).create().show();
                }
            });
        }
    
        void initData() {
            //图标
            int icno[] = { R.drawable.ic5,R.drawable.ic5,R.drawable.ic5,R.drawable.ic5,R.drawable.ic5,R.drawable.ic5,R.drawable.ic5,R.drawable.ic5,R.drawable.ic5,
                    R.drawable.ic5,R.drawable.ic5,R.drawable.ic5};
            //图标下的文字
            String name[]={"时钟","信号","宝箱","秒钟","大象","FF","记事本","书签","印象","商店","主题","迅雷"};
            dataList = new ArrayList<Map<String, Object>>();
            for (int i = 0; i <icno.length; i++) {
                Map<String, Object> map=new HashMap<String, Object>();
                map.put("img", icno[i]);
                map.put("text",name[i]);
                dataList.add(map);
            }
        }
    
    }

    主activity

  • 相关阅读:
    需求获取常见的方法是进行客户访谈,结合你的实践谈谈会遇到什么问题,你是怎么解决的?
    软件工程导论 习题五
    软件工程导论 习题四
    软件工程导论 习题三
    软件工程导论 习题二(1.2.3.5)
    软件工程导论 习题一
    面向对象分析方法和面向过程分析方法的区别
    几大开发模型区别与联系
    项目总结
    关于需求
  • 原文地址:https://www.cnblogs.com/Ocean123123/p/10988980.html
Copyright © 2011-2022 走看看