今天给大家分享一下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>