欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

一,android开发经典实例系列-----ListView、GridView

程序员文章站 2022-03-03 08:16:53
...

 
 1,ListView 是android开发中最常用的控件之一,一般构成列表包括三个元素,ListView:用来展示列表的视图、Adapter:数据与视图连接的桥梁、Data:具体的数据包括 字符串 图片 或者控件。

     适配器一般有以下几种类型

     ArrayAdapter:Android中最简单的一种适配器,专门用于列表控件。只显示一行数据。

     SimpleAdapter:此适配器有最好的扩充性,可以自定义出各种效果。经常使用静态数据填充列表。

     CursorAdapter:通过游标向列表提供数据。

     ResourceCursorAdapter:这个适配器扩展了CursorAdapter,知道如何从资源创建视图。

     SimpleCursorAdapter:这个适配器扩展了ResourceCursorAdapter,从游标中得列创建 TextView/ImageView视图。下面获取通讯录的示例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg"
   
    >
   	<ListView 
   	android:id="@+id/contacts_list"
   	android:layout_width="fill_parent"
   	android:layout_height="fill_parent"
	>
	</ListView>
</LinearLayout>

 

package net.csdn.blog.androidtoast;

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

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	 ListView     mListView;
	 ArrayList<Map<String, String>> listData;
	     
    static final String NAME = "name";
	static final String NUMBER = "number";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        getContacts();
    }

    /**
     * 获取联系人列表
     */
	private void getContacts() {
		mListView=(ListView) findViewById(R.id.contacts_list);
		listData = new ArrayList<Map<String, String>>();
		//获取数据库Cursor
		Cursor cur=getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
		startManagingCursor(cur);
		while (cur.moveToNext()) {
          Map<String, String> mp = new HashMap<String, String>();
          long id = cur.getLong(cur.getColumnIndex("_id"));
		  Cursor pcur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
			                  null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + Long.toString(id), 
		                      null, null);
		          
			            // 处理多个号码的情况
		           String phoneNumbers = "";
		           while (pcur.moveToNext()) {
			               String strPhoneNumber = pcur.getString(pcur.getColumnIndex(
			                       ContactsContract.CommonDataKinds.Phone.NUMBER));
			              phoneNumbers += strPhoneNumber + ":";
			           }
			           phoneNumbers += "\n";
			           pcur.close();
			           
			           String name = cur.getString(cur.getColumnIndex("display_name"));
			           mp.put(NAME, name);
			           mp.put(NUMBER, phoneNumbers);
			           listData.add(mp);
			        }
		 cur.close();
		 
		// 建立一个适配器去查询数据
        ListAdapter adapter = new SimpleAdapter(this, listData, android.R.layout.simple_list_item_2,
               new String[]{NAME, NUMBER},
               new int[] {android.R.id.text1, android.R.id.text2});
        mListView.setAdapter(adapter);
		//为listView添加事件监听
		mListView.setOnItemSelectedListener(new ListView.OnItemSelectedListener(){

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(getApplicationContext(), "当前所在行为:"+Long.toString(parent.getSelectedItemId()+1), 1).show();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				
			}
			
		});
	}
}

  
一,android开发经典实例系列-----ListView、GridView
            
    
    博客分类: android--应用实战 andoridListViewGridViewAdapter
 2,GridView 网格视图,用于显示多行多列。直接上示例:

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

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent" android:scrollbars="vertical">
	 <ImageView   
               android:layout_height="100dip"   
               android:id="@+id/ItemImage"   
               android:layout_width="80dip" 
               android:src="@drawable/png1" 
               android:layout_gravity="center_horizontal"/> 
                 
         <TextView   
               android:layout_width="wrap_content"   
               android:layout_height="wrap_content" 
               android:layout_gravity="center"  
               android:id="@+id/ItemText" />  
</LinearLayout>

 

package net.csdn.blog.androidtoast;

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

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
	//定义图片整型数组
	private int[] mImages={
			R.drawable.png1,
			R.drawable.png2,
			R.drawable.png3,
			R.drawable.png4,
			R.drawable.png5,
			R.drawable.png6,
			R.drawable.png7,
			R.drawable.png8,
			R.drawable.png9

	};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //实例化GridView
        GridView mGridView=(GridView) findViewById(R.id.gridview);
        // 生成动态数组,并且传入数据
        ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();
        
        for (int i = 0; i < 9; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("ItemImage", mImages[i]);// 添加图像资源的ID
            map.put("ItemText", "NO." + String.valueOf(i+1));// 按序号做ItemText
            lstImageItem.add(map);
        }
        //构建一个适配器
        SimpleAdapter simple = new SimpleAdapter(this, lstImageItem,
                R.layout.gridviewitem,
                new String[] { "ItemImage", "ItemText" }, new int[] {
                        R.id.ItemImage, R.id.ItemText });
        mGridView.setAdapter(simple);
        //添加选择项监听事件
        mGridView.setOnItemClickListener(new GridView.OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast toast=Toast.makeText(getApplicationContext(), "你选择了"+(position+1)+"号图片", 1);
				toast.setGravity(Gravity.BOTTOM, 0, 0);
				toast.show();
			}
        	
        });
        
    }
}

 
一,android开发经典实例系列-----ListView、GridView
            
    
    博客分类: android--应用实战 andoridListViewGridViewAdapter
 
一,android开发经典实例系列-----ListView、GridView
            
    
    博客分类: android--应用实战 andoridListViewGridViewAdapter
 

  • 一,android开发经典实例系列-----ListView、GridView
            
    
    博客分类: android--应用实战 andoridListViewGridViewAdapter
  • 大小: 42.8 KB
  • 一,android开发经典实例系列-----ListView、GridView
            
    
    博客分类: android--应用实战 andoridListViewGridViewAdapter
  • 大小: 71.4 KB
  • 一,android开发经典实例系列-----ListView、GridView
            
    
    博客分类: android--应用实战 andoridListViewGridViewAdapter
  • 大小: 71.9 KB