android ListView,GridView 使用小结

本文详细介绍了如何在Android开发中使用动态数组和适配器来生成和展示动态数据,通过实例展示了从创建动态数组、适配器配置到数据展示的全过程,包括UI组件的布局和XML文件的实现。
1、//生成动态数组,并且转入数据  
      ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();  
      for(int i=0;i<10;i++)  
      {  
        HashMap<String, Object> map = new HashMap<String, Object>();  
        map.put("ItemImage", R.drawable.icon);//添加图像资源的ID  
    map.put("ItemText", "NO."+String.valueOf(i));//按序号做ItemText  
        lstImageItem.add(map);  
      }  
 2、     //生成适配器的ImageItem <====> 动态数组的元素,两者一一对应  
      SimpleAdapter saImageItems = new SimpleAdapter(this, //没什么解释  
                                                lstImageItem,//数据来源   
                                                R.layout.night_item,//night_item的XML实现  
                                                  
                                                //动态数组与ImageItem对应的子项          
                                                new String[] {"ItemImage","ItemText"},   
                                                  
                                                //ImageItem的XML文件里面的一个ImageView,两个TextView ID  
                                                new int[] {R.id.ItemImage,R.id.ItemText});  
  3、    //添加并且显示  

      gridview.setAdapter(saImageItems); 


代码实现:

res/layout/third.xml  的源代码,就是它实现ImageItem的UI:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/txtv"
        android:text="thrid activiy"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         />

    <GridView
        android:id="@+id/txtlist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:columnWidth="90sp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        />
</LinearLayout>



thirdlist.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="12dip"
    android:paddingRight="12dip"
    android:paddingBottom="4dip"
     >
    <ImageView
        android:paddingTop="12dip"
        android:contentDescription="alter img"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/itemimage"
        />
    <TextView
        android:id="@+id/itemtitle"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textSize="20sp"
         />
     <TextView
        android:id="@+id/itemtext"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textSize="20sp"
        android:layout_below="@id/itemtitle"
         />
    
    

</RelativeLayout>

//java


    package com.example.helloword;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class ThirdActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.third);
        
        GridView lv = (GridView)findViewById(R.id.txtlist);
        //生成动态数组,加入数据
        ArrayList<HashMap<String,Object>> listItem  = new ArrayList<HashMap<String,Object>>();
        for(int i = 0;i < 10 ;i++)
        {
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("image", R.drawable.p1);
                map.put("title", "itemtitle" + i);
                map.put("text", "itemtext" + i);
                listItem .add(map);
        }
        ////生成适配器的Item和动态数组对应的元素  
        SimpleAdapter sadapter = new SimpleAdapter(this,
                listItem ,//数据源
                R.layout.thirdlist,//xml
                new String[]{"image","title","text"},//动态数组与ImageItem对应的子项                  
                new int[]{ R.id.itemimage,R.id.itemtitle,R.id.itemtext });
        //添加并且显示  
       lv.setAdapter(sadapter);
       
        
        
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值