Android自定义适配器,BaseAdapter的使用方法

本文介绍如何在Android应用中自定义ListView适配器来显示已安装应用的图标和名称。通过创建自定义适配器类MyAdapter,实现BaseAdapter接口,并结合布局文件item.xml,实现了将获取到的应用信息展示在ListView上。
摘要由CSDN通过智能技术生成

适配器,顾名思义,是为了连接某些不能直接关联的数据或控件等,android为我们提供了多种多样的适配器,但往往不能达到自己理想的效果,这里就简单总结了怎样自定义适配器的方法。

这里,我们的程序是获取当前已安装的应用名称以及图标,并将其显示在ListView中:

由于代码简单,所以就木有写注释。。。

首先,我们先完成布局文件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:id="@+id/imageView_icon"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView_name"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:text="Large Text"
        android:gravity="center_vertical"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

然后是主 MainActivity

package com.wx.test_getallapps;

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

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ListView;

public class MainActivity extends Activity {
	private Context context;
	private List<PackageInfo> packageInfos;
	private List<Map<String, Object>> data;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = this;
		setContentView(R.layout.activity_main);
		packageInfos = getPackageManager().getInstalledPackages(0);
		data = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < packageInfos.size(); i++) {
			PackageInfo packageInfo = packageInfos.get(i);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("icon",
					packageInfo.applicationInfo.loadIcon(getPackageManager()));
			map.put("name",
					packageInfo.applicationInfo.loadLabel(getPackageManager())
							.toString());
			data.add(map);
		}
		MyAdapter myAdapter = new MyAdapter(context, data);
		ListView listView = (ListView) findViewById(R.id.listView_item_checked);
		listView.setAdapter(myAdapter);
	}

	@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;
	}

}

最后登场的就是 MyAdapter

package com.wx.test_getallapps;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter {
	
	private List<Map<String,Object>> data = null;
	private LayoutInflater mlInflater;
	public MyAdapter(){
		
	}
	
	public MyAdapter(Context context, List<Map<String, Object>> data) {
		// TODO Auto-generated constructor stub
		this.data = data;
		mlInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder;
		if(convertView == null){
			convertView = mlInflater.inflate(R.layout.item, null);
			viewHolder = new ViewHolder();
			viewHolder.imageView = (ImageView)convertView.findViewById(R.id.imageView_icon);
			viewHolder.textView = (TextView)convertView.findViewById(R.id.textView_name);
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder)convertView.getTag();
		}
		
		Map<String , Object> map = data.get(position);
		
		viewHolder.imageView.setImageDrawable((Drawable) map.get("icon"));
		viewHolder.textView.setText((CharSequence) map.get("name"));
		
		return convertView;
	}

	class ViewHolder{
		ImageView imageView;
		TextView textView;
	}
	
}


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值