ListView与GridView是Android开发中的常用控件,它们和Adapter配合使用能够实现很多界面效果。下面分别以实例说明ListView、GridView的用法。
1.ListView的Android开发实例
ListView 是android开发中最常用的控件之一,一般构成列表包括三个元素,ListView:用来展示列表的视图、Adapter:数据与视图连接的桥梁、Data:具体的数据包括字符串 、图片或者控件。
适配器一般有以下几种类型:
ArrayAdapter:Android中最简单的一种适配器,专门用于列表控件。只显示一行数据。
SimpleAdapter:此适配器有最好的扩充性,可以自定义出各种效果。经常使用静态数据填充列表。
CursorAdapter:通过游标向列表提供数据。
ResourceCursorAdapter:这个适配器扩展了CursorAdapter,知道如何从资源创建视图。
SimpleCursorAdapter:这个适配器扩展了ResourceCursorAdapter,从游标中得列创建 TextView/ImageView视图。下面获取通讯录的示例:
XML/HTML代码
<?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>
Java代码
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 } }); } }
2.GridView的Android开发实例
GridView 网格视图,用于显示多行多列。直接上示例:
XML/HTML代码
<?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/HTML代码 <?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>
Java代码
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的使用有了一定掌握了。大家还可以使用它们和Adapter实现更多的功能试试。
以上就是对Android ListView 和GridView 的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!