展会信息港展会大全

Android中关于Adapter的使用(中)SimpleAdapter
来源:互联网   发布日期:2015-10-02 16:16:44   浏览:1632次  

导读:在前面的两篇文章中,我们讲到了关于ArrayAdapter的使用。用ArrayAdapter来在ListView中展示数据是很不错的,但是很多时候,我们的ListView中,可不只是展示文字,...

在前面的两篇文章中,我们讲到了关于ArrayAdapter的使用。用ArrayAdapter来在ListView中展示数据是很不错的,但是很多时候,我们的ListView中,可不只是展示文字,我们还想展示图片呢。

可能有些朋友刚才会问,第二篇不是已经可以展示图片了吗?是的呀,但是它就只能展示我们在xml中定义给它的那一张埃

而究其原因,其实是因为我们传给它的数据源就只有字符串,没有传给图片给它,而事实上,我们也只能传文字给它,因为它用的是TextView嘛。

所以,光用ArrayAdapter显然不够丰富多彩,生活呀,总得有点不一样吧。

接下来我们就来看看在Android中关于SimpleAdapter的使用吧。

SimpleAdapter

SimpleAdapter其实一点也不simple的,因为我们需要自己去定义每个listitem的布局,如下:

我们定义了1个ImageView控件和两个TextView控件。

接下来我们看看MainActivity中的代码:

List> simpleList = new ArrayList>();

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initSimpleList();

ListView listView = (ListView)findViewById(R.id.listView1);

SimpleAdapter adapter = new SimpleAdapter(this, simpleList, R.layout.simpleadapter,

new String[] {"title","content","drawable"},

new int[] {R.id.tvTitle,R.id.tvContent,R.id.imageView1});

listView.setAdapter(adapter);

}

private void initSimpleList(){

String[] titles = {"title1", "title2", "title3","title4", "title5", "title6"};

String[] contents = {"content1", "content2", "content3","content4", "content5", "content6"};

int[] drawableIds = {R.drawable.computer,R.drawable.excel,R.drawable.game,

R.drawable.gc,R.drawable.network,R.drawable.pdf};

Map map;

for(int i=0;i();

map.put("title", titles[i]);

map.put("content", contents[i]);

map.put("drawable", drawableIds[i]);

simpleList.add(map);

}

}

从上面的代码中,我们可以看到,跟ArrayAdapter类似,也要先创建一个SimpleAdapter,其构造函数如下:

/**

* Constructor

*

* @param context The context where the View associated with this SimpleAdapter is running

* @param data 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"

* @param resource 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"

* @param from A list of column names that will be added to the Map associated with each

*item.

* @param to 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.

*/

public SimpleAdapter(Context context, List extends Map> data,

int resource, String[] from, int[] to) {

mData = data;

mResource = mDropDownResource = resource;

mFrom = from;

mTo = to;

mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}1)context,上下文

2)List extends Map> data,这是一个包含Map对象的list,也就是说,每个map对象都是每个item对应的内容。

3)resource,对应的就是这个item的layout,也就是我们上面自定义的布局。

4)from 和 to,这两个参数,其实就是做一个映射,将map中key对应的值,传给layout中每个对象的id。比如,我们上面在map中设置了title,那么这个title的值就是设给item布局对应的TextView(tvTitle),而其它的事情就由SimpleAdapter来帮我们操作了。

下面就是效果图(有点丑,请不要见怪,@-@!!):

虽然说SimpleAdapter一点也不simple,但是如果我们理解了,用起来是不是也是挺简单的呢?

大家如果对ArrayAdapter有兴趣的话,可以看看前面关于ArrayAdapter的文章:

Android中关于Adapter的使用(上)ArrayAdapter

Android中关于Adapter的使用(再上)ArrayAdapter

赞助本站

人工智能实验室

相关热词: android开发 教程

相关内容
AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港