ListView的两种实现方法

     今天给大家分享一下ListView的两种实现。

一、ArrayAdapter

public ArrayAdapter (Context context, int resource, T[] objects)

context: 上下文. 
resource: 资源,包含了一个TextView组件
objects :ListView呈现的内容

下面来看代码

MainActivity<span style="font-family: 'microsoft yahei'; background-color: rgb(255, 255, 255);">.java</span>

public class MainActivity extends Activity {

	private static String[] name = {"功能1","功能2","功能3","功能4","功能5"};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		ListView lv = (ListView) findViewById(R.id.lv);
		lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.tv, name));
	}
}
activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.arrayadapter2.MainActivity" >

   <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</RelativeLayout>
list_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="horizontal" >
     <ImageView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/wzx"
        />

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    

</LinearLayout>
二、SimpleAdapter

SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

Context :上下文

 List<? extends Map<String, ?>> data:一个Map组成的List数据结构,在列表中的每个条目对应列表中的一行,每一个Map中应该包含所有在from参数中指定的键(Key)

int resource:一个定义列表项的布局文件的资源ID,布局文件将至少包含那些在to中定义的ID

String[] from:一个将被添加到Map映射上的键名(key)
int[] to:将绑定数据的视图的ID(map中的value),跟from对数对应,并且应该是view

MainActivity.java

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		ListView lv = (ListView) findViewById(R.id.lv);
		List<Map<String,Object>> data = new ArrayList<Map<String, Object>>();
		
		Map<String, Object> map1 = new HashMap<String,Object>();
		map1.put("name", "我是宇智波鼬");
		map1.put("pcid", R.drawable.c);
		Map<String, Object> map2 = new HashMap<String,Object>();
		map2.put("name", "我是宇智波鼬");
		map2.put("pcid", R.drawable.v);
		Map<String, Object> map3 = new HashMap<String,Object>();
		map3.put("name", "我是宇智波鼬");
		map3.put("pcid", R.drawable.w);
		Map<String, Object> map4 = new HashMap<String,Object>();
		map4.put("name", "我是宇智波鼬");
		map4.put("pcid", R.drawable.z);
		Map<String, Object> map5 = new HashMap<String,Object>();
		map5.put("name", "我是宇智波鼬");
		map5.put("pcid", R.drawable.x);
		
		data.add(map1);
		data.add(map2);
		data.add(map3);
		data.add(map4);
		data.add(map5);
		lv.setAdapter(new SimpleAdapter(this, data, R.layout.list_item, new String[]{"pcid","name"}, new int[]{R.id.iv,R.id.tv}));
		
	}
}
activity_main.xml
<pre name="code" class="html"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    tools:context="com.example.simpleadapter.MainActivity" >

    <ListView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lv"
        ></ListView>

</RelativeLayout>


 list_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="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值