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>>();
结果图: