先看一下成果吧(图片有点Low,用的系统自带的):
SimpleAdapter的用法比ArrayAdapter还要复杂一点,毕竟功能更强大嘛。先看SimpleAdapter的构造方法:
public SimpleAdapter(Context context, List extends Map> data, int resource, String[] from, int[] to) {}
详细说一下方法中的5个参数:
* Context:上下文。
* data:数据源(List
data数据源我专门用ItemData类生成:
package com.engineer.shizhibin.tvshow;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by 13922 on 2018/6/17.
*/
public class ItemData {
private static String[] mDataList = new String[] {
"CCTV1综合","CCTV2财经","CCTV3综艺","CCTV4中文国际","CCTV5","CCTV6","CCTV7","CCTV8",
"CCTV9","CCTV10科教","CCTV11戏曲","CCTV12社会与法","CCTV13新闻"
};
private static String[] mUrlList = new String [] {};
private static List
每一个Item的布局,需要自定义item.xml:(注意id要和SimpleAdapter构造方法中的to参数对应)
在MainActivity中创建Adapter绑定ListView:
package com.engineer.shizhibin.tvshow;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private ListView mListview;
private SimpleAdapter mAdpater;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListview = (ListView)this.findViewById(R.id.list);
/**
* 参数1 上下文
* 参数2 当前listview加载的每一个列表项对应的布局文件
* 参数3 数据源
*/
String[] list = ItemData.getmDataList();
List> data = new ArrayList>();
data = ItemData.getDataLIst("pic","text");
/**
* SimpleAdapter的五个参数:
* Context:上下文
* data:数据源(List>data)一个有Map组成的集合
* 每一个Map都会对应ListView列表中的一行
* 每一个Map(键-值对)中的键必须包含所有在from参数中所指定的键
*resource: ListView中每一项的布局文件ID
*from:Map中的键名
*to:绑定item.xml文件中的view控件ID,与from,Map的键形成对应
*/
mAdpater = new SimpleAdapter
(MainActivity.this,data,R.layout.item,new String[]{"pic","text"},new int[]{R.id.pic,R.id.text});
mListview.setAdapter(mAdpater);
}
}