ArrayAdapter()与SimpleAdapter()适配器

ArrayAdapter<T>(上下文,每个条目对应的布局文件,要求改的textView内容,每个条目显示的内容);


ArrayAdapterActivity.java

<span style="color:#ffffff;">p</span><span style="color:#330099;">ublic class ArrayAdapterMainActivity extends Activity {
	ListView lv ;
	String[] names={"item_1","item_2","item_3","item_4","item_5","item_6",};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_array_adapter);
		lv = (ListView) findViewById(R.id.listView);
		lv.setAdapter(
				new ArrayAdapter<String>
				(getApplicationContext(), R.layout.activity_arrayadapter_item, R.id.arrayAdapterItemTv,names));

	}
</span>
avtivity_array_adapter.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.android_4_4.ArrayAdapterMainActivity" >

    <ListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</RelativeLayout>
activity_arrayadapter_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/ic_launcher"
         />
     <TextView
        android:id="@+id/arrayAdapterItemTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</LinearLayout>

SimpleAdapterActivity.java

public class <span style="color: rgb(204, 0, 0);">SimpleAdapterActivity </span>extends Activity {
	ListView lv ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_array_adapter);
		lv = (ListView) findViewById(R.id.listView);
		 List< Map<String,Object>> data = new ArrayList<Map<String,Object>>();
		 
		 Map<String,Object> map1 = new HashMap<String, Object>();
		 map1.put("name", "功能_1");
		 map1.put("iconId",R.drawable.aaa);
		 
		 Map<String,Object> map2 = new HashMap<String, Object>();
		 map2.put("name", "功能_2");
		 map2.put("iconId",R.drawable.bbb);
		 
		 Map<String,Object> map3 = new HashMap<String, Object>();
		 map3.put("name", "功能_3");
		 map3.put("iconId",R.drawable.ccc);
		 
		 data.add(map1);
		 data.add(map2);
		 data.add(map3);
		 
		
		 lv.setAdapter(new SimpleAdapter(
				 this, 
				 data, 
				 R.layout.list_item,//每个条目对应的布局文件,
				 new String[]{"name","iconId"},//from
				 new int[]{R.id.listItemTv,R.id.listItemIV}//to
				 ));
	}
}	

数据结构:List< Map<String,Object>> data = new ArrayList<Map<String,Object>>();


结果图:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值