• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

Android开发中使用GridView实现数据网格显示

Android 水墨上仙 1602次浏览

Android开发中使用GridView实现数据网格显示
转自:http://blog.csdn.net/ssun125/article/details/7627135

UsingGridView.java代码

package com.sinaapp.ssun.girdview;
import java.util.*;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class UsingGridViewActivity extends Activity {
	private GridView gridView;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);         
       
        gridView = (GridView) this.findViewById(R.id.gridView);        
        show();
    } 
	private void show() {  
		List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
		for(int i=0; i<12; i++){
			HashMap<String, Object> hm = new HashMap<String, Object>();
			hm.put("image", R.drawable.ic_launcher);
			hm.put("name", "NO." + i);
			data.add(hm);
		}
		
		SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item, 
                                        new String[]{"image","name"},new int[]{R.id.imageView,R.id.textView});
		
 		gridView.setAdapter(adapter);
 		
 		gridView.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				HashMap<String, Object> hm = (HashMap<String, Object>)parent.getItemAtPosition(position);
				Toast.makeText(UsingGridViewActivity.this, hm.get("name").toString(), Toast.LENGTH_LONG).show();
			}});
	}
}

main.xml文件:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:orientation="vertical" >  
  
    <GridView  
        android:id="@+id/gridView"  
        android:layout_width="wrap_content"  
        android:layout_height="match_parent"  
        android:numColumns="3" >  
    </GridView>  
      
</LinearLayout>  

item.xml文件:

<?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" >  
  
    <ImageView  
        android:id="@+id/imageView"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:src="@drawable/ic_launcher"   
        android:layout_gravity="center_horizontal"/>  
  
    <TextView  
        android:id="@+id/textView"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_gravity="center_horizontal"/>  
      
</LinearLayout>  


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明Android开发中使用GridView实现数据网格显示
喜欢 (0)
加载中……