android适配器之SimpleAdapt

前一章学习的ArrayAdapter在实际开发中的灵活性非常小,显示的内容不仅单一,而且灵活性极差,只能进行简单的数据显示,远远无法满足实际开发中的需求,因此android Api中有自带了SimpleAdapt适配器提供开发人员使用,相比于ArrayAdapter,它具更灵活。还是那句话,先举个简单的Demo,代码注释非常详细,相信都能看懂。

一 布局文件

<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.simpleadapt.MainActivity" >
	
    <ListView android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lv"></ListView>

</RelativeLayout>

二 自定义的ListView样式

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="match_parent" >
    
	<ImageView android:layout_width="60dp"
	    android:id="@+id/image"
	    android:layout_height="60dp"/>
	<LinearLayout android:layout_width="match_parent"
	    android:layout_height="60dp"
	    android:orientation="vertical">
	    <TextView android:layout_width="match_parent"
	        android:layout_height="30dp"
	        android:gravity="left|center_vertical"
	        
	        android:id="@+id/title"/>
	    
	    <TextView android:layout_width="match_parent"
	        android:layout_height="30dp"
	        android:gravity="left|center_vertical"
	        android:id="@+id/desc"/>
	</LinearLayout>
</LinearLayout>
三 MainActivity文件

package com.example.simpleadapt;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.support.v7.app.ActionBarActivity;
import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Adapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {
	private ListView listView;
	private SimpleAdapter simpleAdapter;
	private List<Map<String, Object>> data;//数据集合 其中的一条相当于ListView中的一行
	private Map<String, Object> map;//存储ListView中内容
	//要引入的图片地址
	private int image[] = {R.drawable.ic_launcher,R.drawable.ic_launcher};
	//文章的标题
	String title[] = {"NBA","电影"};
	//文章的简单描述
	String desc[] = {"骑士三巨头怒刷90分,吊打猛龙","最新电影你的名字好评率极高"};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		data = new ArrayList<>();
		
		//填充ListView中每行的数据
		for (int i = 0; i < image.length; i++) {
			map = new HashMap<String, Object>();
			map.put("image", image[i]);
			map.put("title", title[i]);
			map.put("desc", desc[i]);
			data.add(map);
		}
		//找到ListView
		listView = (ListView) findViewById(R.id.lv);
		//定义一个simpleadapt,并初始化
		//1.第一个参数为上下文对象
		//2.第二个参数为要显示的数据集合
		//3.第三个参数为自定义的ListView样式文件
		//4.第四个参数为map中的key值 这个key值必须一一对应要显示内容区域的地址
		//5.第五个参数为ListView中每一行中的标签id,也就是内容放的位置,要和第三个参数一一对应
		simpleAdapter = new SimpleAdapter(this, data, R.layout.mylayout, 
				new String[]{"image","title","desc"}, new int[]{R.id.image,R.id.title,R.id.desc});
		listView.setAdapter(simpleAdapter);//注册adapt
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}

四 结果图:

四 总结

simpleadapt相比于之前的arrayadapt,它使用起来更灵活,可扩张性也大大增强,通过自定义样式文件能灵活的根据自己的需求对内容进行显示。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值