AdapterView对象.setAdapter(Adapter对象); |
1)ArrayAdapter
列表的显示需要三个元素:
a.ListVeiw 用来展示列表的View。
b.适配器 用来把数据映射到ListView上的中介。
c.数据 具体的将被映射的字符串,图片,或者基本组件。
一.
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.xiaoyun.listview.MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
main文件
package com.example.xiaoyun.listview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView ls;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ls=(ListView)findViewById(R.id.listView);
ls.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,getData()));
// setContentView(ls);
}
private List<String> getData()
{
List<String> data=new ArrayList<String>();
data.add("1");
data.add("2");
data.add("3");
data.add("4");
return data;
}
}
<img src="https://img-blog.csdn.net/20160305122005504?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
2)SimpleAdapter
simpleAdapter的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片),还可以放上Button(按钮),CheckBox(复选框)等等。下面的代码都直接继承了ListActivity,ListActivity和普通的Activity没有太大的差别,不同就是对显示ListView做了许多优化,方面显示而已。
public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
Added in
API level 1
Constructor
Context
: The context where the View associated with this SimpleAdapter is running
List
: A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from"int
: Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to"String
: A list of column names that will be added to the Map associated with each item.int
: The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal">
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3px" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="40sp" />
<TextView
android:id="@+id/info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
main.java
package com.example.xiaoyun.simpleadapter;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import android.app.ListActivity;
import android.os.Bundle;
import javax.sql.RowSetListener;
//要改成继承自ListActivity!!!!!
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 下面这句话是一定不需要的
// setContentView(R.layout.activity_main);
//
SimpleAdapter adapter = new SimpleAdapter(this
, getData(), R.layout.activity_main, new String[]{"img", "title", "info"},
new int[]{R.id.img, R.id.title, R.id.info}
);
// 在当前视图view绑定adapter适配器
setListAdapter(adapter);
}
//数据源
private List<Map<String, Object>> getData() {
// 创建list并将每一个map添加到list中
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("img", R.drawable.bookmark);
map.put("title", "Book");
map.put("info", "aa");
list.add(map);
map = new HashMap<String, Object>();
map.put("img", R.drawable.calendar);
map.put("title", "calendar");
map.put("info", "adf");
list.add(map);
map = new HashMap<String, Object>();
map.put("img", R.drawable.camera);
map.put("title", "camera");
map.put("info", "ca");
list.add(map);
// 返回list
return list;
}
}