`
xiehongdong
  • 浏览: 68629 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

ListView组件与SimpleAdapter

阅读更多

记录两个ListView的简单小例子.其中使用到 了SimpleAdapter适配器。

 

1、不带图片

listview.xml

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:paddingLeft="10dip"
  android:paddingRight="10dip"
  android:paddingTop="1dip"
  android:paddingBottom="1dip">
    
    <TextView   
	    android:id="@+id/name"  
	    android:layout_width="180dip"  
	    android:layout_height="30dip"  
	    android:textSize="10pt"  
	    android:singleLine="true"  
  		/>   
  
	  <TextView   
	    android:id="@+id/phone"  
	    android:layout_width="fill_parent"  
	    android:layout_height="fill_parent"  
	    android:gravity="right"  
	    android:textSize="10pt"  
	  />
    
</LinearLayout>

 

ListViewActivity.java

public class ListViewActivity extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();
        
        HashMap<String, String> map1 = new HashMap<String, String>();
        HashMap<String, String> map2 = new HashMap<String, String>();
        HashMap<String, String> map3 = new HashMap<String, String>();
        map1.put("name", "张三");
        map1.put("phone", "13800138000");
        map2.put("name", "李四");
        map2.put("phone", "13800138022");
        map3.put("name", "王武");
        map3.put("phone", "13800138033");
        
        list.add(map1);
        list.add(map2);
        list.add(map3);
        
        SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.info,
        							new String[]{"name","phone"},new int[]{R.id.name,R.id.phone});
        setListAdapter(adapter);
        
        
    }

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Toast.makeText(ListViewActivity.this, ((HashMap)l.getItemAtPosition(position)).get("name").toString(), Toast.LENGTH_LONG).show();
	}

 

运行效果:



 

2、带有图片效果:

listviewtwo.xml

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
    <ImageView android:id="@+id/image"
    	android:layout_width="30px"
    	android:layout_height="30px"
    	android:layout_margin="5px"/>
    <LinearLayout android:id="@+id/linerlayout"
    	android:layout_width="wrap_content"
    	android:layout_height="fill_parent"
    	android:orientation="vertical"
    	android:gravity="center_vertical">
    	
    	<TextView android:id="@+id/name1"
    		android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:textSize="20px"/>
    	
    	<TextView android:id="@+id/phone1"
    		android:layout_width="wrap_content"
    		android:layout_height="fill_parent"
    		android:textSize="10px"/>
    	
   </LinearLayout>
</LinearLayout>

 

 

ListViewTwoActivity.java

public class ListViewTwoActivity extends ListActivity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String,Object>>();
        
        HashMap<String, Object> map1 = new HashMap<String, Object>();
        HashMap<String, Object> map2 = new HashMap<String, Object>();
        HashMap<String, Object> map3 = new HashMap<String, Object>();
        map1.put("name", "张三");
        map1.put("phone", "13800138000");
        map1.put("image", R.drawable.icon);
        map2.put("name", "李四");
        map2.put("phone", "13800138022");
        map2.put("image", R.drawable.jinji1);
        map3.put("name", "王武");
        map3.put("phone", "13800138033");
        map3.put("image", R.drawable.jixugoucai);
        
        list.add(map1);
        list.add(map2);
        list.add(map3);
        list.add(map1);
        list.add(map2);
        list.add(map3);
        list.add(map1);
        list.add(map2);
        list.add(map3);
        list.add(map1);
        list.add(map2);
        list.add(map3);
        list.add(map1);
        list.add(map2);
        list.add(map3);
        list.add(map1);
        list.add(map2);
        list.add(map3);
        list.add(map1);
        list.add(map2);
        list.add(map3);
        
        SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.listviewtwoinfo,
        			new String[]{"name","phone","image"},new int[]{R.id.name1,R.id.phone1,R.id.image});
        
        setListAdapter(adapter);
	}
	
}

 

运行效果:



 

  • 大小: 13.8 KB
  • 大小: 29.3 KB
分享到:
评论

相关推荐

    ListView和SimpleAdapter的简单应用

    在Android开发中,ListView是一个非常重要的组件,它用于展示大量数据列表,用户可以通过滚动查看不同条目。SimpleAdapter则是ListView常用的适配器,它简化了数据绑定和UI展示的过程。本篇文章将深入探讨ListView和...

    ListView与SimpleAdapter导航简单实例

    在Android开发中,ListView是一个非常重要的组件,常用于展示大量数据列表,比如联系人列表、邮件列表等。它具有良好的可滚动性和可交互性,能够有效地利用屏幕空间。本实例将探讨如何结合SimpleAdapter来创建一个...

    listview实例 simpleAdapter自定义列

    在Android开发中,ListView是一种常用的UI组件,用于展示大量数据列表。这个实例是关于如何使用SimpleAdapter来自定义ListView的列显示。SimpleAdapter是Android提供的一种便捷方式,它可以帮助我们快速将数据绑定到...

    ListView使用SimpleAdapter和自定义Adapter填充数据

    在Android开发中,ListView是一个非常常见的组件,用于展示大量数据的列表形式。在这个场景中,我们探讨了如何使用`SimpleAdapter`和自定义`Adapter`来填充ListView,并且在自定义Adapter中添加了删除功能。下面我们...

    Android之simpleAdapter的ListView

    在Android开发中,ListView是一种非常常见且重要的组件,它用于展示大量的数据列表,通常与适配器(Adapter)配合使用,以实现数据和视图之间的解耦。本教程将聚焦于`SimpleAdapter`,它是Android提供的一种简单易用...

    5-ListView-SimpleAdapter及自定义Adapter-进阶2.rar

    本教程将深入探讨如何使用SimpleAdapter和自定义Adapter与ListView协同工作,以实现数据的动态展示。以下是对相关知识点的详细说明: 1. **ListView**: ListView是Android中的一个视图容器,可以显示一系列可滚动的...

    如何使用ListView组件列表显示数据

    本文将详细介绍如何使用`ListView`组件来显示数据,包括创建自定义布局、绑定数据以及使用`SimpleAdapter`简化数据绑定过程。 ### 创建自定义布局 为了使`ListView`中的每一项具有特定的外观,我们需要创建一个...

    Android源码:ListView组件应用演示代码

    Android源码:ListView组件应用演示代码,layout的xml文件名需要全部小写。  ActivityList1:   1.初步介绍ListView这个组件。   2.初步介绍ArrayAdapter。   # simple_list_item_single_choice   # ...

    使用SimpleAdapter创建ListView

    在Android开发中,ListView是一种常见的UI组件,用于展示大量数据列表。`SimpleAdapter`是Android提供的一个方便快捷的适配器,它可以帮助我们快速将数据绑定到ListView上,而无需自定义复杂的Adapter。本篇文章将...

    android-listView-SimpleAdapter-进阶1.rar

    在Android开发中,ListView是用于显示可滚动列表的视图组件,经常被用来展示大量数据。SimpleAdapter是Android提供的一种简单易用的适配器,它可以帮助开发者将数据集(如数组或列表)绑定到ListView上,使得数据...

    SimpleAdapter实现ListView表格效果

    3. **实例化SimpleAdapter**:传入Context、数据集合、布局资源ID、数据与视图映射的键值对数组以及视图ID数组。键值对数组用于指定数据集合中的哪一列数据映射到布局文件中的哪个视图;视图ID数组则是布局文件中...

    SimpleAdapter实现LIstView的范例[有注解]

    在Android开发中,ListView是一种常用的组件,用于展示大量的列表数据。`SimpleAdapter`是Android SDK提供的一种简单方便的适配器,它可以帮助我们快速将数据绑定到ListView上。本范例将详细介绍如何使用`...

    利用JDBC读取mysql数据展示在listview

    在本教程中,我们将深入探讨如何使用Java的JDBC(Java Database Connectivity)接口来从MySQL数据库中读取数据,并将这些数据动态地显示在Android应用的ListView组件上,同时利用SimpleAdapter进行数据绑定。...

    浅谈使用Android中ListView组件.pdf

    在Android应用开发中,ListView组件是开发者们最常使用的控件之一,用于展示大量数据的列表形式。ListView的高效性和可定制性使得它在各种应用中都有广泛的应用,如通讯录、消息列表、购物清单等。本文将深入探讨...

    Android之ListView<3>ArrayAdapter,SimpleAdapter

    2. 自动绑定:与ArrayAdapter类似,当数据集发生变化时,SimpleAdapter也会自动调用`notifyDataSetChanged()`来更新ListView。 两者的主要区别在于: 1. 数据结构:ArrayAdapter直接使用List,而SimpleAdapter使用...

    SimpleAdapter基础小程序

    在Android开发中,ListView是一种非常常见且重要的组件,它用于展示大量的数据列表,通常与Adapter配合使用以实现数据和视图的绑定。本教程将深入讲解如何使用SimpleAdapter创建一个基础的小程序,来展示一个包含...

    Android SimpleAdapter的多种显示GridView/ListView

    在Android开发中,SimpleAdapter是一种常用的适配器,它用于将数据绑定到视图上,如GridView或ListView。这些组件是Android中展示列表数据的主要方式,适用于创建各种类型的列表应用。下面将详细介绍如何使用...

    Android UI:ListView - SimpleAdapter实例详解

    在Android开发中,ListView是一种常用的组件,用于展示可滚动的列表数据。SimpleAdapter是ListView的适配器之一,它的特点是简单易用且具有高度的可扩展性,允许开发者自定义列表项的布局。本篇文章将深入讲解如何...

    SimpleAdapter

    在Android开发中,适配器(Adapter)是一个至关重要的组件,它充当了数据源与UI控件之间的桥梁,尤其在处理列表或者网格等可滚动视图时。标题提到的"SimpleAdapter"是Android SDK提供的一种简易适配器,主要用于将...

Global site tag (gtag.js) - Google Analytics